CountA返回0,WorksheetFunction.CountA返回1

在debugging窗口中,以下expression式全部返回一个。

Application.WorksheetFunction.CountA(Cells(4 + (i - 1) * rows_per_record, 28) & ":" & Cells(5 + (i - 1) * rows_per_record, 58)) Application.WorksheetFunction.CountA(ThisWorkbook.Sheets(n).Cells(4 + (i - 1) * rows_per_record, 28) & ":" & ThisWorkbook.Sheets(n).Cells(5 + (i - 1) * rows_per_record, 58)) 

在这种情况下,范围是AB60:BF61,在debugging窗口中确认。

在工作表中,当脚本暂停时,我input一个单元格=CountA(AB60:BF61) ,结果为0,这应该是。

任何解释将不胜感激。

Windows 7,Excel 2010

你的语法错了。

正确的语法:

Application.WorksheetFunction.CountA (Range(Cells(4 + (i - 1) * rows_per_record, 28), Cells(5 + (i - 1) * rows_per_record, 58)))

你的代码正在计算每个不是空string的参数。 唯一的参数是由两个空单元值包围的冒号。

Application.WorksheetFunction.CountA(ThisWorkbook.Sheets(n).Cells(4 + (i - 1) * rows_per_record, 28) & ":" & ThisWorkbook.Sheets(n).Cells(5 + (i - 1) * rows_per_record, 58))

在这里输入图像说明

我相信你在另一个回应中有正确的语法的答案和一个版本。 这是一个替代:

 dim c as long with ThisWorkbook.Sheets(n) c = Application.CountA(.range(.Cells(4 + (i - 1) * rows_per_record, 28), _ .Cells(5 + (i - 1) * rows_per_record, 58))) debug.print c end with 

Range对象是通过提供两个Range.Cells属性的左上angular和右下angular来构造的。 父工作表是通过With … End With语句引用的。