MS ACCESS VBA:向Excel工作表添加边框

时间:2011-08-23 20:17:30

标签: excel ms-access vba

以下不起作用。如何添加边框?谢谢!

            Set objApp = CreateObject("Excel.Application")
            objApp.Visible = True
            Set wb = objApp.Workbooks.Open("aFile.xls", True, False)
            objApp.Cells.Select
            objApp.Selection.Borders(xlDiagonalDown).LineStyle = xlNone
            objApp.Selection.Borders(xlDiagonalUp).LineStyle = xlNone
            With objApp.Selection.Borders(xlEdgeLeft)
                .LineStyle = xlContinuous
                .ColorIndex = 0
                .TintAndShade = 0
                .Weight = xlThin
            End With
            With objApp.Selection.Borders(xlEdgeTop)
                .LineStyle = xlContinuous
                .ColorIndex = 0
                .TintAndShade = 0
                .Weight = xlThin
            End With
            With objApp.Selection.Borders(xlEdgeBottom)
                .LineStyle = xlContinuous
                .ColorIndex = 0
                .TintAndShade = 0
                .Weight = xlThin
            End With
            With objApp.Selection.Borders(xlEdgeRight)
                .LineStyle = xlContinuous
                .ColorIndex = 0
                .TintAndShade = 0
                .Weight = xlThin
            End With
            With objApp.Selection.Borders(xlInsideVertical)
                .LineStyle = xlContinuous
                .ColorIndex = 0
                .TintAndShade = 0
                .Weight = xlThin
            End With
            With objApp.Selection.Borders(xlInsideHorizontal)
                .LineStyle = xlContinuous
                .ColorIndex = 0
                .TintAndShade = 0
                .Weight = xlThin
            End With

            Set objApp = Nothing

7 个答案:

答案 0 :(得分:2)

Set wb = objApp.Workbooks.Open("aFile.xls", True, False)
wb.Sheets(1).UsedRange.Borders.Weight=xlThin

最好避免不必要地格式化整张纸。

答案 1 :(得分:2)

我有同样的问题,“borders.weight”构造对我有用,并且不需要使用“.cells”来引用范围。例如:

.Range("A11:H11").Borders.Weight = 2

答案 2 :(得分:1)

Set objApp = CreateObject("Excel.Application")
objApp.Visible = True
Set wb = objApp.Workbooks.Open("aFile.xls", True, False)
With objApp.Cells.Borders
    .LineStyle = xlContinuous
    .Weight = xlThin
    .ColorIndex = 0
End With

这应该为您打开工作簿的ACTIVE工作表上的所有单元格边框。 Diaganols默认是关闭的。

答案 3 :(得分:0)

VBA代码对我不起作用,所以我发现了一种解决方法。因为我使用Excel模板来创建Excel工作表。我修改了Excel模板以打印网格线。

启用网格线打印:

Excel>打印预览>页面设置>表>打印下的复选标记网格。

答案 4 :(得分:0)

问题是Access不知道Excel枚举,请尝试:

Function CreateBorders(Range As Object)
    With Range.Borders(7)
        .LineStyle = 1
        .ColorIndex = 0
        .TintAndShade = 0
        .Weight = 2
    End With
    With Range.Borders(8)
        .LineStyle = 1
        .ColorIndex = 0
        .TintAndShade = 0
        .Weight = 2
    End With
    With Range.Borders(9)
        .LineStyle = 1
        .ColorIndex = 0
        .TintAndShade = 0
        .Weight = 2
    End With
    With Range.Borders(10)
        .LineStyle = 1
        .ColorIndex = 0
        .TintAndShade = 0
        .Weight = 2
    End With
    With Range.Borders(11)
        .LineStyle = 1
        .ColorIndex = 0
        .TintAndShade = 0
        .Weight = 2
    End With
    With Range.Borders(12)
        .LineStyle = 1
        .ColorIndex = 0
        .TintAndShade = 0
        .Weight = 2
    End With
End Function

答案 5 :(得分:0)

我已经工作了很长时间,在阅读了这个讨论之后它给了我一些想法,我相应地更改了我的代码,它对我有用..这是与你分享的代码..

将所有边框格式化为xlMedium:

With objApp.Worksheets("AR Tab").Range(.cells(4, 5), .cells(4, 8))
  '.LineStyle = xlMedium    
  .Borders.Weight = 3
End With

仅将左边框格式化为xlMedium:

With objApp.Worksheets("AR Tab").Range(.cells(4, 5), .cells(4, 8))
  '.LineStyle = xlMedium
  .Borders("1").Weight = 3
End With

如果您想要不同的linestyle:

.Borders("1").LineStyle = 1 'Line Style xlContinues
.Borders("1").LineStyle = 2 'Line Style xlDash

希望有所帮助

答案 6 :(得分:-1)

我遇到了类似的问题

  • 之前,然后是..一堆东西..将工作簿和工作表作为变量输入,然后,只需为使用范围设置边界即可。

Set objXLBook = objXLApp.Workbooks.Open(strXLSFile) Set objXLSheet = objXLBook.Sheets("Name of Sheet")

objXLSheet.UsedRange.Borders.LineStyle = xlContinuous