VBA为除BudgetByMonth外所有工作表加边框仅当前页生效如何解决
问题根源
With ws代码块内的Range、Rows对象未加.绑定到当前循环的工作表对象,VBA中未显式指定所属工作表的区域对象,默认指向当前处于激活状态的工作表,因此边框仅应用到激活表。
修正后代码
Sub FormatLastRow() Dim ws As Worksheet Dim LastRow As Long Dim TargetRng As Range For Each ws In ThisWorkbook.Worksheets If ws.Name <> "BudgetByMonth" Then With ws LastRow = .Range("A" & .Rows.Count).End(xlUp).Row Set TargetRng = .Range("A" & LastRow, "BB" & LastRow) With TargetRng.Borders(xlEdgeBottom) .Weight = xlMedium .ColorIndex = 29 End With With TargetRng.Borders(xlEdgeTop) .Weight = xlMedium .ColorIndex = 29 End With End With End If Next ws End Sub
内容的提问来源于stack exchange,提问作者J Dodge
相关产品推荐
相关产品推荐

