Jump to content

Change userform colour every 20ms in VBA..


Recommended Posts

Posted

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..

Posted

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

Posted

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 :)

Posted

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.

Posted

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?

Posted

:offtopic:

 

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.

Posted

That's no worries, its always best to make it clear i reckon :)

Posted
That's no worries, its always best to make it clear i reckon :)

 

Cheers dude :beer:

Posted

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

Posted

You should call the Repaint method after change label's color and not before.

Posted

Thats done it. Brilliant, thanks :) and thanks for all your help

Posted

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.

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.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...