Consulting

Results 1 to 9 of 9

Thread: If Elseif to toggle

  1. #1
    VBAX Regular
    Joined
    Jan 2016
    Posts
    10
    Location

    If Elseif to toggle

    When right clicking within cell range A1 : D4, cell turns green.

    I want to expand further on this and add that when right clicking on green cell, cell turns yellow. And when right clicking on yellow, cell turns white.

    How would that look like?

    And is there a cleaner way by not using Elseif (like switch?)

    Private Sub Worksheet_BeforeRightClick(ByVal Target As Range, Cancel As Boolean)
        Dim rInt As Range
        Dim rCell As Range
           Set rInt = Intersect(Target, Range("A1:D4"))
        If Not rInt Is Nothing And Target.Interior.ColorIndex = 0 Then
            For Each rCell In rInt
            Next
        End If
        Set rInt = Nothing
        Set rCell = Nothing
    Cancel = True
    Target.Interior.ColorIndex = 4
    End Sub

  2. #2
    Knowledge Base Approver VBAX Wizard p45cal's Avatar
    Joined
    Oct 2005
    Location
    Surrey UK
    Posts
    5,875
    Private Sub Worksheet_BeforeRightClick(ByVal Target As Range, Cancel As Boolean)
    Dim rInt As Range, rCell As Range
    Set rInt = Intersect(Target, Range("A1:D4"))
    If Not rInt Is Nothing Then
    Cancel = True
      For Each rCell In rInt
        Select Case rCell.Interior.ColorIndex
          Case -4142, 0
            rCell.Interior.ColorIndex = 4
          Case 4
            rCell.Interior.ColorIndex = 6
          Case 6
            rCell.Interior.ColorIndex = -4142
        End Select
      Next
    End If
    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.

  3. #3

  4. #4
    Quote Originally Posted by Fluff View Post
    For those who might be interested, here is the code I posted over in the MrExcel forum where the OP cross-posted his question (see above quote by Fluff)...
    Private Sub Worksheet_BeforeRightClick(ByVal Target As Range, Cancel As Boolean) If Not Intersect(Target, Range("A1:D4")) Is Nothing Then Target.Interior.Color = Split(Trim(Split("16777215 65280 65535 16777215", CStr(Target.Interior.Color))(1)))(0) Cancel = True End If End Sub

  5. #5
    VBAX Regular
    Joined
    Dec 2018
    Posts
    23
    Location
    Private Sub Worksheet_BeforeRightClick(ByVal Target As Range, Cancel As Boolean) If Not Intersect(Target, Range("A1:D4")) Is Nothing Then Target.Interior.Color = Split(Trim(Split("16777215 65280 65535 16777215", CStr(Target.Interior.Color))(1)))(0) Cancel = True End If End Sub
    Rick

    As a fairly new VBAer, can you explain to me how the third statement works (with the two Split functions)? TIA
    Russ
    Last edited by MagPower; 03-24-2019 at 10:15 AM.

  6. #6
    VBAX Mentor 大灰狼1976's Avatar
    Joined
    Dec 2018
    Location
    SuZhou China
    Posts
    479
    Location
    Since there has been no reply for a long time, let me reply for you.
    Private Sub Worksheet_BeforeRightClick(ByVal Target As Range, Cancel As Boolean)
      If Not Intersect(Target, Range("A1:D4")) Is Nothing Then
        Target.Interior.Color = Split(Trim(Split("16777215 65280 65535 16777215", CStr(Target.Interior.Color))(1)))(0)
        Cancel = True
      End If
    End Sub
    For ease of explanation, Break it down into the following styles
    Private Sub Worksheet_BeforeRightClick(ByVal Target As Range, Cancel As Boolean)
      Dim rng As Range
      Set rng = Range("A1:D4")
      If Not Intersect(Target, rng) Is Nothing Then
        Dim s$, colorTgt$, arr, colorTmp$, colorRst&
        'Values of three colors,Duplicate values are meant to facilitate the acquisition of next values
        s = "16777215 65280 65535 16777215"
        'Converting color values to strings, example: green = "65280"
        colorTgt = CStr(Target.Interior.color)
        'Converting strings into one-dimensional arrays using colorTgt as a delimiter
        'example: colorTgt = "65280" ,then arr = array("16777215 "," 65535 16777215")
        arr = Split(s, colorTgt)
        'example: arr(1) = " 65535 16777215",The subscript of a one-dimensional array starts at 0
        colorTmp = arr(1)
        'example: colorTmp = "65535 16777215", Trim () Remove the header and tail spaces
        colorTmp = Trim(colorTmp)
        'Split(colorTmp) = array("65535","16777215"), Split(colorTmp)(0) = "65535"
        'example: colorRst = 65535 (yellow), Split () is delimited by space by default when delimiters are omitted
        colorRst = Split(colorTmp)(0)
        'Colour cell areas use colorRst
        rng.Interior.color = colorRst
        'Do not pop up the right-click menu
        Cancel = True
      End If
    End Sub

  7. #7
    Quote Originally Posted by 大灰狼1976 View Post
    Since there has been no reply for a long time, let me reply for you.
    Thank you for following up for me. I saw the request but was not sure how to explain my code so that the OP would understand. Your method was perfect and should help the OP understand my code. Thanks again for jumping in.

  8. #8
    VBAX Mentor 大灰狼1976's Avatar
    Joined
    Dec 2018
    Location
    SuZhou China
    Posts
    479
    Location
    Hi rothstein!
    You're welcome

  9. #9
    VBAX Regular
    Joined
    Dec 2018
    Posts
    23
    Location
    Thanks guys!

Posting Permissions

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