我正在创建一个包含许多列的电子表格,我试图让它检查某一列中的单元格是否为空,然后它将检查同一行的其他列中的单元格是否为空。目前我知道的唯一方法是,如果您确切地说出您正在检查的单元格。
'Checking If the "Email Sent Columb cells are empty so it can be set to "No"
If IsEmpty(ThisWorkbook.Sheets(2).Range("F2")) Then
ThisWorkbook.Sheets(2).Range("F2") = "No"
If IsEmpty(ThisWorkbook.Sheets(2).Range("J2")) Then
ThisWorkbook.Sheets(2).Range("J2") = "No"
If IsEmpty(ThisWorkbook.Sheets(2).Range("M2")) Then
ThisWorkbook.Sheets(2).Range("M2") = "No"
End If
End If
End If
'If Acknowledge Email Sent = No then it will check if there are any fields that are empty if not then it will send the email and mark
'it as sent
If ThisWorkbook.Sheets(2).Range("F2") = "No" Then
If IsEmpty(ThisWorkbook.Sheets(2).Range("A2")) Then
MsgBox "A2 Empty"
Else
If IsEmpty(ThisWorkbook.Sheets(2).Range("B2")) Then
MsgBox "B2 Empty"
Else
If IsEmpty(ThisWorkbook.Sheets(2).Range("C2")) Then
MsgBox "C2 Empty"
Else
If IsEmpty(ThisWorkbook.Sheets(2).Range("D2")) Then
MsgBox "D2 Empty"
Else
If IsEmpty(ThisWorkbook.Sheets(2).Range("E2")) Then
MsgBox "E2 Empty"
Else
MsgBox "Acknowledge Email Ready To Be Sent"
ThisWorkbook.Sheets(2).Range("F2") = "Yes"
End If
End If
End If
End If
End If
End If
'If Update Email Sent = No then it will check if further evidence is required if it is then it will check if the evidence required is
'not empty, if it isnt empty then it will send the email and mark it as sent, but if further evidence required is set to no then it
'will mark it as unnecessary.
If ThisWorkbook.Sheets(2).Range("J2") = "No" Then
If ThisWorkbook.Sheets(2).Range("H2") = "Yes" Or ThisWorkbook.Sheets(2).Range("H2") = "" Then
If IsEmpty(ThisWorkbook.Sheets(2).Range("H2")) Then
MsgBox "H2 Is Empty"
Else
If IsEmpty(ThisWorkbook.Sheets(2).Range("I2")) Then
MsgBox "I2 Is Empty"
Else
MsgBox "Update Email Ready To Be Sent"
ThisWorkbook.Sheets(2).Range("J2") = "Yes"
End If
End If
Else
ThisWorkbook.Sheets(2).Range("J2") = "Unnecessary"
End If
End If
If ThisWorkbook.Sheets(2).Range("M2") = "No" Then
If ThisWorkbook.Sheets(2).Range("L2") = "" Then
MsgBox "L2 Is Empty"
Else
If ThisWorkbook.Sheets(2).Range("L2") = "Approved with Pay" Then
MsgBox "Approved with Pay Email Sent"
ThisWorkbook.Sheets(2).Range("M2") = "Yes"
Else
If ThisWorkbook.Sheets(2).Range("L2") = "Approved without Pay" Then
MsgBox "Approved without Pay Email Sent"
ThisWorkbook.Sheets(2).Range("M2") = "Yes"
Else
If ThisWorkbook.Sheets(2).Range("L2") = "Approved in Part" Then
MsgBox "Approved in Part Email Sent"
ThisWorkbook.Sheets(2).Range("M2") = "Yes"
Else
If ThisWorkbook.Sheets(2).Range("L2") = "Referred back to the Line Manager" Then
MsgBox "Approved without Pay Email Sent"
ThisWorkbook.Sheets(2).Range("M2") = "Yes"
End If
End If
End If
End If
End If
End If
这就是我编辑以下代码的方式,但它似乎不起作用
Option Explicit
Sub Test3()
End Sub
Public Function CheckIfRowIsEmpty(ByVal P2 As Range, _
ByVal colsToCheck As Integer) As Boolean
Dim isRowEmpty As Boolean
Dim startRow As Integer
Dim endRow As Integer
Dim a As Integer
isRowEmpty = True
If IsCellEmpty(wb.Sheets(2).Range("P2")) = True Then
startRow = P2.Column
endRow = P2.Column + (13 - 1)
For a = startRow To endRow
If IsCellEmpty(P2.Offset(, a)) = False Then
isRowEmpty = False
Exit For
End If
Next a
Else
isRowEmpty = False
End If
Debug.Print CStr(isRowEmpty)
CheckIfRowIsEmpty = CStr(isRowEmpty)
End Function
Private Function IsCellEmpty(ByVal P2 As Range) As Boolean
If Len(P2.Value & "") < 1 Then
IsCellEmpty = True
Else
IsCellEmpty = False
End If
End Function