2011-08-22 85 views
0

在下面的代碼中,如何將網格線添加到整個Excel工作表中?將網格線添加到Excel工作表

Set objApp = CreateObject("Excel.Application") 

    objApp.Visible = True 
    Set wb = objApp.Workbooks.Open("template.xls", True, False) 
    wb.Sheets(1).Rows(3).Delete 
    wb.Sheets(1).Range("A1").Value = title 
    'need to format column E & F as currency 

    Set objApp = Nothing 

回答

4

這是一個很長的答案(Excel VBA在錄製宏時生成的代碼)。你絕對可以縮短這一點。例如,您不需要設置.ColorIndex或.TintAndShade屬性只是爲了執行標準的黑色[編輯] 邊框

Cells.Select 
Selection.Borders(xlDiagonalDown).LineStyle = xlNone 
Selection.Borders(xlDiagonalUp).LineStyle = xlNone 
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 
With Selection.Borders(xlEdgeBottom) 
    .LineStyle = xlContinuous 
    .ColorIndex = 0 
    .TintAndShade = 0 
    .Weight = xlThin 
End With 
With Selection.Borders(xlEdgeRight) 
    .LineStyle = xlContinuous 
    .ColorIndex = 0 
    .TintAndShade = 0 
    .Weight = xlThin 
End With 
With Selection.Borders(xlInsideVertical) 
    .LineStyle = xlContinuous 
    .ColorIndex = 0 
    .TintAndShade = 0 
    .Weight = xlThin 
End With 
With Selection.Borders(xlInsideHorizontal) 
    .LineStyle = xlContinuous 
    .ColorIndex = 0 
    .TintAndShade = 0 
    .Weight = xlThin 
End With 

編輯

對於網格線:

ActiveWindow.DisplayGridlines = True 

你也可以使用:

Windows(1).DisplayGridlines = True 
+0

這是爲了邊界,網格線還是別的......謝謝你! – Bruno

+0

我該如何使用下面這段代碼的邊界代碼(我無法引用Cells.Select):'Set objApp = CreateObject(「Excel.Application」) objApp.Visible = True Set wb = objApp.Workbooks。 Open(「template.xls」,True,False) wb.Sheets(1).Rows(3).Delete wb.Sheets(1).Range(「A1」)。Value = title' – Bruno

+0

'wb.Sheets (1).Cells.Select'不起作用。 – Bruno

3

試試這個:

ActiveWindow.DisplayGridlines = True 

這應該打開網格線。當然,將該物業設置爲False將關閉它們。