excel - How can I run a macro as a workbook opens for the first time only? -
i've got workbook runs macro show userform open1 opens, using (very basic) code:
private sub workbook_open() open1.show end sub
this job fine - each time open workbook, userform pops , runs perfectly.
but, want userform appear first time workbook opened only. there way allow happen?
you use dummy module gets deleted first time open spreadsheet...
something like:
if moduleexists("dummymodule") open1.show docmd.deleteobject acmodule, "dummymodule" end if function moduleexists(strmodulename string) boolean dim mdl object each mdl in currentproject.allmodules if mdl.name = strmodulename moduleexists = true exit end if next end function
update: stated, docmd isn't used in excel vba. teach me write code without testing it! following updated code work, in order access vb environment, excel needs trusted.
there setting in trust center>macro settings can tick code work under developer macro settings
as such, may not way go opens possibility of security issues...
sub removemodule() if moduleexists("dummymodule") open1.show dim vbcom object: set vbcom = application.vbe.activevbproject.vbcomponents vbcom.remove vbcomponent:=vbcom.item("dummymodule") end if end sub function moduleexists(strmodulename string) boolean dim mdl object each mdl in application.vbe.activevbproject.vbcomponents if mdl.name = strmodulename moduleexists = true exit end if next end function
Comments
Post a Comment