Flashing controls on a userform

RawlinsCross

Active Member
Joined
Sep 9, 2016
Messages
437
Good day,

I have some controls on a userform that have their BackColor property set to 'vbRed'. I want these controls to blink on and off. Eventually there should be an acknowledge button to stop it but for now I'm working on getting these controls to blink. Here's my attempt. I'm getting an error:

"Cannot the run the maco {macro location string of 'StartFlashing'}. The macro may not be available in this workbook or all macros may be disabled"

The idea is to identify all controls that have a backcolor vbRed (not all do) and store their names in an array. I checked the array, they're all in there. I can set the controls 'visible' property to False but looks like I can't re-run the Flashing subroutine. Any thoughts?

Setup Code
VBA Code:
'member Array for Userform
Private msFlashArray() As Variant

Private Sub CheckControls()

For Each ctrl In Me.Controls
    If ctrl.BackColor = vbRed Then
        i = i + 1
        ReDim Preserve msFlashArray(1 To i)
        msFlashArray(i) = ctrl.Name
    End If
Next ctrl
' I confirmed that the correct controls were indeed listed in the FlashArray
Call StartFlashing
Application.OnTime (Now + TimeValue("00:00:01")), "StartFlashing"

End Sub

Flashing Code
VBA Code:
Private Sub StartFlashing()

Dim item As Variant

For Each item In msFlashArray
    If Me.Controls(item).Visible = True Then
        Me.Controls(item).Visible = False
    ElseIf Me.Controls(item).Visible = False Then
        Me.Controls(item).Visible = True
   End If
Next item

End Sub
 
Last edited:
FINAL CODE

Userform Code (no changes)

VBA Code:
Private Sub CheckControls
Dim i As Integer
Dim ctrl As Control

For Each ctrl In Me.Controls
   If ctrl.BackColor = vbRed Then
      i = i + 1
       ReDim Preserve sFlashArray_PVar(1 To i)
       sFlashArray_PVar(i) = ctrl.Name
   End If
Next ctrl

Call StartFlashing(True, UTWells)

End Sub


Module Code
VBA Code:
'Public Variables
Public sFlashArray_PVar() As Variant
Public myForm_PVar As UserForm


Public Sub StartFlashing(OnOff As Boolean, myForm As UserForm)

Dim item As Variant
Dim RunWhen As Double

'In case sFlashArray is empty, IsArrayAllocated is a function that tests array
If Not IsArrayAllocated(sFlashArray_PVar) Then Exit Sub

For Each item In sFlashArray_PVar
      myForm.Controls(item).Visible = Not myForm.Controls(item).Visible
Next item

Set myForm_PVar = myForm

'Added variable here so that we can effectively shut the OnTime down
RunWhen = Now + TimeValue("00:00:01")
If OnOff Then
      Application.OnTime RunWhen, "TempCall"
Else
     'Stop flashing and re-establish all red controls to be visible
      For Each item In sFlashArray_PVar
           myForm.Controls(item).Visible = True
      Next item
      Application.OnTime RunWhen, "TempCall", Schedule:=False
      Erase sFlashArray_PVar 'delete FlashArray
End If

End Sub


Public Sub TempCall()
Call StartFlashing(True, myForm_PVar)
End Sub


Shutdown Code (Command Button on Userform)
VBA Code:
Private Sub Acknowledge_Click()
Call StartFlashing(False, Me)
End Sub


IsArrayAllocatedFunction (www.cpearson.com)
VBA Code:
Function IsArrayAllocated(Arr As Variant) As Boolean

On Error GoTo eh:

IsArrayAllocated = IsArray(Arr) And Not IsError(LBound(Arr, 1)) And LBound(Arr, 1) <= UBound(Arr, 1)
Exit Function

eh:
IsArrayAllocated = False

End Function
 
Upvote 0

Excel Facts

Easy bullets in Excel
If you have a numeric keypad, press Alt+7 on numeric keypad to type a bullet in Excel.

Forum statistics

Threads
1,214,908
Messages
6,122,186
Members
449,071
Latest member
cdnMech

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top