2011-08-23 84 views
0

以下不起作用。我如何添加边框?谢谢!MSACCESS VBA:将边框添加到Excel工作表

  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 
+0

什么是您的错误信息还是它不工作?为什么首先选择细胞?你应该可以直接使用'objApp.cells'。 – Banjoe

+0

没有错误消息。 objApp.Cells.Borders不起作用。 – Bruno

回答

0

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

为了使网格线打印:

的Excel>打印预览>页面设置>页>检查下打印标记网格线。

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 

这应该给你周围的所有单元格边框上,你打开的工作簿中的工作表。 Diaganols默认关闭。

+0

如何将纸张(1)设置为活动纸张? objApp.ActiveSheet = wb.Sheets(1)? – Bruno

+0

你的代码似乎是围绕活动单元放置边框。如何选择活动工作表?谢谢。 – Bruno

+1

wb.Sheets(1).Activate将激活第一张纸。 –

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

更好的是避免不必要地格式化整张纸。

+0

'wb.Sheets(1).Range(“A:K”)。Borders.Weight = xlThin'这不起作用。什么都没发生。 – Bruno

+0

适用于我(XL2007)。这是你正在使用的第一张纸吗? –

+0

我必须做一些不同的事情。我试过'wb.Sheets(1).Activate wb.Sheets(1).Range(“A:K”)。Borders.Weight = xlThick'什么都没发生。如果我找到解决方案,我会在这里发布。谢谢! – Bruno

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 
0

我一直在这个相当长的时间,阅读本dicussion后,它给了我一些想法,我改变了我因此代码和它的作品对我..这里是与大家分享代码..

要格式化所有边框xlMedium:

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

要仅左边框的格式,以XLM edium:

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

如果你想有不同的线型:

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

希望它可以帮助

2

我有同样的问题和“borders.weight”建设为我的作品和有不需要使用“.cells”来引用一个范围。例如:

.Range("A11:H11").Borders.Weight = 2 
相关问题