Excel VBA: 重复添加外边框
Excel VBA: Repeatedly Add Outside Borders
我需要在多个工作表的某些范围内添加外边框,因此我编写了以下测试代码(有效):
Sub TestFun()
Dim wb As Workbook
Dim ws As Worksheet
Set wb = Workbooks("TestBook.xlsm")
wb.Worksheets("Sheet1").Select
Range("B2:D10").Select
AddOutsideBorders
wb.Worksheets("Sheet2").Select
Range("B2:D10").Select
AddOutsideBorders
wb.Worksheets("Sheet3").Select
Range("B2:D10").Select
AddOutsideBorders
End Sub
Sub AddOutsideBorders()
With Selection
.Borders(xlEdgeLeft).LineStyle = xlContinuous
.Borders(xlEdgeTop).LineStyle = xlContinuous
.Borders(xlEdgeBottom).LineStyle = xlContinuous
.Borders(xlEdgeRight).LineStyle = xlContinuous
End With
End Sub
但是,我的真实工作簿包含更多工作表,我需要执行更复杂的任务。所以问题是,如何在不先选择范围的情况下添加边框?例如,我想要像这样整洁的东西(它不起作用):
Sub TestFun()
Dim wb As Workbook
Dim ws As Worksheet
Set wb = Workbooks("TestBook.xlsm")
wb.Worksheets("Sheet1").Range("B2:D10").AddOutsideBorders
wb.Worksheets("Sheet2").Range("B2:D10").AddOutsideBorders
wb.Worksheets("Sheet3").Range("B2:D10").AddOutsideBorders
End Sub
Sub AddOutsideBorders(rng As Range)
With rng
.Borders(xlEdgeLeft).LineStyle = xlContinuous
.Borders(xlEdgeTop).LineStyle = xlContinuous
.Borders(xlEdgeBottom).LineStyle = xlContinuous
.Borders(xlEdgeRight).LineStyle = xlContinuous
End With
End Sub
更一般地说,我如何调用在子范围内定义的另一个函数?
您尝试的方法几乎是有效的(并且您尝试避免 select 对您有好处),您只是以错误的方式调用了该过程。相反,它应该被称为:
Call AddOutsideBorders(wb.Worksheets("Sheet1").Range("B2:D10"))
Call AddOutsideBorders(wb.Worksheets("Sheet2").Range("B2:D10"))
Call AddOutsideBorders(wb.Worksheets("Sheet3").Range("B2:D10"))
将 AddOutsideBorders 转换为如下函数:
Sub TestFunc()
Dim wb As Workbook
Set wb = Workbooks("TestBook.xlsm")
AddOutsideBorders wb.Worksheets("Sheet1").Range("B2:D10")
AddOutsideBorders wb.Worksheets("Sheet2").Range("B2:D10")
AddOutsideBorders wb.Worksheets("Sheet3").Range("B2:D10")
End Sub
Public Function AddOutsideBorders(rng As Range)
With rng
.Borders(xlEdgeLeft).LineStyle = xlContinuous
.Borders(xlEdgeTop).LineStyle = xlContinuous
.Borders(xlEdgeBottom).LineStyle = xlContinuous
.Borders(xlEdgeRight).LineStyle = xlContinuous
End With
End Function
事实证明,有一个范围方法可以执行您的 AddOutsideBorders 子程序所做的事情(并且无需指定 4 个外部边界中的每一个)- .BorderAround 方法。在这种情况下,您将实施:
wb.Worksheets("Sheet1").Range("B2:D10").BorderAround LineStyle:=xlContinuous
您也可以同时设置其他参数。 Here's the link 在 Microsoft 文档中。
我需要在多个工作表的某些范围内添加外边框,因此我编写了以下测试代码(有效):
Sub TestFun()
Dim wb As Workbook
Dim ws As Worksheet
Set wb = Workbooks("TestBook.xlsm")
wb.Worksheets("Sheet1").Select
Range("B2:D10").Select
AddOutsideBorders
wb.Worksheets("Sheet2").Select
Range("B2:D10").Select
AddOutsideBorders
wb.Worksheets("Sheet3").Select
Range("B2:D10").Select
AddOutsideBorders
End Sub
Sub AddOutsideBorders()
With Selection
.Borders(xlEdgeLeft).LineStyle = xlContinuous
.Borders(xlEdgeTop).LineStyle = xlContinuous
.Borders(xlEdgeBottom).LineStyle = xlContinuous
.Borders(xlEdgeRight).LineStyle = xlContinuous
End With
End Sub
但是,我的真实工作簿包含更多工作表,我需要执行更复杂的任务。所以问题是,如何在不先选择范围的情况下添加边框?例如,我想要像这样整洁的东西(它不起作用):
Sub TestFun()
Dim wb As Workbook
Dim ws As Worksheet
Set wb = Workbooks("TestBook.xlsm")
wb.Worksheets("Sheet1").Range("B2:D10").AddOutsideBorders
wb.Worksheets("Sheet2").Range("B2:D10").AddOutsideBorders
wb.Worksheets("Sheet3").Range("B2:D10").AddOutsideBorders
End Sub
Sub AddOutsideBorders(rng As Range)
With rng
.Borders(xlEdgeLeft).LineStyle = xlContinuous
.Borders(xlEdgeTop).LineStyle = xlContinuous
.Borders(xlEdgeBottom).LineStyle = xlContinuous
.Borders(xlEdgeRight).LineStyle = xlContinuous
End With
End Sub
更一般地说,我如何调用在子范围内定义的另一个函数?
您尝试的方法几乎是有效的(并且您尝试避免 select 对您有好处),您只是以错误的方式调用了该过程。相反,它应该被称为:
Call AddOutsideBorders(wb.Worksheets("Sheet1").Range("B2:D10"))
Call AddOutsideBorders(wb.Worksheets("Sheet2").Range("B2:D10"))
Call AddOutsideBorders(wb.Worksheets("Sheet3").Range("B2:D10"))
将 AddOutsideBorders 转换为如下函数:
Sub TestFunc()
Dim wb As Workbook
Set wb = Workbooks("TestBook.xlsm")
AddOutsideBorders wb.Worksheets("Sheet1").Range("B2:D10")
AddOutsideBorders wb.Worksheets("Sheet2").Range("B2:D10")
AddOutsideBorders wb.Worksheets("Sheet3").Range("B2:D10")
End Sub
Public Function AddOutsideBorders(rng As Range)
With rng
.Borders(xlEdgeLeft).LineStyle = xlContinuous
.Borders(xlEdgeTop).LineStyle = xlContinuous
.Borders(xlEdgeBottom).LineStyle = xlContinuous
.Borders(xlEdgeRight).LineStyle = xlContinuous
End With
End Function
事实证明,有一个范围方法可以执行您的 AddOutsideBorders 子程序所做的事情(并且无需指定 4 个外部边界中的每一个)- .BorderAround 方法。在这种情况下,您将实施:
wb.Worksheets("Sheet1").Range("B2:D10").BorderAround LineStyle:=xlContinuous
您也可以同时设置其他参数。 Here's the link 在 Microsoft 文档中。