[英]Check If excel cell is empty
我需要excel的vba代碼。 我正在檢查A1,B7,C9是否為空。
如果它們是空的(任何或所有變量都是空的),我需要返回:
“是如此,所以細胞是空的點擊確定填補它”
如果沒有單元格為空,或者所有單元格都不為空,或者它們包含任何值,我需要返回:
“做我的東西”
有一個特定工作簿的鏈接,但我也想在這里查看。
Sub tgr()
Dim CheckCell As Range
For Each CheckCell In Sheets("Sheet1").Range("A1,B7,C9").Cells
If Len(Trim(CheckCell.Value)) = 0 Then
CheckCell.Select
MsgBox "Cell " & CheckCell.Address(0, 0) & " is empty. Click OK and populate it.", , "Missing Information"
Exit Sub
End If
Next CheckCell
'All cells filled, code to Do My Stuff goes here
MsgBox "Do my stuff", , "Continue macro"
End Sub
如果您關心單元格是否為空,則需要在Value屬性上使用IsEmpty函數。 對於具有單撇號的單元格或返回空字符串的函數,這將返回false。
Public Function CellsAreEmpty(ParamArray aCells() As Variant) As Boolean
Dim vItm As Variant
Dim bReturn As Boolean
bReturn = True
For Each vItm In aCells
bReturn = bReturn And IsEmpty(vItm.Value)
Next vItm
CellsAreEmpty = bReturn
End Function
Sub TestCells()
If CellsAreEmpty(Range("A1"), Range("B7"), Range("C9")) Then
Debug.Print "Do stuff"
Else
Debug.Print " is so so so cell is empty click ok to fill it"
End If
End Sub
這是你在找什么?
下面的代碼將檢查sheet1-范圍A1,B7,c9。
Sub checkEmptyCells()
With ThisWorkbook.Sheets("sheet1")
If (Len(.Range("A1")) = 0) Then
MsgBox "Cell A1 is empty. Click ok to fill"
Exit Sub
ElseIf (Len(.Range("B7")) = 0) Then
MsgBox "Cell B7 is empty. Click ok to fill"
Exit Sub
ElseIf (Len(.Range("C9")) = 0) Then
MsgBox "Cell C9 is empty. Click ok to fill"
Exit Sub
Else
MsgBox "Do my stuff"
Exit Sub
End If
End With
End Sub
如果您更喜歡使用非vba解決方案,那么您可以條件格式化。 它不會給出消息框,但會在空白時突出顯示單元格。
要使用條件格式,請按照以下步驟操作
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.