简体   繁体   English

如何在不使用 ActiveWindow 的情况下使用 VBA 关闭 excel 中的网格线

[英]How can I turn off gridlines in excel using VBA, without using ActiveWindow

I have a VBA macro over Excel 2013 which generate a separate excel report.我在 Excel 2013 上有一个 VBA 宏,它生成一个单独的 excel 报告。 In this excel report that is created, I would like to turn off the GridLines.在创建的这个 excel 报告中,我想关闭 GridLines。

The only piece of code that I come across to make this happens is as below我遇到的唯一一段代码如下

ActiveWindow.DisplayGridlines = False

However, this excel is generated in the background ie,然而,这个excel是在后台生成的,即,

Dim appObject As New Excel.Application
appObject.Visible = False

Which means that this report is not the ActiveWindow.这意味着该报告不是 ActiveWindow。 Is there an alternate way of turning off the gridlines without using the ActiveWindow object?是否有另一种方法可以在不使用 ActiveWindow 对象的情况下关闭网格线?

If you have a reference to the workbook, you can just iterate over all of the Windows in its collection.如果您有对工作簿的引用,则只需遍历其集合中的所有 Windows。 If the application isn't visible, you should only get 1 but it's safer than trying to hard code an index:如果应用程序不可见,您应该只得到 1,但这比尝试硬编码索引更安全:

Private Sub ToggleGridLines(target As Workbook)
    Dim wnd As Window
    For Each wnd In target.Windows
        wnd.DisplayGridlines = False
    Next
End Sub

Note that this will set change the display on the active worksheet in the workbook - why this is a property of the window and not the worksheet is beyond me.请注意,这将设置更改工作簿中活动工作表上的显示 - 为什么这是窗口的属性而不是工作表超出我的范围。

EDIT:编辑:

Thanks to the link that @Tim shared, I realized I'd completely spaced off the SheetViews collection.感谢@Tim 分享的链接,我意识到我已经完全脱离了SheetViews集合。 This should turn off gridlines for an arbitrary Worksheet object:这应该关闭任意Worksheet对象的网格线:

Private Sub TurnOffGridLines(target As Worksheet)
    Dim view As WorksheetView
    For Each view In target.Parent.Windows(1).SheetViews
        If view.Sheet.Name = target.Name Then
            view.DisplayGridlines = False
            Exit Sub
        End If
    Next
End Sub

The ActiveWindow is a member of the Windows objects collection. ActiveWindow 是 Windows 对象集合的成员。 As with any collection, simply refer to the actual window by name rather than by specifying the active window.与任何集合一样,只需通过名称而不是通过指定活动窗口来引用实际窗口。 eg例如

Windows("My Workbook.xls").DisplayGridlines = False

We can either do it as "Comintern" suggested or activating the wanted sheets to execute de adequate line of code in a loop.我们可以按照“Comintern”的建议进行操作,也可以激活所需的工作表以在循环中执行足够的代码行。 I tried the code posted above in several ways and I'm posting the snippet that worked the best for me:我以多种方式尝试了上面发布的代码,并发布了最适合我的代码段:

Sub GridLines(Optional target As Worksheet, Optional display As Boolean = True)

    Dim oWnd As Window
    Dim oShView As WorksheetView
    If IsMissing(target) Or target Is Nothing Then
        For Each oShView In ActiveSheet.Parent.Windows(1).SheetViews
            oShView.DisplayGridlines = display
        Next
    Else
        For Each oShView In target.Parent.Windows(1).SheetViews
            If oShView.Sheet.Name = target.Name Then
                oShView.DisplayGridlines = display
                Exit For
            End If
        Next
    End If
    Set oShView = Nothing
    Set oWnd = Nothing
End Sub

Any feedback is very welcomed非常欢迎任何反馈

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM