2011-08-23 98 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 
相關問題