Access / excel vba中的范围问题

不确定我做错了什么。 以下任何一条语句都不会产生错误,但是它们不起作用。 我之前有一个关于边界的问题,我认为它的工作,但没有

这是代码片段

With ws With .Range(.Cells(startRow, startCol), .Cells(endRow, endCol)) .Borders.LineStyle = 1 .Borders.Weight = 4 End With End With ws.Range("D1").Value = 3.14159 With ws.Range("D1").Borders(9) .LineStyle = 1 .Weight = 2 .ColorIndex = 3 End With ws.usedrange.Borders.LineStyle = 1 ws.usedrange.Borders.Weight = 4 ws.usedrange.Borders.Color = RGB(255, 0, 0) With ws.Range("E1:F2") .Value = "x" .Font.Bold = True .Borders.LineStyle = 1 End With 

我把你的代码从上一个问题,并添加了最后四行保存更改和closureswb。 这应该解决您的问题:

 Sub setBorder() Dim xlApp As Object Dim wb As Object Dim ws As Object Dim rng As Object Dim startRow As Integer, startCol As Integer Dim endRow As Integer, endCol As Integer Set xlApp = CreateObject("Excel.Application") xlApp.DisplayAlerts = False Set wb = xlApp.Workbooks.Open("H:\Documents\Misc-Work\BU\test.xlsx") 'your code wb.Close True xlApp.Quit Set wb = Nothing Set xlApp = Nothing End Sub 

请注意,由于您已经运行了以前的代码(不closureswb和xlApp),可能需要使用任务pipe理器closures文件“H:\ Documents \ Misc-Work \ BU \ test.xlsx”(即使看起来好像你没有打开这个文件)

可能是你的参数………….以下工作:

 Sub dural() Dim ws As Worksheet Set ws = ActiveSheet startRow = 3 startCol = 3 endRow = 7 endCol = 7 With ws With .Range(.Cells(startRow, startCol), .Cells(endRow, endCol)) .Borders.LineStyle = 1 .Borders.Weight = 4 End With End With End Sub