CountA返回0,WorksheetFunction.CountA返回1

时间:2016-08-31 04:49:40

标签: excel vba excel-vba excel-formula excel-2010

在调试窗口中,以下表达式都返回一个。

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,在调试窗口中确认。

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

非常感谢任何解释。

Windows 7,Excel 2010

2 个答案:

答案 0 :(得分:1)

你的语法错了。

更正语法:

Application.WorksheetFunction.CountA (Range(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))

enter image description here

答案 1 :(得分:1)

我相信你在另一个回复中得到了正确语法的答案和一个版本。这是另一种选择:

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 object是通过为左上角和右下角提供两个Range.Cells properties来构建的。父工作表通过With ... End With statement引用。