vba 将网格线添加到 Excel 工作表

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7153205/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-11 13:51:23  来源:igfitidea点击:

Add gridlines to an Excel sheet

excelms-accessvba

提问by Bruno

In the below code, how can I add grid-lines to the entire Excel sheet?

在下面的代码中,如何向整个 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

回答by Stewbob

This is the long answer (the code Excel VBA generates when you record a Macro). You can definitely shorten this up. For instance, you don't need to set the .ColorIndex or .TintAndShade properties just to do a standard black [edit] border.

这是一个很长的答案(Excel VBA 在录制宏时生成的代码)。你绝对可以缩短这个。例如,您不需要设置 .ColorIndex 或 .TintAndShade 属性只是为了执行标准的黑色 [edit] border

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

EDIT

编辑

For gridlines:

对于网格线:

ActiveWindow.DisplayGridlines = True

you can also use:

您还可以使用:

Windows(1).DisplayGridlines = True

回答by Patrick

Try this:

尝试这个:

ActiveWindow.DisplayGridlines = True

That should turn the gridlines on. And, of course, setting the property to Falsewill turn them off.

那应该打开网格线。而且,当然,将属性设置为False将关闭它们。