Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/vba/16.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/csharp-4.0/2.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
Excel 如何使用具有多个范围标准的if函数_Excel_Vba - Fatal编程技术网

Excel 如何使用具有多个范围标准的if函数

Excel 如何使用具有多个范围标准的if函数,excel,vba,Excel,Vba,我运行宏将数据从A5复制到F5单元格,从ABC工作表复制到XYZ工作表, 但是如果从A5到F5的任何单元格为空,我需要获取msgbox来更新所有字段中的数据 我使用了以下代码,但出现运行时错误: Sub Main() If Range("A5,B5,C5,D5,E5,F5") = Empty Then MsgBox "Update data in all fields" Else MsgBox "Data is appropr

我运行宏将数据从
A5
复制到
F5
单元格,从
ABC
工作表复制到
XYZ
工作表, 但是如果从
A5
F5
的任何单元格为空,我需要获取
msgbox
来更新所有字段中的数据

我使用了以下代码,但出现运行时错误:

Sub Main()

If Range("A5,B5,C5,D5,E5,F5") = Empty Then
    MsgBox "Update data in all fields"
Else 
    MsgBox "Data is appropriate"
End If

End Sub

有人能推荐我吗?

这是我会做的,我不是专家,也许是一种简单或优雅的方式。 这是用于列检查的

Sub check()
    Dim i As Integer
    Dim lastcolumn As Integer
    lastcolumn = Worksheets("Excel sheet name").Cells(1, Worksheets("Excel sheet name").columns.Count).End(xlToLeft).Column
    For i = 1 To lastcolumn ' A to F
        If Worksheets("Excel sheet name").Cells(5, i).Value = "" Then
           MsgBox "Update data field: " & i
        Else
           MsgBox "Data is appropriate"
        End If
    Next i
End Sub
这是行检查用的

Sub check()
    Dim i As Integer
    Dim lastrow As Long
    lastrow = Worksheets("Excel sheet name").Cells(Worksheets("Excel sheet name").Rows.Count, "A").End(xlUp).Row
    For i = 2 To lastrow
        If Worksheets("Excel sheet name").Cells(i, 1).Value = "" Then
           MsgBox "Update data field: " & i
        Else
           MsgBox "Data is appropriate"
        End If
    Next i
End Sub
你说什么

For i = 1 To 6  'check         
    If Worksheets("YOUR EXCEL SHEET NAME").Cells(5, i).Value = "" Then                                                        
       MsgBox "Update data field: " & i         
    Else            
       'your code       
    End If     
Next i

若要检查空单元格,您必须在单元格中进行太多的循环。使用
Exit For
可以在需要时停止循环:

Dim c
For Each c In Array("A5", "B5", "C5", "D5", "E5", "F5")
   If Range(c) = Empty Then
    MsgBox "One of the cells is Empty"
    Exit For
   End If
Next

您可以使用命名范围和
CountA
功能:

Set myRange = Range("A5,B5,C5,D5,E5,F5")
If Application.CountA(myRange) < myRange.Count Then
    MsgBox "Update data in all fields"
Else
    MsgBox "Data is appropriate"
End If
Set myRange=范围(“A5、B5、C5、D5、E5、F5”)
如果Application.CountA(myRange)
每次尝试仅显示一次消息框

参考:


Getting error on last row=工作表行,即使我做了更改,msgbox也会弹出6到7次Sub Main(),如果范围(“A5、B5、C5、D5、E5、F5”)=空,则msgbox“更新所有字段中的数据”否则msgbox“数据合适”如果End Subi不想检查任何行或列,仅检查i=1到6'A到F的上述单元格
,如果工作表(“您的Excel工作表名称”)。单元格(5,i)。Value=“”,然后MsgBox“更新数据字段:”&i Else MsgBox“数据是适当的”如果下一个i
否,则结束它根本不工作,这只是让msgbox弹出7次非常感谢,但还有一件事我需要知道。上面的vba表示,如果单元格为空,那么我需要else调用另一个宏。您需要一个布尔变量,例如,
isEmpty
在代码顶部设置为false,在if块中设置为true。然后,您可以在代码末尾检查它是真的(范围中有一个空单元格)还是假的(范围已填充)。但请继续使用JMP解决方案,这样应该会更快。最好使用引用工作表和