单元格为空时显示消息

时间:2015-10-22 03:01:52

标签: excel vba excel-vba

目前,当指定的单元格为空时,我设法为单个单元格执行操作,然后在单元格上显示消息/语句。

Private Sub Worksheet_SelectionChange(ByVal Target As Range)

If Range("AA17").Value = ISBLANK Then
    Range("AA17").Value = "Please Specify"
End If

End Sub

我想要做的是,对于几个单元格,它将显示相同的内容。我可以继续为所有celsl做同样的事情,但我有几百个单元格来格式化它。

有办法吗?

2 个答案:

答案 0 :(得分:1)

如果细胞是连续的,你可以循环它们。

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Dim checkRng As Range
Dim cel As Range

Set checkRng = Range("A7:A70")
For Each cel In checkRng
    If cel.Value = ISBLANK Then
        cel.Value = "Please Specify"
    End If
Next cel

End Sub

答案 1 :(得分:1)

如果指定范围内有任何更改,则以下代码将运行

Private Sub Worksheet_SelectionChange(ByVal Target As Range)

    Dim Rng As Range
    Dim wb As Workbook
    Dim ws As Worksheet

    Set wb = ThisWorkbook
    Set ws = wb.ActiveSheet
    Set Rng = ws.Range("A1:A100")

        If Not Intersect(Target, Rng) Is Nothing Then
            For Each Cell In Rng
                If IsEmpty(Cell.Value) = True Then
                     Cell.Value = "Please Specify"
                End If
            Next
        End If

    Set Rng = Nothing
    Set ws = Nothing
    Set wb = Nothing

End Sub