Excel 检查单元格是否为空
我正在做一个宏来检查单元格是空的还是满的。但是,有没有快速的方法来检查三个单元格中是否只有一个不是空的 我的代码:Excel 检查单元格是否为空,excel,excel-2010,vba,Excel,Excel 2010,Vba,我正在做一个宏来检查单元格是空的还是满的。但是,有没有快速的方法来检查三个单元格中是否只有一个不是空的 我的代码: LastRow = Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).row ThisWorkbook.Sheets(1).Range("A1").Select Do Until ActiveCell.row = LastRow + 1 If IsEmpty(Activ
LastRow = Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).row
ThisWorkbook.Sheets(1).Range("A1").Select
Do Until ActiveCell.row = LastRow + 1
If IsEmpty(ActiveCell) = False Then
If IsEmpty(Cells(ActiveCell.row, 1)) = False And IsEmpty(Cells(ActiveCell.row, 1)) = False And IsEmpty(Cells(ActiveCell.row, 3))=False And IsEmpty(Cells(ActiveCell.row, 4))=False Then
MsgBox "None empty empty"
ElseIf IsEmpty(Cells(ActiveCell.row, 1)) = True And IsEmpty(Cells(ActiveCell.row, 2)) = True And IsEmpty(Cells(ActiveCell.row, 3)) = True And IsEmpty(Cells(ActiveCell.row, 4)) = True Then
MsgBox "All empty"
End If
End If
ActiveCell.Offset(1, 0).Select
Loop
但是,有没有办法检查四个单元格中是否只有一个、两个或三个不是空的
我在找你。在我的代码中,我希望它检查以下内容:
如果IsEmpty(Cells(ActiveCell.row,1))=False且IsEmpty(Cells(ActiveCell.row,1))=False且IsEmpty(Cells(ActiveCell.row,3))=True且IsEmpty(Cells(ActiveCell.row,4))=True,则MsgBox“2 empty”
因此,如果2为空,2为非空,则应始终检查它。我不想写太多if语句,这就是为什么我要问是否有更快的方法-对于一组特定的单元格,A1到D1 单向:
Sub EmptyCounter()
Dim rng As Range
Dim wf As WorksheetFunction
Set wf = Application.WorksheetFunction
Set rng = Range("A1:D1")
MsgBox "There are " & 4 - wf.CountA(rng) & " empties"
End Sub
这里我们明确忽略空字符串的情况。对于一组特定的单元格,A1到D1 单向:
Sub EmptyCounter()
Dim rng As Range
Dim wf As WorksheetFunction
Set wf = Application.WorksheetFunction
Set rng = Range("A1:D1")
MsgBox "There are " & 4 - wf.CountA(rng) & " empties"
End Sub
这里我们明确忽略空字符串的情况。对于一组特定的单元格,A1到D1 单向:
Sub EmptyCounter()
Dim rng As Range
Dim wf As WorksheetFunction
Set wf = Application.WorksheetFunction
Set rng = Range("A1:D1")
MsgBox "There are " & 4 - wf.CountA(rng) & " empties"
End Sub
这里我们明确忽略空字符串的情况。对于一组特定的单元格,A1到D1 单向:
Sub EmptyCounter()
Dim rng As Range
Dim wf As WorksheetFunction
Set wf = Application.WorksheetFunction
Set rng = Range("A1:D1")
MsgBox "There are " & 4 - wf.CountA(rng) & " empties"
End Sub
这里我们明确忽略空字符串的情况。根据您的示例代码,您的目标是确定何时:
Rem Code showing messages
Sub Wsh_MarkCellsEmptyAndNotEmpty_Msg()
Dim RngTrg As Range
Dim lRowLast As Long
Dim vCellsValue As Variant
Dim lRow As Long
Dim bNoneEmpty As Byte
Dim b As Byte
Rem No changes to your method for finding last row
lRowLast = Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
Rem Set Target Range
Set RngTrg = ThisWorkbook.Sheets(1).Range(Cells(1), Cells(lRowLast, 4))
For lRow = 1 To lRowLast
With RngTrg.Rows(lRow)
Rem To Select cells [NOT RECOMMENDED PRACTICE]
Rem Instead suggest to marked cells found
.Select
Rem Initiate Variables
bNoneEmpty = 0
vCellsValue = Empty
Rem Look into cells values
For b = 1 To 4
If .Cells(b).Value <> Empty Then bNoneEmpty = 1 + bNoneEmpty
vCellsValue = vCellsValue & .Cells(b).Value2
Next
Rem Show Message with Results
If vCellsValue = Empty Then
MsgBox "All Cells are empty"
ElseIf bNoneEmpty = 4 Then
MsgBox "None Cell is empty"
End If
End With: Next
End Sub
Rem Code marking cells with color (user friendly)
Sub Wsh_MarkCellsEmptyAndNotEmpty_Color()
Dim RngTrg As Range
Dim lRowLast As Long
Dim vCellsValue As Variant
Dim lRow As Long
Dim bNoneEmpty As Byte
Dim b As Byte
Rem No changes to your method for finding last row
lRowLast = Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
Rem Set Target Range
Set RngTrg = ThisWorkbook.Sheets(1).Range(Cells(1), Cells(lRowLast, 4))
Rem To Clear Cells Colors if marking with colors cells found
RngTrg.Interior.Pattern = xlNone
For lRow = 1 To lRowLast
With RngTrg.Rows(lRow)
Rem Initiate Variables
bNoneEmpty = 0
vCellsValue = Empty
Rem Look into cells values
For b = 1 To 4
If .Cells(b).Value <> Empty Then bNoneEmpty = 1 + bNoneEmpty
vCellsValue = vCellsValue & .Cells(b).Value2
Next
Rem Mark Resulting cells
If vCellsValue = Empty Then
Rem Colors Empty Cells in Red
.Interior.Color = RGB(255, 199, 206)
ElseIf bNoneEmpty = 4 Then
Rem Colors No Empty Cells in Green
.Interior.Color = RGB(198, 239, 206)
End If
End With: Next
End Sub
Rem代码显示消息
子Wsh_MarkCellsEmptyAndNotEmpty_Msg()
变暗RngTrg As范围
黯淡如水
变暗vCells值作为变量
暗淡的光线和长的一样
Dim bNoneEmpty作为字节
作为字节的Dim b
Rem不更改查找最后一行的方法
lRowLast=单元格。查找(“*”,搜索顺序:=xlByRows,搜索方向:=xlPrevious)。行
Rem设定目标距离
设置RngTrg=ThisWorkbook.Sheets(1).范围(单元格(1),单元格(lRowLast,4))
对于lRow=1到lRowLast
带RngTrg.行(lRow)
Rem选择单元格[不推荐的做法]
相反,Rem建议找到标记的细胞
.选择
Rem启动变量
bOneEmpty=0
vCellsValue=空
Rem查看单元格值
对于b=1到4
如果.Cells(b).Value为空,则bNoneEmpty=1+bNoneEmpty
vCellsValue=vCellsValue和单元格(b).Value2
下一个
Rem显示带有结果的消息
如果vCellsValue=空,则
MsgBox“所有单元格均为空”
ElseIf bNoneEmpty=4则
MsgBox“无单元格为空”
如果结束
以:下一个结束
端接头
Rem代码用颜色标记单元格(用户友好型)
Sub-Wsh_MarkCellsEmptyAndNotEmpty_Color()
变暗RngTrg As范围
黯淡如水
变暗vCells值作为变量
暗淡的光线和长的一样
Dim bNoneEmpty作为字节
作为字节的Dim b
Rem不更改查找最后一行的方法
lRowLast=单元格。查找(“*”,搜索顺序:=xlByRows,搜索方向:=xlPrevious)。行
Rem设定目标距离
设置RngTrg=ThisWorkbook.Sheets(1).范围(单元格(1),单元格(lRowLast,4))
Rem清除单元格颜色(如果找到带有颜色的单元格标记)
RngTrg.Interior.Pattern=xlNone
对于lRow=1到lRowLast
带RngTrg.行(lRow)
Rem启动变量
bOneEmpty=0
vCellsValue=空
Rem查看单元格值
对于b=1到4
如果.Cells(b).Value为空,则bNoneEmpty=1+bNoneEmpty
vCellsValue=vCellsValue和单元格(b).Value2
下一个
Rem标记结果细胞
如果vCellsValue=空,则
Rem将空细胞染成红色
.Interior.Color=RGB(255、199、206)
ElseIf bNoneEmpty=4则
Rem将无空单元格显示为绿色
.Interior.Color=RGB(198239206)
如果结束
以:下一个结束
端接头
根据您的示例代码,您的目标是确定何时:
Rem Code showing messages
Sub Wsh_MarkCellsEmptyAndNotEmpty_Msg()
Dim RngTrg As Range
Dim lRowLast As Long
Dim vCellsValue As Variant
Dim lRow As Long
Dim bNoneEmpty As Byte
Dim b As Byte
Rem No changes to your method for finding last row
lRowLast = Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
Rem Set Target Range
Set RngTrg = ThisWorkbook.Sheets(1).Range(Cells(1), Cells(lRowLast, 4))
For lRow = 1 To lRowLast
With RngTrg.Rows(lRow)
Rem To Select cells [NOT RECOMMENDED PRACTICE]
Rem Instead suggest to marked cells found
.Select
Rem Initiate Variables
bNoneEmpty = 0
vCellsValue = Empty
Rem Look into cells values
For b = 1 To 4
If .Cells(b).Value <> Empty Then bNoneEmpty = 1 + bNoneEmpty
vCellsValue = vCellsValue & .Cells(b).Value2
Next
Rem Show Message with Results
If vCellsValue = Empty Then
MsgBox "All Cells are empty"
ElseIf bNoneEmpty = 4 Then
MsgBox "None Cell is empty"
End If
End With: Next
End Sub
Rem Code marking cells with color (user friendly)
Sub Wsh_MarkCellsEmptyAndNotEmpty_Color()
Dim RngTrg As Range
Dim lRowLast As Long
Dim vCellsValue As Variant
Dim lRow As Long
Dim bNoneEmpty As Byte
Dim b As Byte
Rem No changes to your method for finding last row
lRowLast = Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
Rem Set Target Range
Set RngTrg = ThisWorkbook.Sheets(1).Range(Cells(1), Cells(lRowLast, 4))
Rem To Clear Cells Colors if marking with colors cells found
RngTrg.Interior.Pattern = xlNone
For lRow = 1 To lRowLast
With RngTrg.Rows(lRow)
Rem Initiate Variables
bNoneEmpty = 0
vCellsValue = Empty
Rem Look into cells values
For b = 1 To 4
If .Cells(b).Value <> Empty Then bNoneEmpty = 1 + bNoneEmpty
vCellsValue = vCellsValue & .Cells(b).Value2
Next
Rem Mark Resulting cells
If vCellsValue = Empty Then
Rem Colors Empty Cells in Red
.Interior.Color = RGB(255, 199, 206)
ElseIf bNoneEmpty = 4 Then
Rem Colors No Empty Cells in Green
.Interior.Color = RGB(198, 239, 206)
End If
End With: Next
End Sub
Rem代码显示消息
子Wsh_MarkCellsEmptyAndNotEmpty_Msg()
变暗RngTrg As范围
黯淡如水
变暗vCells值作为变量
暗淡的光线和长的一样
Dim bNoneEmpty作为字节
作为字节的Dim b
Rem不更改查找最后一行的方法
lRowLast=单元格。查找(“*”,搜索顺序:=xlByRows,搜索方向:=xlPrevious)。行
Rem设定目标距离
设置RngTrg=ThisWorkbook.Sheets(1).范围(单元格(1),单元格(lRowLast,4))
对于lRow=1到lRowLast
带RngTrg.行(lRow)
Rem选择单元格[不推荐的做法]
相反,Rem建议找到标记的细胞
.选择
Rem启动变量
bOneEmpty=0
vCellsValue=空
Rem查看单元格值
对于b=1到4
如果.Cells(b).Value为空,则bNoneEmpty=1+bNoneEmpty
vCellsValue=vCellsValue和单元格(b).Value2
下一个
Rem显示带有结果的消息
如果vCellsVal