选择应用边框的范围 - Access VBA
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21558366/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Selecting range to apply borders - Access VBA
提问by rajeev
Here is what I need to do. Select a range of cells based on the row and column numbers. Since it is a dynamic one, I have to use the row and column numbers. I have hardcoded them here as an example. Once I have the range, I need to apply a thick border all around it. I am uable to select the range. Any help would be greatly appreciated. I have included part of the selection code. I have indicated where it fails.
这是我需要做的。根据行号和列号选择一系列单元格。由于它是动态的,我必须使用行号和列号。作为例子,我在这里对它们进行了硬编码。一旦我有了范围,我需要在它周围应用一个粗边框。我可以选择范围。任何帮助将不胜感激。我已经包含了部分选择代码。我已经指出它失败的地方。
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
回答by Dmitry Pavliv
There is no need to use 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
Btw, Access don't know about constantsxlEdgeLeft
and etc, until you add reference to Microsoft Excel Object Libraryin Tools->References. You can also change this constanst to their actual values without adding reference to library as in my code above, but this will make your code less clear.
顺便说一句,Access 不知道常量xlEdgeLeft
等,直到您在Tools->References 中添加对Microsoft Excel 对象库的引用。您也可以将此常量更改为它们的实际值,而无需像上面的代码那样添加对库的引用,但这会使您的代码不太清晰。