在 Excel 文件中获取带有公式的单元格 [英] Getting Cells with Formulas in Excel file
本文介绍了在 Excel 文件中获取带有公式的单元格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试阅读单元格的公式,我正在阅读工作表中的所有单元格,这需要太多时间.我怎样才能只选择那些有公式的单元格.
I am trying to read formulas of cells currently I am reading all cells in sheet which takes too much time. How can I only select those cells which have formulas.
这是我使用的代码
foreach (Excel.Worksheet workSht in xWorkBook.Worksheets)
{
for (int rCnt = 1; rCnt <= workSht .Rows.Count; rCnt++)
{
for (int cCnt = 1; cCnt <= workSht .Columns.Count; cCnt++)
{
string str = (string)(workSht.Cells[rCnt, cCnt] as Excel.Range).Formula;
if (str.Contains("_R*"))
{
if (File.Exists(excelFilePath))
{
File.Delete(excelFilePath);
}
CloseExcelObject(ref xWorkBook, ref xApp);
return "UnReviewedFile";
}
}
}
}
推荐答案
在 VBA 中,您可以使用以下语句选择所有包含公式的单元格:
In VBA you can select all the cells containing formulas with the following statement:
Sheet1.Cells.SpecialCells(xlCellTypeFormulas, 23).Select
其中 Sheet1
是对当前工作表的引用,xlCellTypeFormulas = -4123
where Sheet1
is a reference to the current sheet and xlCellTypeFormulas = -4123
这意味着您应该能够使用类似于以下代码(未测试)的公式搜索单元格:
That means you should be able to search through the cells with formulas with something like the following code (not tested):
foreach (Excel.Worksheet workSht in xWorkBook.Worksheets)
{
foreach (var cell in workSht.Cells.SpecialCells(-4123, 23)) {
// your code here
}
}
这篇关于在 Excel 文件中获取带有公式的单元格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文