使用数组VBA计算多列中的空白单元格

时间:2017-10-07 16:10:44

标签: excel vba excel-vba

我编写了一个代码,它给出了列/ s中空/空单元格的精确计数。

Image

如果我运行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

但问题如下: -

  1. 如果没有空格,则此宏将抛出错误并自行终止。如果我想运行剩余的代码怎么办?
  2. 使用数组或类似的东西我想同时按标题搜索多个列,而不是代码中显示的列号。
  3. 如果找到空白/ s,它会弹出一个Msgbox,但是我们可以在名为“error_sheet”的单独新工作表中获取错误列表吗?

4 个答案:

答案 0 :(得分:2)

function getvalues(){
    var select1 = document.getElementById('select1').value;

    var datastring = 'select1='+select1;

    $.ajax({

        type:"POST",
        url:"gettypid.php",
        data:datastring,
        dataType: 'Text',
        cache:false,
        success:function(html){
            $('#typid').val(html);
            getvalues();
        }

    });
    return false;

}

答案 1 :(得分:1)

试试这个

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

答案 2 :(得分:0)

尝试使用 MAIN 来检查前三列:

 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

答案 3 :(得分:0)

  1. 可以使用错误处理语句来回答Q1。错误处理语句可以像人们希望的那样简单或复杂。下面的一个可能是我的第一个方法。
  2. 
    
    ' if no blank cells found, code continues
            On Error Resume Next
            MsgBox ("There are " & r.SpecialCells(xlCellTypeBlanks).Count & _
                " Rows with blank cells  in column B")
    
    
    

    1. 使用标题可以正常工作。请参阅下面的最终答案。

    2. 此答案与Imran Malek提交的答案略有不同

    3. 
      
      Sub countblank()
      
          Dim i As Long
          ' new integer "row" declared
          Dim row As Integer
          
          ' new integer "row" set
          row = 1
      
          For i = 2 To 4    ' 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)
              
              ' using the value in row to insert our output
              Sheets("error_sheet").Range("A" & row).Value = "There are " & Application.WorksheetFunction.countblank(r) & " Rows with blank cells in column" & r.Column
              ' adding 1 to "row" to prep for next output
              row = row + 1
          Next i
      End Sub
      
      
      

      最后的回答:我为这个冗长的答案道歉。这个答案是对Imran Malek答案的修改,在答案3的链接中找到。请注意,此版本不包含错误处理,在Q1中解释。

      
      
      Sub countblank()
      
          Dim Header(1 To 4) As String
              Header(1) = "Name"
              Header(2) = "Age"
              Header(3) = "Salary"
              Header(4) = "Test"
          
              
          Dim i As Integer
          Dim row As Integer
          Dim r As Range
          Dim c As Integer
      
          row = 1
      
          ' **NOTE** if you add any more values to {Header}, the loop has to be equal to the Header count
          ' i.e. 4 {Headers}, 4 in the loop
          For i = 1 To 4
          
              'looking for the header in row 1
              c = Cells(1, 1).EntireRow.Find(What:=Header(i), LookIn:=xlValues).Column
              
              'defining the column after header is found
              Set r = Range(Cells(2, c), Cells(Rows.Count, c).End(xlUp))
              
              ' using the value in row to insert our output
              Sheets("error_sheet").Range("A" & row).Value = "There are " & Application.WorksheetFunction.countblank(r) & " Rows with blank cells in column" & r.Column
              ' adding 1 to "row" to prep for next output
              row = row + 1
              
              
          Next i
      End Sub