简体   繁体   English

我对这个 Excel vba 做错了什么

[英]What am I doing wrong with this Excel vba

I'm attempting to check and see if each column is empty or not, but it's not working.我正在尝试检查每列是否为空,但它不起作用。 What am I doing wrong?我究竟做错了什么? I need it for more calculations.我需要它进行更多计算。 If I could get some help that would be great.如果我能得到一些帮助,那就太好了。 I need to do it for nine of the columns f through n and for each I need that value as a Boolean afterwards which is why I made it the way I did I made it based on a tutorial.我需要对 f 到 n 中的九个列执行此操作,并且之后我需要将该值作为布尔值,这就是为什么我按照我根据教程制作的方式制作它的原因。 I'm trying to get a message that tells me if a column is empty我正在尝试收到一条消息,告诉我列是否为空

Sub emptysinder()
Dim cell As Range
Dim fIsEmpty As Boolean
Dim gIsEmpty As Boolean
Dim hIsEmpty As Boolean
Dim iIsEmpty As Boolean
Dim jIsEmpty As Boolean
Dim kIsEmpty As Boolean
Dim lIsEmpty As Boolean
Dim mIsEmpty As Boolean
Dim nIsEmpty As Boolean
fIsEmpty = False
gIsEmpty = False
hIsEmpty = False
iIsEmpty = False
jIsEmpty = False
kIsEmpty = False
lIsEmpty = False
mIsEmpty = False
nIsEmpty = False
For Each cell In Range("F1:F200")
    If IsEmpty(cell) = True Then
        fIsEmpty = True
        Exit For
    End If
Next cell
If fIsEmpty = True Then
    MsgBox "All cells are empty in your range!"
Else
    MsgBox "Cells!"
End If
For Each cell In Range("G1:G200")
    If IsEmpty(cell) = True Then
        gIsEmpty = True
        Exit For
    End If
Next cell
If gIsEmpty = True Then
    MsgBox "All cells are empty in your range!"
Else
    MsgBox "Cells "
End If
For Each cell In Range("H1:H200")
    If IsEmpty(cell) = True Then
        hIsEmpty = True
        Exit For
    End If
Next cell
If hIsEmpty = True Then
    MsgBox "All cells are empty in your range!"
Else
    MsgBox "Cells have values!"
End If
For Each cell In Range("I1:I200")
    If IsEmpty(cell) = True Then
        iIsEmpty = True
        Exit For
    End If
Next cell
If iIsEmpty = True Then
    MsgBox "All cells are empty in your range!"
Else
    MsgBox "Cells have values!"
End If
For Each cell In Range("J1:J200")
    If IsEmpty(cell) = True Then
        jIsEmpty = True
        Exit For
    End If
Next cell
If jIsEmpty = True Then
    MsgBox "All cells are empty in your range!"
Else
    MsgBox "Cells have values!"
End If
For Each cell In Range("K1:K200")
    If IsEmpty(cell) = True Then
        kIsEmpty = True
        Exit For
    End If
Next cell
If kIsEmpty = True Then
    MsgBox "All cells are empty in your range!"
Else
    MsgBox "Cells have values!"
End If
For Each cell In Range("L1:L200")
    If IsEmpty(cell) = True Then
        lIsEmpty = True
        Exit For
    End If
Next cell
If lIsEmpty = True Then
    MsgBox "All cells are empty in your range!"
Else
    MsgBox "Cells have values!"
End If
For Each cell In Range("M1:M200")
    If IsEmpty(cell) = True Then
        mIsEmpty = True
        Exit For
    End If
Next cell
If mIsEmpty = True Then
    MsgBox "All cells are empty in your range!"
Else
    MsgBox "Cells have values!"
End If
For Each cell In Range("N1:N200")
    If IsEmpty(cell) = True Then
        nIsEmpty = True
        Exit For
    End If
Next cell
If nIsEmpty = True Then
    MsgBox "All cells are empty in your range!"
Else
    MsgBox "Cells have values!"
End If
End Sub

Essentially you are checking nine columns for rows 1 through 200 if they are empty or not.本质上,您正在检查第 1 行到第 200 行的九列是否为空。 Using a loop you can call some custom function (see below) to make the checks.使用循环,您可以调用一些自定义函数(见下文)进行检查。

Option Explicit

Public Sub emptysinder()

    Dim i As Long
    Dim r As Range
    
    For i = 1 To 9
        Set r = Range("F1").Offset(0, i - 1).Resize(200, 1)
        If IsAllEmpty(r) Then
            Debug.Print "Range " & r.Address & " is all empty."
        ElseIf IsAnyEmpty(r) Then
            Debug.Print "Range " & r.Address & " is partially empty."
        Else
            Debug.Print "Range " & r.Address & " filled."
        End If
    Next i
    

End Sub

And the sample output on the immediate window以及即时窗口上的示例输出

