生成所有Excel单元格公式的平面列表

我有一个用VBA和单元格公式编写的大规模程序。 我的任务是将其逆向工程化为C#winforms。 我想了一个开始,我需要查看所有的单元格公式列表。

任何现有的方式来做到这一点? 提前致谢!

编辑:只是分享,在答复者的帮助下,我设法想出了这个:

Excel公式浏览器可让您在树视图中查看先例。

在VBA中(可以很容易地修改为VBScript),你可以快速地将所有表格中的所有公式转储到一个平坦的TXT文件(改变你的path来适应)与一个有效的variables数组。 代码来自我的文章在这里

 Const sFilePath = "C:\test\myfile.txt" Sub CreateTxt_Output() Dim ws As Worksheet Dim rng1 As Range Dim X Dim lRow As Long Dim lCol As Long Dim strTmp As String Dim lFnum As Long lFnum = FreeFile Open sFilePath For Output As lFnum For Each ws In ActiveWorkbook.Worksheets Print #lFnum, "*****" & ws.Name & "*****" 'test that sheet has been used Set rng1 = ws.UsedRange If Not rng1 Is Nothing Then 'only multi-cell ranges can be written to a 2D array If rng1.Cells.Count > 1 Then X = ws.UsedRange.Formula For lRow = 1 To UBound(X, 1) For lCol = 1 To UBound(X, 2) 'write each line to txt file Print #lFnum, X(lRow, lCol) Next lCol Next lRow Else Print #lFnum, rng1.Formula End If End If Next ws Close lFnum MsgBox "Done!", vbOKOnly End Sub 

[更新部分 – 您可以使用SpecialCells在VBA中快速分离公式。 如果工作表上没有公式,则需要error handling,请参阅下面的GetFormula

 Sub GetFormula() Dim ws As Worksheet Dim rng1 As Range Dim rng2 As Range For Each ws In ActiveWorkbook.Sheets Set rng1 = Nothing On Error Resume Next Set rng1 = ws.Cells.SpecialCells(xlCellTypeFormulas) On Error GoTo 0 If Not rng1 Is Nothing Then For Each rng2 In rng1.Areas 'dump cells here Next rng2 End If Next ws End Sub 

这里有一些代码,我用它们来获取工作表上的单元格列表。 这似乎相当快。

 try { Excel.Worksheet excelWorksheet = workbook.ActiveSheet as Excel.Worksheet; Excel.Range formulaCell = excelWorksheet.Cells.SpecialCells( Excel.XlCellType.xlCellTypeFormulas, Type.Missing); Excel.Range cell; foreach (var fc in formulaCell) { cell = fc as Excel.Range; string s1 = cell.Formula as string; int c = cell.Column; int r = cell.Row; // Gives formula text and location of formula. } } catch (Exception) { ; // Throws an exception if there are no results. // Probably should ignore that exception only } 

按键组合CTRL +(返回打勾)在查看值和公式之间切换,它不是一个扁平的列表,但它是有用的。

在brettdj的帮助下,我现在设法search了四叉树

 private static void FindFormula(Excel excel, TextWriter writer, int rowstart, int rowend, int colstart, int colend) { // Select the range excel.Range(rowstart, rowend, colstart, colend); // Check whether this range has formulas if (!excel.RangeHasFormula()) return; // Check if we only have a single cell if (excel.RangeCellCount() == 1) { Console.WriteLine(excel.CellFormula(rowstart, colstart)); return; } int r1, r2, r3, r4; int c1, c2, c3, c4; r1 = rowstart; r2 = rowstart + (rowend - rowstart + 1) / 2 - 1; r3 = r2 + 1; r4 = rowend; if (colstart == colend) { c1 = c2 = c3 = c4 = colstart; FindFormula(excel, writer, r1, r2, c1, c2); FindFormula(excel, writer, r3, r4, c1, c2); } else { c1 = colstart; c2 = colstart + (colend - colstart + 1) / 2 - 1; c3 = c2 + 1; c4 = colend; FindFormula(excel, writer, r1, r2, c1, c2); FindFormula(excel, writer, r1, r2, c3, c4); FindFormula(excel, writer, r3, r4, c1, c2); FindFormula(excel, writer, r3, r4, c3, c4); } }