使用数组VBA对多列中的空白单元格进行计数
我已经编写了一个代码,可以精确计算一列中的空/空白单元格数 这显示了运行列A的代码时的结果使用数组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 "
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
但问题如下:-
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
”如果未找到空白单元格,代码将继续
出错时继续下一步
MsgBox(“有”和r.SpecialCells(xlCellTypeBlanks)。计数和_
“B列中有空白单元格的行”)
”如果未找到空白单元格,代码将继续
出错时继续下一步
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中