WorksheetFunction.CountA() 在我认为不应该的时候返回 0

WorksheetFunction.CountA() is returning 0 when i dont think it should be

基本上我需要从第一个 sheet 中获取每种不同类型问题的列表,然后在第二个中显示。

第一个 sheet 有 1 行只是列的标题,然后是 4 行数据。

这很有效,但我想我不小心更改了一些我现在无法解决问题的东西,如果有人能看到问题或者如果有更好的方法那么我会洗耳恭听。

Sub ListQueryTypes()

'Create Variables'
Dim numberOfIssues As Integer
Dim numberOfMacroIssues As Integer
Dim numberOfReportIssues As Integer
Dim numberOfTechnicalIssues As Integer
Dim numberOfTrendIssues As Integer
Dim cellValue As String

'Set query register as active worksheet'
Sheets("Query Register").Activate

'set range for search'
Set myRange = Range("G:G")

'Minus 1 for the first row being column name'
numberOfIssues = Application.WorksheetFunction.CountA(myRange)

'Do, for as many issues as were found previously'
For i = 2 To numberOfIssues + 1
    cellValue = ActiveSheet.Cells(i, 7).Value
        Select Case cellValue
            Case "Macro"
                numberOfMacroIssues = numberOfMacroIssues + 1
            Case "Report"
                numberOfReportIssues = numberOfReportIssues + 1
            Case "Technical"
                numberOfTechnicalIssues = numberOfTechnicalIssues + 1
            Case "Trend"
                numberOfTrendIssues = numberOfTrendIssues + 1

        End Select
Next i
Sheets("Inventory").Activate
ActiveCell = Cells(2, 2)
ActiveCell.Value = numberOfMacroIssues
ActiveCell.Offset(1).Value = numberOfReportIssues
ActiveCell.Offset(2).Value = numberOfTechnicalIssues
ActiveCell.Offset(3).Value = numberOfTrendIssues

收集评论中提到的所有问题,改进您的代码。看看代码中的注释

Option Explicit 'force to declare all variables correctly (this way you don't forget any)

Public Sub ListQueryTypes()

    'Create Variables'
    Dim numberOfIssues As Long 'was Integer but we can always use Long (only advantages) and Excel has more rows than Integer can handle
    Dim numberOfMacroIssues As Long 
    Dim numberOfReportIssues As Long 
    Dim numberOfTechnicalIssues As Long 
    Dim numberOfTrendIssues As Long 
    'Dim cellValue As String  'you don't need that variable see below

    'Set query register as active worksheet'
    'Sheets("Query Register").Activate 'instead of activate we define that sheet as variable which is more save
                                       'and we use Worksheets
    Dim WsQueryReg As Worksheet
    Set WsQueryReg = ThisWorkbook.Worksheets("Query Register")

    'set range for search'
    Dim myRange As Range 'we should to declare all variable properly
    Set myRange = WsQueryReg.Range("G:G") 'we need to define in which sheet the range is

    'Minus 1 for the first row being column name'
    numberOfIssues = Application.WorksheetFunction.CountA(myRange)

    'Do, for as many issues as were found previously'
    Dim i As Long 'declare every variable first
    For i = 2 To numberOfIssues + 1
        'cellValue = WsQueryReg.Cells(i, 7).Value 'no need to write this in a variable we can use it directly in Select Case
        Select Case WsQueryReg.Cells(i, 7).Value 'was cellValue before
            Case "Macro"
                numberOfMacroIssues = numberOfMacroIssues + 1
            Case "Report"
                numberOfReportIssues = numberOfReportIssues + 1
            Case "Technical"
                numberOfTechnicalIssues = numberOfTechnicalIssues + 1
            Case "Trend"
                numberOfTrendIssues = numberOfTrendIssues + 1
        End Select
    Next i

    'we can use with instead of activating a sheet and selecting a cell
    'this also specifies in which sheet and workbook the cell is
    With ThisWorkbook.Worksheets("Inventory").Cells(2, 2)
        .Value = numberOfMacroIssues
        .Offset(1).Value = numberOfReportIssues
        .Offset(2).Value = numberOfTechnicalIssues
        .Offset(3).Value = numberOfTrendIssues
    End With
End Sub

而不是遍历你的行 For i = 2 To numberOfIssues + 1 你可能会数 Macro, Report, 等等

With WsQueryReg
    numberOfMacroIssues = Application.WorksheetFunction.CounfIf(.Range(.Cells(2, 7), .Cells(numberOfIssues + 1, 7)), "Macro")
    numberOfReportIssues  = Application.WorksheetFunction.CounfIf(.Range(.Cells(2, 7), .Cells(numberOfIssues + 1, 7)), "Report")
    '… others here
End With