用于检查空白单元格并突出显示它们的宏

时间:2013-08-18 06:55:12

标签: excel vba

我需要在Excel中创建一个宏,可以检查单元格内容是否为空,然后我需要一个边框。

我试过这个宏:

Sub testborder()

    Dim rRng As Range

    Set rRng = Sheet1.Range("B2:D5")

    'Clear existing
    rRng.Borders.LineStyle = xlNone

    'Apply new borders
    rRng.BorderAround xlContinuous
    rRng.Borders(xlInsideHorizontal).LineStyle = xlContinuous
    rRng.Borders(xlInsideVertical).LineStyle = xlContinuous

End Sub

2 个答案:

答案 0 :(得分:0)

尝试以下方法:

Sub testborder()

    Dim rRng As Range, row As Range, c As Range
    Set rRng = Sheet1.Range("B2:D5")
    'Clear existing
    rRng.Borders.LineStyle = xlNone

    For Each row In rRng.Rows
      For Each c In row.Columns
        'Apply new borders
        If (c.Value > "") Then c.BorderAround xlContinuous
      Next c
    Next row

End Sub

或者,使用更简单的循环:

    For Each c In rRng.Cells
      'Apply new borders
      If (c.Value > "") Then c.BorderAround xlContinuous
    Next c

答案 1 :(得分:0)

你可以做任何你想要的测试。在此示例中,它检查每个单元格中是否有任何文本,如果是,则在其周围放置边框。

Sub BorderForNonEmpty()

    Dim myRange As Range
    Set myRange = Sheet1.Range("B2:D5")

    ' Clear existing borders
    myRange.Borders.LineStyle = xlLineStyleNone


    ' Test each cell and put a border around it if it has content
    For Each myCell In myRange
        If myCell.Text <> "" Then
            myCell.BorderAround (xlContinuous)
        End If
    Next

End Sub
相关问题