我有用VBA和單元格公式編寫的大量程序。我的任務是將其逆向工程化爲C#winforms。我想了一個開始,我需要在平面列表中看到所有單元格公式。生成所有Excel單元格公式的平面列表
任何現有的方式來做到這一點?提前致謝!
編輯:只是爲了分享,與應答者的幫助下,我設法想出這個:
我有用VBA和單元格公式編寫的大量程序。我的任務是將其逆向工程化爲C#winforms。我想了一個開始,我需要在平面列表中看到所有單元格公式。生成所有Excel單元格公式的平面列表
任何現有的方式來做到這一點?提前致謝!
編輯:只是爲了分享,與應答者的幫助下,我設法想出這個:
,你可以迅速傾倒在所有的公式所有工作表都轉換爲一個平坦的txt文件(改變你的路徑以適應)和一個高效的變體數組。 code sourced from my article here
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
[更新部分 - 你可以在VBA通過使用SpecialCells迅速隔離公式。需要的情況下的錯誤處理有在片材上沒有公式,見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
與brettdj的幫助下,我成功的那一刻
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);
}
}
掀起一個四叉樹搜索下面是一些代碼,我曾經得到與他們的公式一個工作表單元格的列表。它看起來很快。
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
}
您可能會發現跟蹤先例和跟蹤依賴項工具很有用。它們位於Excel 2003的「工具」>「公式審覈」菜單中。有關更高版本的Excel,請參閱http://office.microsoft.com/en-us/excel-help/display-the-relationships-between-formulas-and- cells-HP010066253.aspx如果大量使用可以動態改變公式引用的「INDIRECT」,查看一組公式可能會令人沮喪 - 請參閱http://www.cpearson.com/excel/indirect。 htm – barrowc
謝謝,但這是因爲跟蹤功能不夠好,這就是爲什麼我需要一個列表。我認爲在這種情況下看一張紙的速度會更快。如果有像MSVS那樣的「定義」,那會更好。 – Jake
這非常酷 - 感謝分享! – brettdj