I'm a novice at VBA and am having trouble making my userform hide(unload) after 4 seconds. I want my userform (redflag) to display
on sheet 1 when I double click on my desktop excel file. Below is my code which I copied from an answer(by zarquon) to iamstevie on 6 Dec 2006. I'm not sure if the code should be in the Workbook or in Sheet 1?
When I double click my file, the redflag form shows but never goes away until I click the red x on the form. Can someone help?
Thanks
Pete
temporarily display userform
-
-
-
Re: temporarily display userform
Place this in a Module
Code
Display MorePublic dTime As Date Sub StartTimer() dTime = Now + TimeValue("00:00:05") Application.OnTime dTime, "ReturnHome" End Sub Sub ReturnHome() On Error Resume Next Application.EnableEvents = False redflag.Hide Application.EnableEvents = True Application.DisplayAlerts = False End Sub
Place this in the Userforms Initialize event.
And finally, place this in the Workbook_Open event.
Hope this helps Cheers
-
Re: temporarily display userform
Thanks. I'll try this later this evening.
-
Re: temporarily display userform
yegarboy,
Your code worked great. Can you give me a few words why I needed to add a module to put the code in and couldn't put it in the workbook and userform. Seems like you should be able to put code in the userform and the workbook but again, I'm a novice so bear with me.
Thanks again for your quick reply. These forms are very beneficial when one gets stumped.
Pete -
Re: temporarily display userform
I'm terrible at explanations lol. Because of the way a UserForm handles the code vs a module. If you place the Application.Wait in a Userform it will pause everything as soon as the Form opens and not finish the cycle. Im not sure why it does but it does. Maybe another expert can shed a tad more light on that for you. Glad it worked for you. Cheers
-
-
Re: temporarily display userform
yegarboy,
Your revised code works. Now I will continue my coding in this workbook.
Pete -
Re: temporarily display userform
Pete, please remember to use Code tags when posting code to the forums. Thanks.
-
Re: temporarily display userform
Here's my next problem. I want to delay my code execution after the userform is closed. My code is:
in This Workbook:
UserForm1.Show vbModeless
Application.OnTime Now + TimeValue("00:00:06"), "UnloadIt"
My UseForm1 has only 2 text boxes, no commad buttons and is Private Sub UserForm_ InitializeModule 1 code is:
Public Sub UnloadIt()
Unload UserForm1
End SubMy problem is that if my next line of Workbook code is:
MsgBox " delay"
it doesn't allow the UserForm1 to go away and the message appears over it.
I think is is because I used vb ModeLess code but I can't slove this problem. I have tried to put Wait code in the Workbook, The Module and UserForm1 but nothing works.
Any help would be appreciated.
Pet -
Re: temporarily display userform
As Ritchie said - please wrap your code with code tags.
Code tags format and colourise the code making it easier to read and so follow the logic. The use of code tags is one of the rules you agreed to when you joined the forum.
Admittedly your code is not exactly complicated, but the rule applies to all code regardless.
You can edit your post by clicking the 'Edit Post' link below the message. Code tags are added by highlighting the code and clicking the '#' button on the toolbar above the edit window.
-
Re: temporarily display userform
cytop & Richie- sorry about not tagging my code. Will try to do it correctly in future posts. Probably the best thing for this post is to just restate my problem. For now, the code is not important it's a concept issue.
I've spent some time researching the load/show and unload/hide for userforms and what it gets down to is that I'm not knowledgable enough to load and unload (or whatever is syntax is needed) before the next userform is displayed. As stated in my #8 post, I could load and unload my userform 1 and 2 correctly but when the next line of code was # MsgBox "whatever", it occured on top of the userform. That's the krux of my problem. From what I read, it seem like it may have to do with loading into memory ( and how to use vbModeless and Initilize/Activate) but Im struggling to find the answer. Based on many other posts, many other are finding that using UserForms can be tricky.
Thanks for any help
Pete -
Participate now!
Don’t have an account yet? Register yourself now and be a part of our community!