0

我需要在 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
4

2 回答 2

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
于 2013-08-18T07:12:26.810 回答
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
于 2013-08-18T07:19:00.763 回答