更新时间:2023-12-05 17:07:04
显然打开一个工作簿不会自动使其成为活动的工作簿,至少在此事件处理程序启动时。尝试这样:
Private Sub App_WorkbookOpen(ByVal Wb As Workbook)
/ pre>
如果不是Wb是没有,然后
如果InStr(Wb.Name,New Quote)然后
quoteCheck = MsgBox(你想运行报价生成器?,vbYesNoQuote Generator)
如果quoteCheck = vbYes然后
准备
Else
结束
结束如果
结束如果
结束如果
结束SubI've created an Excel add-in that attempts to run upon the opening of any and all workbooks that are opened during this session. It works sometimes - but not always, and I don't know why.
I created a file,
addin.xlam
, and in this file, inThisWorkbook
, I have:Private XLApp As CExcelEvents Private Sub Workbook_Open() Set XLApp = New CExcelEvents End Sub
I then created a class module based off the code here: http://www.cpearson.com/Excel/AppEvent.aspx
Private WithEvents App As Application Private Sub Class_Initialize() Set App = Application End Sub Private Sub App_WorkbookOpen(ByVal Wb As Workbook) If Not ActiveWorkbook Is Nothing Then If InStr(ActiveWorkbook.Name, "New Quote") Then quoteCheck = MsgBox("Do you want to run the Quote Generator?", vbYesNo, "Quote Generator") If quoteCheck = vbYes Then prepare Else End End If End If End If End Sub
If I close out of Excel and open a file from Windows Explorer, this line hits:
Private Sub App_WorkbookOpen(ByVal Wb As Workbook)
And starts the code - if the workbook in question has "new quote" in its name, the macro runs. Boom. Perfect.
However, after this runs ONCE, if I open another workbook with the words "new quote", this private sub doesn't trigger. Why?
How do I get this to trigger each time I open any workbook?
Apparently opening a workbook doesn't automatically make it the active workbook, at least in time for this event handler to fire. Try this:
Private Sub App_WorkbookOpen(ByVal Wb As Workbook) If Not Wb Is Nothing Then If InStr(Wb.Name, "New Quote") Then quoteCheck = MsgBox("Do you want to run the Quote Generator?", vbYesNo, "Quote Generator") If quoteCheck = vbYes Then prepare Else End End If End If End If End Sub