尝试在相同行但不同列的范围上设置边框

时间:2012-03-09 13:21:58

标签: excel vba excel-vba

我基本上有一套汇总表,从一百个左右的工作表中汇集信息 - 一个汇总用于当前周期的信息,而我正在搞乱的那些现在将当前周期信息分解为单个部门财政年度跟踪表。我已经将代码工作得很好用于移动信息,但是在复制信息以设置新添加信息周围的边框后,我无法获得设置/选择正确范围的正确语法。我移动信息的代码是:

Sub FYFTEE()

    Dim shtCurrent As Worksheet
    Dim shtFY As Worksheet
    Dim LastCol As Long
    Dim CopyRng As Range

    With Application
        .ScreenUpdating = False
        .EnableEvents = False
    End With

    Set shtCurrent = ThisWorkbook.Sheets("Cycle Summary")
    Set shtFY = ThisWorkbook.Sheets("FY12 D1")
    Set CopyRng = shtCurrent.Range("$C$12:$C$47")

    LastCol = shtFY.Cells("5", Columns.Count).End(xlToLeft).Column + 1

    With CopyRng
        shtFY.Cells("5", LastCol).Resize(.Rows.Count, .Columns.Count).Value = .Value
    End With

    shtFY.Cells("4", LastCol).Value = shtCurrent.Range("I3").Value

    With Application
        .ScreenUpdating = True
        .EnableEvents = True
    End With

End Sub

感谢您的建议和指导!

1 个答案:

答案 0 :(得分:2)

假设您希望围绕每组复制数据设置一组新边框,请声明:

Dim destinationRange As Range

并将您的With CopyRng声明更改为:

With CopyRng
    Set destinationRange = shtFY.Cells("5", LastCol).Resize(.Rows.Count, .Columns.Count)
    destinationRange.Value = .Value
    destinationRange.BorderAround Weight:=xlThick
End With

这将为您复制的每组数据提供一个粗边框。

或者,如果您需要在目标表单上的所有内容周围设置边框,则可以使用此边框而不是上面的代码:

shtFY.Cells.Borders.LineStyle = xlNone
shtFY.UsedRange.BorderAround Weight:=xlThick

紧接着这一行:

shtFY.Cells("4", LastCol).Value = shtCurrent.Range("I3").Value