简体   繁体   English

Excel 2007 Workbook_open无法正常工作

[英]excel 2007 Workbook_open not working

I am trying to clear Print Area And Autofilter when excel opens: Am total novice in Excel vba so Assmebled the followingcode from googling around This code I have put in ThisWorkbook of Personal.xlsb in the XLstart folder and ofcourse the macro security has been set to enable all macros 我试图在Excel打开时清除“打印区域和自动过滤器”:Excel vba中的新手,因此将以下代码从谷歌搜索中搜集了此代码。我已将此代码放入XLstart文件夹中的Personal.xlsb的ThisWorkbook中,并且将宏安全性设置为启用所有宏

Option Explicit
Public WithEvents xlApp As Excel.Application
Private Sub Workbook_Open()
Set xlApp = Application
End Sub
Private Sub Workbook_Close()
Set xlApp = Nothing
End Sub

Private Sub xlApp_WorkbookOpen(ByVal Wb As Workbook)
Application.EnableEvents = False
Call ClrPrntArea
Application.EnableEvents = True
End Sub

Here is the ClrPrntArea 这是ClrPrntArea

Sub ClrPrntArea()
Dim ws As Object


For i = 1 To ActiveWorkbook.Worksheets.count
    With Worksheets(i)
        .PageSetup.PrintArea = ""
        .PageSetup.FitToPagesWide = 1
    End With
Next
End Sub

I will also be putting another macro call to module in personal xlsb for resetting the autofiter once above starts working..Any inputs will be really helpfull 我还将在个人xlsb中对模块进行另一个宏调用,以在上述方法开始工作后重置自动装配器。任何输入都将非常有帮助

in PERSONAL.xlsb , module ThisWorkbook , try the below; PERSONAL.xlsb模块ThisWorkbook ,尝试以下操作; it's nearly the same code as in your request, with some modif's: 它几乎与您的请求中的代码相同,但有一些修改:

  • application object declared Private 声明为私有的应用程序对象
  • event routine uses the local WB object variable handed over as parameter, instead of the ActiveWorkbook object 事件例程使用移交的本地WB对象变量作为参数,而不是ActiveWorkbook对象
  • replaced For ... Next by For Each ... Next and working with local object variables For Each ... Next代替For ... NextFor Each ... Next使用局部对象变量
  • trap processing of PERSONAL.xlsb itself 陷阱处理PERSONAL.xlsb本身

Once you're happy remove all the MsgBox statements (and the Else ), they are just to show what is happening and when. 一旦您满意,请删除所有MsgBox语句(以及Else ),它们只是用来显示发生了什么情况以及何时发生。

Private WithEvents Excel_App As Excel.Application

' runs when Excel_App encounters a Workbook_Open() event
Private Sub Excel_App_WorkbookOpen(ByVal WB As Workbook)
Dim WS As Worksheet

    If WB.Name <> "PERSONAL.xlsb" Then
        MsgBox "PERSONAL.xlsb: Excel_App_WorkbookOpen(): " & WB.Name
        For Each WS In WB.Worksheets
            WS.PageSetup.PrintArea = ""
            WS.PageSetup.FitToPagesWide = 1
            If WS.FilterMode Then
                WS.ShowAllData
            End If
        Next
    Else
        MsgBox "PERSONAL.xlsb: Excel_App_WorkbookOpen(): myself"
    End If
End Sub

' runs when PERSONAL.xlsb is opened
' assign current Excel application to object variable Excel_App
Private Sub Workbook_Open()
    MsgBox "PERSONAL.xlsb: Workbook_Open()"
    Set Excel_App = Application
End Sub

Note : 注意事项

When the event handler doesn't start when you double-click an Excel file (eg on your desktop), close all Excel applications and inspect the task manager for additional orphaned Excel processes which need to be killed. 当您双击 Excel文件(例如,在桌面上)时事件处理程序没有启动时,请关闭所有Excel应用程序,并检查任务管理器中是否有其他需要终止的孤立Excel进程。 It happened to me while playing around with this code 我在玩这段代码时发生了

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

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