hardwired Posted February 20, 2013 Posted February 20, 2013 Hey all, Using VBA, is there a way to change the colour of a userform every 20ms or so - thus making a flashing, multicoloured changing form. I'm just making a joke program at work and want the userform to rapidly change colours party / celebration / fireworks-esque.. Quote
MSasu Posted February 20, 2013 Posted February 20, 2013 You can try something like this, but please pay attention that will have to wait until the "fireworks" were finished to be able to interact with the form. Private Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long) Private Sub UserForm_Activate() Dim listColors As Variant Dim waitTime As Variant Dim index As Integer listColors = Array(&HFF&, &H80FF&, &HFFFF&, &HFF00&, &HFFFF00, &HFF0000, &HFF00FF) For index = 0 To UBound(listColors) UserForm1.BackColor = listColors(index) UserForm1.Repaint Sleep 20 Next index End Sub Quote
hardwired Posted February 20, 2013 Author Posted February 20, 2013 Hey Mircea, Thanks that works perfectly. I do want to add a loop on the whole thing now so it loops until the user closes the form but i'm sure i can work that out. Thank you for the code Quote
MSasu Posted February 20, 2013 Posted February 20, 2013 You're entirely welcome. Regarding the continuous loop, please pay attention that it may force the user to use in order to dismiss the form. I'm not sure if can trigger other event (i.e. button press) while the "fireworks" were executed. Quote
hardwired Posted February 20, 2013 Author Posted February 20, 2013 yeah thats what i thought. I was thinking of adding an error trap that accepts ESC as a cancel out of the trap or the loop but scratching my head a bit. Is that a way to do it? Quote
BlackBox Posted February 20, 2013 Posted February 20, 2013 I'd just like to pop in and say thank you for including 'VBA' in the thread title (so I know not to read through the entire thread to find that out), especially since this thread in the forum feedback has not gotten any traction. Quote
hardwired Posted February 20, 2013 Author Posted February 20, 2013 That's no worries, its always best to make it clear i reckon Quote
BlackBox Posted February 20, 2013 Posted February 20, 2013 That's no worries, its always best to make it clear i reckon Cheers dude Quote
hardwired Posted February 21, 2013 Author Posted February 21, 2013 Hey Mircea (or anybody else who can help) I've adapted the code to this and it works for what i need except for one thing - the loop changes the colour of both the label1 and userform colour, however they are not in sync - i want the colour of the label to be the same as the form everytime it changes colour.. Any ideas? Private Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long) Private Sub UserForm_Activate() Dim listColors As Variant Dim waitTime As Variant Dim index As Integer Dim counter medalform.BackColor = &H800000 Label1.BackColor = &H800000 counter = 1 listColors = Array(&HFF&, &H80FF&, &HFFFF&, &HFF00&, &HFFFF00, &HFF0000, &HFF00FF) Do While counter < 5 counter = counter + 1 For index = 0 To UBound(listColors) medalform.BackColor = listColors(index) medalform.Repaint Label1.BackColor = listColors(index) Sleep 200 Next index Loop medalform.BackColor = &H800000 Label1.BackColor = &H800000 End Sub Quote
MSasu Posted February 21, 2013 Posted February 21, 2013 You should call the Repaint method after change label's color and not before. Quote
hardwired Posted February 21, 2013 Author Posted February 21, 2013 Thats done it. Brilliant, thanks and thanks for all your help Quote
MSasu Posted February 21, 2013 Posted February 21, 2013 Gald to hear that is solved! You're entirely welcome! The explanation is that, even if is a method of the form, Repaint apply to all form'e childs. Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.