我一直在尝试在两个不同的工作表上为两个范围添加边框。 在我的例子中,第一张有14行,而第二张有30行。每个工作表都具有相同数量的列。当我运行我的代码时,第一个工作表工作正常,但第二个工作表只有14行边界,其他16个没有边框。为什么我的代码不在我的第二个工作表的最后16列?
Sub lines()
Dim wb As Worksheet
Dim wb2 As Worksheet
Dim arrBorders As Variant, vBorder As Variant
Set wb = Worksheets("wb Summary")
Set wb2 = Worksheets("wb2 Summary")
arrBorders = Array(xlEdgeLeft, xlEdgeTop, xlEdgeBottom, _
xlEdgeRight, xlInsideVertical, xlInsideHorizontal)
With wb.Range("A2:H" & Cells(Rows.Count, "H").End(xlUp).Row)
For Each vBorder In arrBorders
With .Borders(vBorder)
.LineStyle = xlContinuous
.Weight = xlThin
End With
Next
End With
With wb2.Range("A2:H" & Cells(Rows.Count, "H").End(xlUp).Row)
For Each vBorder In arrBorders
With .Borders(vBorder)
.LineStyle = xlContinuous
.Weight = xlThin
End With
Next
End With
End Sub
答案 0 :(得分:1)
您需要完全参考表格。我认为你也可以通过避免循环来缩短你的代码。
Sub lines()
Dim wb As Worksheet
Dim wb2 As Worksheet
Set wb = Worksheets("wb Summary")
Set wb2 = Worksheets("wb2 Summary")
With wb.Range("A2:H" & wb.Cells(wb.Rows.Count, "H").End(xlUp).Row)
.Borders.LineStyle = xlContinuous
.Borders.Weight = xlThin
End With
With wb2.Range("A2:H" & wb2.Cells(wb2.Rows.Count, "H").End(xlUp).Row)
.Borders.LineStyle = xlContinuous
.Borders.Weight = xlThin
End With
End Sub