Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用数组VBA对多列中的空白单元格进行计数_Vba_Excel - Fatal编程技术网

使用数组VBA对多列中的空白单元格进行计数

使用数组VBA对多列中的空白单元格进行计数,vba,excel,Vba,Excel,我已经编写了一个代码,可以精确计算一列中的空/空白单元格数 这显示了运行列A的代码时的结果 Sub countblank() Const column_to_test = 2 'column (B) Dim r As Range Set r = Range(Cells(2, column_to_test), Cells(Rows.Count, column_to_test).End(xlUp)) MsgBox ("There are "

我已经编写了一个代码,可以精确计算一列中的空/空白单元格数

这显示了运行列A的代码时的结果

  Sub countblank()

    Const column_to_test = 2    'column (B)
    Dim r As Range
    Set r = Range(Cells(2, column_to_test), Cells(Rows.Count, 
    column_to_test).End(xlUp))
     MsgBox ("There are " & r.SpecialCells(xlCellTypeBlanks).Count & " Rows 
     with blank cells in column B")

    Const columns_to_test = 3    'column (C)
    Set r = Range(Cells(3, columns_to_test), Cells(Rows.Count, 
    columns_to_test).End(xlUp))
    MsgBox ("There are " & r.SpecialCells(xlCellTypeBlanks).Count & " Rows 
    with blank cells  in column c ")

    'and so on i can count the blanks for as many columns i want

    End Sub
但问题如下:-

  • 如果没有空格,此宏将抛出错误并自行终止。如果我想运行剩下的代码呢
  • 使用数组或类似的东西,我想同时按标题搜索多个列,而不是像代码中所示的那样单独搜索列号
  • 如果发现一个空白的/s,它会弹出一个Msgbox,但是我们能在一个单独的新表“错误表”中得到错误列表吗
  • 试试这个

    Sub countblank()
    
        Dim i As Long
    
        For i = 2 To 10    ' for looping through the columns
            Dim r As Range
            Set r = Range(Cells(2, i), Cells(Rows.Count, i).End(xlUp))
            'for not getting error and adding error messages in the error_sheet
            'MsgBox ("There are " & Application.WorksheetFunction.countblank(r) & " Rows with blank cells in column" & r.Column)
            Sheets("error_sheet").Range(r.Address).Value = "There are " & Application.WorksheetFunction.countblank(r) & " Rows with blank cells in column" & r.Column
        Next i
    End Sub
    
    试试这个

    Sub countblank()
    
        Dim i As Long
    
        For i = 2 To 10    ' for looping through the columns
            Dim r As Range
            Set r = Range(Cells(2, i), Cells(Rows.Count, i).End(xlUp))
            'for not getting error and adding error messages in the error_sheet
            'MsgBox ("There are " & Application.WorksheetFunction.countblank(r) & " Rows with blank cells in column" & r.Column)
            Sheets("error_sheet").Range(r.Address).Value = "There are " & Application.WorksheetFunction.countblank(r) & " Rows with blank cells in column" & r.Column
        Next i
    End Sub
    
    尝试subMAIN检查前三列:

     Sub countblank(column_to_test As Long)
    
        Dim r As Range, rr As Range, col As String
        col = Split(Cells(1, column_to_test).Address, "$")(1)
    
        Set r = Range(Cells(2, column_to_test), Cells(Rows.Count, column_to_test).End(xlUp))
        On Error Resume Next
            Set rr = r.SpecialCells(xlCellTypeBlanks)
        On Error GoTo 0
        If rr Is Nothing Then
            MsgBox ("There are no Rows with blank cells in column " & col)
        Else
            MsgBox ("There are " & r.SpecialCells(xlCellTypeBlanks).Count & " Rows with blank cells in column " & col)
        End If
        End Sub
    
    Sub MAIN()
        Dim i As Long
    
        For i = 1 To 3
            Call countblank(i)
        Next i
    End Sub
    
    尝试subMAIN检查前三列:

     Sub countblank(column_to_test As Long)
    
        Dim r As Range, rr As Range, col As String
        col = Split(Cells(1, column_to_test).Address, "$")(1)
    
        Set r = Range(Cells(2, column_to_test), Cells(Rows.Count, column_to_test).End(xlUp))
        On Error Resume Next
            Set rr = r.SpecialCells(xlCellTypeBlanks)
        On Error GoTo 0
        If rr Is Nothing Then
            MsgBox ("There are no Rows with blank cells in column " & col)
        Else
            MsgBox ("There are " & r.SpecialCells(xlCellTypeBlanks).Count & " Rows with blank cells in column " & col)
        End If
        End Sub
    
    Sub MAIN()
        Dim i As Long
    
        For i = 1 To 3
            Call countblank(i)
        Next i
    End Sub
    
  • 可以使用错误处理语句来回答Q1。错误处理语句可以是简单的,也可以是复杂的。下面的一个可能是我第一次使用的方法
  • ”如果未找到空白单元格,代码将继续
    出错时继续下一步
    MsgBox(“有”和r.SpecialCells(xlCellTypeBlanks)。计数和_
    “B列中有空白单元格的行”)
  • 可以使用错误处理语句来回答Q1。错误处理语句可以是简单的,也可以是复杂的。下面的一个可能是我第一次使用的方法
  • ”如果未找到空白单元格,代码将继续
    出错时继续下一步
    MsgBox(“有”和r.SpecialCells(xlCellTypeBlanks)。计数和_
    
    “B列中有空白单元格的行”)
    谢谢!@Imran Malek的工作非常出色,但错误表如下所示:-在错误表的“A”列中,“第1列中有0行空白单元格”是表1的“A”列中有15行的15倍。在错误工作表的“B”列中,“第2列中有2行有空白单元格”是工作表1的“A”列中有15行的15倍。我们可以为每列打印一行吗。还有一件事,列的位置是未知的,难道我们不能通过它们各自的标题来搜索列吗?仅供参考,数据保存在Sheet1Tanks中!@Imran Malek的工作非常出色,但错误表如下所示:-在错误表的“A”列中,“第1列中有0行空白单元格”是表1的“A”列中有15行的15倍。在错误工作表的“B”列中,“第2列中有2行有空白单元格”是工作表1的“A”列中有15行的15倍。我们可以为每列打印一行吗。还有一件事,列的位置是未知的,难道我们不能通过它们各自的标题来搜索列吗?供参考的数据保存在Sheet1中