如何在不使用ActiveWindow的情况下使用VBA在excel中关闭网格线 [英] How can I turn off gridlines in excel using VBA, without using ActiveWindow
问题描述
我在Excel 2013上有一个VBA宏,该宏可生成单独的Excel报告.在创建的这个excel报告中,我想关闭GridLines.
I have a VBA macro over Excel 2013 which generate a separate excel report. In this excel report that is created, I would like to turn off the GridLines.
我碰到的唯一的代码是这样的
The only piece of code that I come across to make this happens is as below
ActiveWindow.DisplayGridlines = False
但是,此excel是在后台生成的,即
However, this excel is generated in the background i.e.,
Dim appObject As New Excel.Application
appObject.Visible = False
这意味着此报告不是ActiveWindow.是否有不使用ActiveWindow对象而关闭网格线的替代方法?
Which means that this report is not the ActiveWindow. Is there an alternate way of turning off the gridlines without using the ActiveWindow object?
推荐答案
如果对工作簿有引用,则可以遍历其集合中的所有Windows.如果应用程序不可见,您应该只获得1,但比尝试对索引进行硬编码更为安全:
If you have a reference to the workbook, you can just iterate over all of the Windows in its collection. If the application isn't visible, you should only get 1 but it's safer than trying to hard code an index:
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.
借助@Tim共享的链接,我意识到我已经完全分隔了SheetViews
集合.这应该关闭任意Worksheet
对象的网格线:
Thanks to the link that @Tim shared, I realized I'd completely spaced off the SheetViews
collection. This should turn off gridlines for an arbitrary Worksheet
object:
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
这篇关于如何在不使用ActiveWindow的情况下使用VBA在excel中关闭网格线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!