Range $F$1:$F$200 is all empty.
Range $G$1:$G$200 filled.
Range $H$1:$H$200 is all empty.
Range $I$1:$I$200 is partially empty.
Range $J$1:$J$200 is all empty.
Range $K$1:$K$200 is partially empty.
Range $L$1:$L$200 is all empty.
Range $M$1:$M$200 is all empty.
Range $N$1:$N$200 is all empty.

With the following helper functions in a module在模块中使用以下辅助函数


Public Function IsAllEmpty(ByVal r_range As Range) As Boolean
    Dim Item As Range
    For Each Item In r_range,Cells
        If Not IsEmpty(Item) Then
            IsAllEmpty = False
            Exit Function
        End If
    Next
    IsAllEmpty = True
End Function

Public Function IsAnyEmpty(ByVal r_range As Range) As Boolean
    Dim Item As Range
    For Each Item In r_range.Cells
        If IsEmpty(Item) Then
            IsAnyEmpty = True
            Exit Function
        End If
    Next
    IsAnyEmpty = False
End Function

Check Columns If Empty (or Blank)检查列是否为空(或空白)

Tips提示

  • Whenever you need to create a ton of variables holding the same data type, you should consider using a data structure eg an array, a collection, a dictionary... etc. Then you can loop through the elements of the data structure and/or easily access individual elements.每当您需要创建大量具有相同数据类型的变量时,您应该考虑使用数据结构,例如数组、集合、字典……等。然后您可以遍历数据结构的元素和/或轻松访问各个元素。

  • When you do Dim cell As Range it actually means当你做Dim cell As Range它实际上意味着

    Dim cell As Range: Set cell = Nothing

    Similarly, when you do Dim fIsEmpty As Boolean , it actually means同样,当您执行Dim fIsEmpty As Boolean时,它实际上意味着

    Dim fIsEmpty As Boolean: fIsEmpty = False

    When declaring a variable, it is always assigned a default value like in both for-mentioned cases.在声明一个变量时,它总是被赋予一个默认值,就像在上述两种情况下一样。 In the first case, it is Nothing , while in the second case it is False ie doing fIsEmpty = False is not necessary.在第一种情况下,它是Nothing ,而在第二种情况下是False ,即不需要执行fIsEmpty = False If it helps with your understanding of the code, you can keep doing it, there's no harm done except that the code is longer.如果它有助于您理解代码,您可以继续这样做,除了代码更长之外没有任何坏处。

  • When looping through the cells of a range, you should consider doing it in this way:当循环一个范围的单元格时,你应该考虑这样做:

     For Each cell In Range("G1:G200").Cells

    Note the .Cells .注意.Cells By doing this, the code will become more readable and you'll avoid errors occurring when VBA 'might decide' that it is .Rows or .Columns (the latter is used in the following codes) or whatnot.通过这样做,代码将变得更具可读性,并且您将避免在 VBA“可能决定”它是.Rows.Columns (后者在以下代码中使用)或诸如此类时发生错误。

  • Instead of If IsEmpty(cell) = True Then you can simply do:而不是If IsEmpty(cell) = True Then你可以简单地做:

     If IsEmpty(cell) Then

    Again, If it helps with your understanding of the code, you can keep doing it, there's no harm done except that the code is longer.同样,如果它有助于您理解代码,您可以继续这样做,除了代码更长之外没有任何害处。

Empty空的

  • You can efficiently check if all the cells in a range are empty by using Application.CountA in the following way:您可以通过以下方式使用Application.CountA有效地检查范围内的所有单元格是否为空:

     If Application.CountA(rg) = 0 Then ' all cells are empty
Sub EmptiesInDer()
    
    ' Reference the worksheet ('ws').
    Dim ws As Worksheet: Set ws = ActiveSheet ' improve!
    
    ' Reference the range ('rg').
    Dim rg As Range: Set rg = ws.Range("F1:N200")
    
    ' Write the number of columns to a variable ('cCount').
    Dim cCount As Long: cCount = rg.Columns.Count
    
    ' Define a 2D one-based two-row array: the first row will hold
    ' the addresses, while the second row will hold the booleans.
    Dim cArr() As Variant: ReDim cArr(1 To 2, 1 To cCount)
    
    ' Declare additional variables.
    Dim crg As Range ' Current Column Range
    Dim c As Long ' Current Array Column
    
    ' Loop through each column of the range.
    For Each crg In rg.Columns
        c = c + 1 ' next array column
        ' Write the column range address to the first array row.
        cArr(1, c) = crg.Address(0, 0)
        ' Write the boolean (all cells are empty) to the second array row.
        cArr(2, c) = IIf(Application.CountA(crg) = 0, True, False)
    Next crg
    
    ' Access the array values.
    
    ' Print the results from the array to the Immediate window (Ctrl+G).
    Debug.Print "Empty Columns (Common)"
    Debug.Print "Column", "Address", "Is Empty"
    For c = 1 To cCount
        Debug.Print c, cArr(1, c), cArr(2, c)
    Next c
    
    ' Print the fancy results from the array to the Immediate window (Ctrl+G).
    Debug.Print "Empty Columns (Fancy)"
    For c = 1 To cCount
        Debug.Print c & ". The column range '" & cArr(1, c) & "' is " _
            & IIf(cArr(2, c), "", "not ") & "empty."
    Next c
    
End Sub

Blank空白的

  • A cell is considered blank if it is either empty, it contains the formula ="" , it contains a single quote ' or whatnot (can't think of anything else).如果一个单元格是空的,它包含公式="" ,它包含一个单引号'或诸如此类的东西(想不出其他任何东西),则它被认为是空白的。

  • You can efficiently check if all the cells in a range are blank by using Application.CountBlank in the following ways:您可以通过以下方式使用Application.CountBlank有效地检查区域中的所有单元格是否为空白:

     If Application.CountBlank(rg) = rg.Rows.Count Then ' all cells in a column range are blank If Application.CountBlank(rg) = rg.Columns.Count Then ' all cells in a row range are blank If Application.CountBlank(rg) = rg.Cells.Count Then ' all cells in a range are blank ' ***
  • *** There is a limit for .Cells.Count so you may consider using rg.CountLarge *** .Cells.Count有一个限制,因此您可以考虑使用rg.CountLarge
    or rg.Rows.Count * rg.Columns.Count for a range.rg.Rows.Count * rg.Columns.Count用于范围。

  • In this particular case, the first option is used and the value of rg.Rows.Count , the number of rows, is written to a variable ( rCount ).在这种特殊情况下,使用第一个选项并将rg.Rows.Count的值(行数)写入变量 ( rCount )。

Sub BlanksInDer()
    
    ' Reference the worksheet ('ws').
    Dim ws As Worksheet: Set ws = ActiveSheet ' improve!
    
    ' Reference the range ('rg').
    Dim rg As Range: Set rg = ws.Range("F1:N200")
    
    ' Write the number of rows to a variable ('rCount').
    Dim rCount As Long: rCount = rg.Rows.Count
    
    ' Write the number of columns to a variable ('cCount').
    Dim cCount As Long: cCount = rg.Columns.Count
    
    ' Define a 2D one-based two-row array: the first row will hold
    ' the addresses, while the second row will hold the booleans.
    Dim cArr() As Variant: ReDim cArr(1 To 2, 1 To cCount)
    
    ' Declare additional variables.
    Dim crg As Range ' Current Column Range
    Dim c As Long ' Current Array Column
    
    ' Loop through each column of the range.
    For Each crg In rg.Columns
        c = c + 1 ' next array column
        ' Write the column range address to the first array row.
        cArr(1, c) = crg.Address(0, 0)
        ' Write the boolean (all cells are blank) to the second array row.
        cArr(2, c) = IIf(Application.CountBlank(crg) = rCount, True, False)
    Next crg
    
    ' Access the array values.
    
    ' Print the results from the array to the Immediate window (Ctrl+G).
    Debug.Print "Blank Columns (Common)"
    Debug.Print "Column", "Address", "Is Blank"
    For c = 1 To cCount
        Debug.Print c, cArr(1, c), cArr(2, c)
    Next c
    
    ' Print the fancy results from the array to the Immediate window (Ctrl+G).
    Debug.Print "Blank Columns (Fancy)"
    For c = 1 To cCount
        Debug.Print c & ". The column range '" & cArr(1, c) & "' is " _
            & IIf(cArr(2, c), "", "not ") & "blank."
    Next c
    
End Sub

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我究竟做错了什么? 使用Excel VBA删除重复项 - What am I doing wrong? Removing duplicates using Excel VBA 在Excel VBA宏中创建一个新行(我做错了什么?) - Creating a new row in Excel VBA macro (what am I doing wrong?) 尝试并无法通过VBA在Excel中的列中循环。 我究竟做错了什么? - Trying and failing to loop through a column in Excel with VBA. What am I doing wrong? Excel VBA:在for循环中跳转:“ Next for For” –我在做什么错? - Excel VBA: Jump inside a for loop: “Next without For” – What am I doing wrong? Excel VBA Find函数未返回整数值,看不到我在做什么 - Excel VBA Find function not returning an integer value, can't see what I am doing wrong VBA创建数据透视表,类型不匹配? 我究竟做错了什么? - VBA to Create PivotTable, Type Mismatch? What Am I Doing Wrong? VBA将多列转换为两列-我在做什么错? - VBA Transform Many Columns to Two - What am I doing wrong? 我在excel行循环的拆分中做错了什么? - What am I doing wrong in this split for excel rows loop? 我正在使用VBA DateSerial函数分隔字符串中的日期,但是当字符串中的年份为'1000'时,Excel Ends Sub会出现。 我究竟做错了什么? - I am using the VBA DateSerial function to separate dates in a string, but Excel Ends Sub when year in string is '1000'. What am I doing wrong? Jar可执行文件我在做什么错? - Jar Executable what am I doing wrong?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM