【问题标题】:Apply borders in a used cell Range VBA在使用的单元格 Range VBA 中应用边框
【发布时间】:2015-04-25 07:16:32
【问题描述】:

我正在尝试在一组已使用的单元格周围动态应用边框column Range is (B7:E7)行数总是变化,所以代码需要是动态的。我下面的代码没有实现这一点:

Sub Borders()

Application.ScreenUpdating = False
Dim lngLstCol As Long, lngLstRow As Long

lngLstRow = ActiveSheet.UsedRange.Rows.Count
lngLstCol = ActiveSheet.UsedRange.Columns.Count

For Each rngCell In Range("B7:B" & lngLstRow)
    If rngCell.Value > "" Then
        r = rngCell.row
        c = rngCell.Column
        Range(Cells(r, c), Cells(r, lngLstCol)).Select
            With Selection.Borders
                .LineStyle = xlContinuous
                .Weight = xlThin
                .ColorIndex = xlAutomatic
            End With
    End If
Next

Application.ScreenUpdating = True

End Sub

【问题讨论】:

  • 您的代码将边框应用于B 列为>"" 的每一行。你到底期待什么?
  • 我希望能够在 B7:C7 列到 lastrow 的已用单元格周围应用边界,所以我的代码是错误的,
  • 您可以添加示例预期输出的图像吗?
  • 此代码在所有单元格B7 周围设置边框,并按预期向右和向下移动..
  • @user3964075 例如,我想在 Range("B6:E12") 等范围内应用一个板。列范围不会改变它的 B6:E6,只是行数根据生成的数据而变化。所以它基本上是一个厚框边框。

标签: vba loops excel border


【解决方案1】:

此代码在B7 之外的所有非空单元格周围设置边框。

Sub Borders()

    Application.ScreenUpdating = False

    Dim lngLstCol As Long, lngLstRow As Long

    lngLstRow = ActiveSheet.UsedRange.Rows.Count
    lngLstCol = ActiveSheet.UsedRange.Columns.Count

    For Each rngCell In Range(Range("B7"), Cells(lngLstRow, lngLstCol))
        If rngCell.Value > "" Then
            rngCell.Select 'Select cells
            With Selection.Borders
                .LineStyle = xlContinuous
                .Weight = xlThin
                .ColorIndex = xlAutomatic
            End With
        End If
    Next

    Application.ScreenUpdating = True

End Sub

下面的代码在B7之外的使用范围周围设置边框:

Sub BordersB()

    Application.ScreenUpdating = False

    Dim lngLstCol As Long, lngLstRow As Long

    lngLstRow = ActiveSheet.UsedRange.Rows.Count
    lngLstCol = ActiveSheet.UsedRange.Columns.Count

    With Range(Range("B7"), Cells(lngLstRow, 2)).Borders(xlEdgeLeft)
        .LineStyle = xlContinuous
        .Weight = xlThin
        .ColorIndex = xlAutomatic
    End With

    With Range(Range("B7"), Cells(7, lngLstCol)).Borders(xlEdgeTop)
        .LineStyle = xlContinuous
        .Weight = xlThin
        .ColorIndex = xlAutomatic
    End With

    With Range(Cells(7, lngLstCol), Cells(lngLstRow, lngLstCol)).Borders(xlEdgeRight)
        .LineStyle = xlContinuous
        .Weight = xlThin
        .ColorIndex = xlAutomatic
    End With

    With Range(Cells(lngLstRow, 2), Cells(lngLstRow, lngLstCol)).Borders(xlEdgeBottom)
        .LineStyle = xlContinuous
        .Weight = xlThin
        .ColorIndex = xlAutomatic
    End With

    Application.ScreenUpdating = True

End Sub

【讨论】:

  • @James 我编辑了我的代码。你也可以使用这个。这更好,可能会更快。
  • 这怎么可能是正确的答案?您要求在 Columns(B:C) 中设置边框,上面的代码将在第 6 行以下的所有单元格以及除 A 列之外的所有单元格上放置边框
  • @Davesexcel 这已经在上面讨论过了。为混乱道歉。非常感谢。
  • @James ı 添加了一个包含已使用范围的代码。 (当然它与空单元格无关。)
  • @danieltakeshi 你是对的;谢谢:不需要。
【解决方案2】:

这将为 Columns(B:C) 中第 6 行下方的所有非空白单元格添加边框

    Sub AddBorders()
    Dim Rws As Long, Rng As Range, c As Range

    Rws = Range("A1").SpecialCells(xlCellTypeLastCell).Row
    Set Rng = Range(Cells(7, "B"), Cells(Rws, "C"))

    For Each c In Rng.Cells

        If c <> "" Then

            With c.Borders

                .LineStyle = xlContinuous
                .Weight = xlThin
                .ColorIndex = xlAutomatic

            End With

        End If

    Next c

End Sub

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-04-08
    • 2019-01-30
    相关资源
    最近更新 更多