Consulting

Results 1 to 7 of 7

Thread: Switching 3 colors on Form

  1. #1

    Switching 3 colors on Form

    Hello,
    I wonder how the following code must be updated so the back color is:

    1st click: yellow
    2nd click: red
    3rd click: blue
    4th click: yellow
    etc.

    Sub btnMyButton_Click()
    '   The background color is gray at the beginnng
    If frmMyForm.BackColor <> vbYellow Or frmMyForm.BackColor = vbRed Then
      frmMyForm.BackColor = vbYellow
    Else
    frmMyForm.BackColor = vbRed
    End If
    End Sub
    Thank you, Malak
    Last edited by Aussiebear; 04-25-2023 at 06:45 PM. Reason: Added code tags to supplied code

  2. #2
    VBAX Mentor
    Joined
    Nov 2022
    Location
    The Great Land
    Posts
    337
    Location
    Need to save this count somewhere and refer to that saved value. Options:

    1. an unbound textbox

    2. a public/global variable

    3. TempVars

    Be aware that VBA variables lose value in run-time errors, however, TempVars do not.
    How to attach file: Reading and Posting Messages (vbaexpress.com), click Go Advanced below post edit window. To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  3. #3
    Moderator VBAX Master georgiboy's Avatar
    Joined
    Mar 2008
    Location
    Kent, England
    Posts
    1,198
    Location
    Maybe the below will help:
    Sub btnMyButton_Click()   
       '   The background color is gray at the beginnng
        Select Case frmMyForm.BackColor
            Case -2147483633 ' grey (none)
                frmMyForm.BackColor = vbYellow
            Case 65535 ' yellow
                frmMyForm.BackColor = vbRed
            Case 255 ' red
                frmMyForm.BackColor = vbBlue
            Case 16711680 ' blue
                frmMyForm.BackColor = vbYellow
        End Select
    End Sub
    Click here for a guide on how to add code tags
    Click here for a guide on how to mark a thread as solved
    Click here for a guide on how to upload a file with your post

    Excel 365, Version 2403, Build 17425.20146

  4. #4
    you may also try:
    Private Sub btnMyButton_Click()
        Static iSwitch As Integer
        Dim intValue As Integer
        Dim lngColor As Long
        
        iSwitch = iSwitch + 1
        
        ' there are only 3 for colors at the moment
        intValue = (iSwitch Mod 3) + 1
        
        'set the lngColor depending on intValue
        lngColor = Choose(intValue, vbBlue, vbYellow, vbRed)
        
        'The background color is gray at the beginnng
        frmMyForm.BackColor = lngColor
    End Sub
    Attached Files Attached Files

  5. #5
    Knowledge Base Approver VBAX Wizard p45cal's Avatar
    Joined
    Oct 2005
    Location
    Surrey UK
    Posts
    5,876
    To deal with any number of colours, adjust the first line:
    Private Sub btnMyButton_Click()
    myColours = Array(vbYellow, vbRed, vbBlue, vbGreen, vbMagenta, 16737894, vbCyan)    'adjust this line to suit, this is an example.
    'myColours = Array(vbYellow, vbRed, vbBlue) 'adjust this line to suit, this is the original request.
    With frmMyForm
      x = Application.Match(.BackColor, myColours, 0)    'is the current colour in the list? and if so, where?…
      If IsError(x) Then    'no it isn't:
        .BackColor = myColours(0)    'first colour in the list if it's not one of the existing colours.
      Else    'yes it is:
        .BackColor = myColours(x Mod (UBound(myColours) + 1))    'the next colour in the list.
      End If
    End With
    End Sub
    p45cal
    Everyone: If I've helped and you can't be bothered to acknowledge it, I can't be bothered to look at further posts from you.

  6. #6
    Thank you to everyone for providing your code.

  7. #7
    VBAX Mentor
    Joined
    Nov 2022
    Location
    The Great Land
    Posts
    337
    Location
    Oh, I get it now, colors are progressive so no need to track the click count. Duh!
    Glad you have working solution.
    How to attach file: Reading and Posting Messages (vbaexpress.com), click Go Advanced below post edit window. To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •