使用CountA时浏览选择

时间:2019-06-17 12:24:46

标签: excel vba

我试图遍历我的工作表,并计算其中是否有几个不同的范围。目前,我不知道如何在不使用.Select的情况下更改CountA函数中的范围,VBA社区中的大部分人都不喜欢使用.Select,因此我尽力避免使用它。

Dim BoxCounter As Integer
Dim TestCounter As Integer
Dim I As Integer
Dim TestExists As Integer
i=0
With ThisWorkbook.Worksheets("Configuration Sheets")
    While (BoxCounter <= 571) 'goes to the last row used for this specific sheet. 

            If (Application.WorksheetFunction.CountA(("F" & (4 + i * 25) & ":F" & (15 + i * 25))) >= 1) Then
            TestExists = TestExists + 1
            i = i + 1
            BoxCounter = BoxCounter + 30 'adds on so that I can loop through all of the rows, 30 is the size of the specific information spaces
        Wend
    End With

1 个答案:

答案 0 :(得分:1)

使用Application.WorksheetFunction.CountA("range")时,除非您使用"range"格式,否则"worksheet!range"将引用活动的工作表。因此,与其使用字符串范围,不如使用所需工作表中的范围对象:

Dim BoxCounter As Integer
Dim TestCounter As Integer
Dim I As Integer
Dim TestExists As Integer
i=0
With ThisWorkbook.Worksheets("Configuration Sheets")
    While (BoxCounter <= 571) 'goes to the last row used for this specific sheet. 

            If (Application.WorksheetFunction.CountA(.Range("F" & (4 + i * 25) & ":F" & (15 + i * 25))) >= 1) Then
            TestExists = TestExists + 1
            i = i + 1
            BoxCounter = BoxCounter + 30 'adds on so that I can loop through all of the rows, 30 is the size of the specific information spaces
        Wend
    End With