选择应用边框的范围 - 访问VBA

时间:2014-02-04 16:46:09

标签: excel vba ms-access

这是我需要做的。根据行号和列号选择一系列单元格。由于它是动态的,我必须使用行号和列号。我在这里硬编码就是一个例子。一旦我有了范围,我需要在它周围应用厚边框。我可以选择范围。任何帮助将不胜感激。我已经包含了部分选择代码。我已经说明它失败了。

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")

startRow = 6
startCol = 5
endRow = 15
endCol = 5

Set ws = wb.worksheets(1)

With ws
   Set rng = .Range(.Cells(startRow, startCol), .Cells(endRow, endCol))
   rng.select -- It fails here
End With

'rng.select

With Selection.Borders(xlEdgeLeft)
    .LineStyle = xlContinuous
    .ColorIndex = 0
    .TintAndShade = 0
    .Weight = xlThin
End With
With Selection.Borders(xlEdgeTop)
    .LineStyle = xlContinuous
    .ColorIndex = 0
    .TintAndShade = 0
    .Weight = xlThin
End With
End Sub

1 个答案:

答案 0 :(得分:2)

无需使用select:

With ws
    Set rng = .Range(.Cells(startRow, startCol), .Cells(endRow, endCol))
End With

With rng.Borders(7)
    .LineStyle = 1
    .ColorIndex = 0
    .TintAndShade = 0
    .Weight = 2
End With
With rng.Borders(8)
    .LineStyle = 1
    .ColorIndex = 0
    .TintAndShade = 0
    .Weight = 2
End With

顺便说一句, Access不知道常量 xlEdgeLeft等,直到您在工具中添加 Microsoft Excel对象库的引用 - > ;参考文献。您也可以将此常量更改为实际值,而不像上面的代码中那样添加对库的引用,但这会使您的代码不那么清晰。