簡體   English   中英

檢查excel單元格是否為空

[英]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解決方案,那么您可以條件格式化。 它不會給出消息框,但會在空白時突出顯示單元格。

要使用條件格式,請按照以下步驟操作

  • 選擇單元格
  • 在主頁選項卡>敘述>條件格式下
  • 將出現條件格式對話框。 單擊“新建規則”按鈕。
  • 選擇使用公式確定要格式化的單元格。
  • 輸入公式(如果選擇單元格A1,則A1 =“”)
  • 單擊格式>轉到填充,然后選擇所需的顏色。

在此輸入圖像描述

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM