+ Reply to Thread
Results 1 to 12 of 12

Use of many alternatives after elseif

Hybrid View

  1. #1
    Registered User
    Join Date
    05-12-2020
    Location
    Lima,Peru
    MS-Off Ver
    2016
    Posts
    25

    Use of many alternatives after elseif

    Hello friends, thanks for your time. Please check the image.
    LINE01 BUTTON.jpg
    I want assign a specific color to the first line (from the first row) then I create this code using macro record.

    ElseIf Range("L5").Value = "Su<Ma" Then
        ActiveSheet.ChartObjects("Chart 2").Activate
        ActiveChart.FullSeriesCollection(1).Select
        With Selection.Format.Line
            .Visible = msoTrue
            .ForeColor.RGB = RGB(47, 204, 11)
            .Transparency = 0
        End With
        With Selection.Format.Line
            .Visible = msoTrue
            .Weight = 2.25
        End With
    But for the yellow color RGB(47, 204, 11) I have different conditions not only "Su<Ma", I want the code can considerate other options like "Su<Mo", "Su<Me", "Su<Ju", etc.
    How I can do that... In excel we have =IF(L5="Su<Ma, L5="Su<Mo", ...) but in macro I don't have an idea how it works.
    Please if you can teach me, it very useful.

    Obviously, if I can work directly in the ActiveSheet and do without using a button it be wonderful.

    Thanks so much.
    Last edited by Vladi108mb; 12-16-2020 at 07:18 AM.

  2. #2
    Valued Forum Contributor
    Join Date
    01-16-2012
    Location
    England
    MS-Off Ver
    MS 365
    Posts
    1,407

    Re: Use of many alternatives after elseif

    Not clear whether you want the line to change colour with each selection? If so, something like this?

    Dim c as range
    
    For each c in range (Cells containing the Day measures ) 
    
    If c= "Su<Ma" THEN
    Code to set colour
    
    ELSEIF c="Su<Mo" THEN
    Code to set different colour
    
    ELSEIF .c= "Su<Ju" THEN
    Code to set different colour
    
    (and so on)
    
    End if
    
    NEXT

    But if you are keeping the line at the same colour whatever the 'date' span, why have it as a Criteria at all (Other than perhaps checking the day cell is not blank)?

    Ochimus
    Last edited by Ochimus; 12-08-2020 at 08:49 PM.

  3. #3
    Forum Guru
    Join Date
    03-02-2006
    Location
    Los Angeles, Ca
    MS-Off Ver
    WinXP/MSO2007;Win10/MSO2016
    Posts
    12,663

    Re: Use of many alternatives after elseif

    Option Explicit
    Sub test()
         ActiveSheet.ChartObjects("Chart 2").Activate
         ActiveChart.FullSeriesCollection(1).Select
         
         With Selection.Format.Line
              .Visible = msoTrue
              .Transparency = 0
              .Weight = 2.25
              
              Select Case Range("L5").Value
                   Case Is = "Su<Ma"
                        .ForeColor.RGB = RGB(47, 204, 11)
                   Case Is = "Su<Mo"
                        .ForeColor.RGB = RGB(47, 204, 11)
                   Case Is = "Su<Me"
                        .ForeColor.RGB = RGB(47, 204, 11)
                   Case Is = "Su<Ju"
                        .ForeColor.RGB = RGB(47, 204, 11)
              End Select
         End With
    End Sub
    Ben Van Johnson

  4. #4
    Registered User
    Join Date
    05-12-2020
    Location
    Lima,Peru
    MS-Off Ver
    2016
    Posts
    25

    Re: Use of many alternatives after elseif

    Excellent!!!! You catch the idea!! So happy for reducing the lines of code!!!

  5. #5
    Forum Expert romperstomper's Avatar
    Join Date
    08-13-2008
    Location
    East Sussex, UK
    MS-Off Ver
    365, varying versions/builds
    Posts
    21,332

    Re: Use of many alternatives after elseif

    If you want the same colour for multiple values, you can put them on the same Case line:

    Select Case Range("L5").Value
                  Case "Su<Ma", "Su<Mo", "Su<Me", "Su<Ju"
                        .ForeColor.RGB = RGB(47, 204, 11)
              End Select
    or maybe you just need:

    If left$(Range("L5").Value, 3) = "Su<" then
    Everyone who confuses correlation and causation ends up dead.

  6. #6
    Registered User
    Join Date
    05-12-2020
    Location
    Lima,Peru
    MS-Off Ver
    2016
    Posts
    25

    Re: Use of many alternatives after elseif

    Hello Rorya, nice answer... then if I use first recommendation, can I do it in this way?

    Select Case Range("L5").Value
                  Case "Su<Ma", "Su<Mo", "Su<Me", "Su<Ju"
                        .ForeColor.RGB = RGB(47, 204, 11)
                  Case "Mo<Ma", "Mo<Mo", "Mo<Me", "Mo<Ju"
                        .ForeColor.RGB = RGB(0, 255, 234)
                  Case "Me<Ma", "Me<Mo", "Me<Me", "Me<Ju"
                        .ForeColor.RGB = RGB(255, 127, 0)
                  
              End Select
    But about If left$(Range("L5").Value, 3) = "Su<" then
    I like it so much because is a very more compact code... but how I can use it if I need other cases for the same L5: "Mo<", "Me<", "Ju<" etc.

    Thanks so much for your time!!
    Last edited by Vladi108mb; 12-16-2020 at 07:18 AM.

  7. #7
    Forum Expert romperstomper's Avatar
    Join Date
    08-13-2008
    Location
    East Sussex, UK
    MS-Off Ver
    365, varying versions/builds
    Posts
    21,332

    Re: Use of many alternatives after elseif

    You could do a similar thing:

    Select Case Left$(Range("L5").Value, 3)
    Case "Su<"
    .ForeColor.RGB = RGB(47, 204, 11)
    Case "Mo<"
    .ForeColor.RGB = RGB(0, 255, 234)
    Case "Me<"
    .ForeColor.RGB = RGB(255, 127, 0)
    
    End Select

  8. #8
    Registered User
    Join Date
    05-12-2020
    Location
    Lima,Peru
    MS-Off Ver
    2016
    Posts
    25

    Re: Use of many alternatives after elseif

    Wow! Perfect!! It let me use one unique button to fix colors for many lines at the same time.
    I will use it for many other parts of my project.

    Thank you so much!

  9. #9
    Registered User
    Join Date
    05-12-2020
    Location
    Lima,Peru
    MS-Off Ver
    2016
    Posts
    25

    Re: Use of many alternatives after elseif

    Hello Rorya, I am at this point... but now I need to apply the same conditions to the lines generated from L6, L7, L8, L9, L10, ... , L15
    What is the correct way? I try to change the range to (Range("L5:L15") but don't work


    Private Sub CommandButton1_Click()
    
         ActiveSheet.ChartObjects("Chart 2").Activate
         ActiveChart.FullSeriesCollection(1).Select
         
         With Selection.Format.Line
              .Visible = msoTrue
              .Transparency = 0
              .Weight = 2.25
              
        Select Case Left$(Range("L5").Value, 3)
        Case "Su", "Su<"
        .ForeColor.RGB = RGB(248, 255, 0)
        Case "Mo", "Mo<"
        .ForeColor.RGB = RGB(0, 255, 249)
        Case "Me", "Me<"
        .ForeColor.RGB = RGB(254, 173, 0)
        Case "Ma", "Ma<"
        .ForeColor.RGB = RGB(254, 0, 0)
        Case "Ju", "Ju<"
        .ForeColor.RGB = RGB(0, 106, 254)
        Case "Ve", "Ve<"
        .ForeColor.RGB = RGB(254, 0, 249)
        Case "Sa", "Sa<"
        .ForeColor.RGB = RGB(56, 6, 184)
        Case "Ra", "Ra<"
        .ForeColor.RGB = RGB(109, 21, 78)
        Case "Ke", "Ke<"
        .ForeColor.RGB = RGB(65, 53, 84)
        Case "As", "As<"
        .ForeColor.RGB = RGB(11, 216, 41)
    
    End Select
                 End With
    End Sub
    
    ---------------- Next, I add ActiveChart.FullSeriesCollection for 11 lines in total. 
    
     ActiveSheet.ChartObjects("Chart 2").Activate
         ActiveChart.FullSeriesCollection(1).Select
         ActiveChart.FullSeriesCollection(2).Select
         ActiveChart.FullSeriesCollection(3).Select
         ActiveChart.FullSeriesCollection(4).Select
         ActiveChart.FullSeriesCollection(5).Select
         ActiveChart.FullSeriesCollection(6).Select
         ActiveChart.FullSeriesCollection(7).Select
         ActiveChart.FullSeriesCollection(8).Select
         ActiveChart.FullSeriesCollection(9).Select
         ActiveChart.FullSeriesCollection(10).Select
    and try again with (Range("L5:L15") but don't work!

    Please, I need again your expertise. Thanks for your time.
    Last edited by Vladi108mb; 12-16-2020 at 07:17 AM.

  10. #10
    Forum Guru
    Join Date
    03-02-2006
    Location
    Los Angeles, Ca
    MS-Off Ver
    WinXP/MSO2007;Win10/MSO2016
    Posts
    12,663

    Re: Use of many alternatives after elseif

    Vladi108mb, Code tag are required....

  11. #11
    Forum Guru
    Join Date
    03-02-2006
    Location
    Los Angeles, Ca
    MS-Off Ver
    WinXP/MSO2007;Win10/MSO2016
    Posts
    12,663

    Re: Use of many alternatives after elseif

    Option Explicit
    Private Sub CommandButton1_Click()
         Dim CollectionIndex As Long
         
         ActiveSheet.ChartObjects("Chart 2").Activate
         
         For CollectionIndex = 1 To ActiveChart.FullSeriesCollection.Count
              ActiveChart.FullSeriesCollection(CollectionIndex).Select
              With Selection.Format.Line
                   .Visible = msoTrue
                   .Transparency = 0
                   .Weight = 2.25
                   
                   Select Case Left$(Range("L4").Offset(RowOffset:=CollectionIndex).Value, 3)
                        Case "Su", "Su<"
                             .ForeColor.RGB = RGB(248, 255, 0)
                        Case "Mo", "Mo<"
                             .ForeColor.RGB = RGB(0, 255, 249)
                        Case "Me", "Me<"
                             .ForeColor.RGB = RGB(254, 173, 0)
                        Case "Ma", "Ma<"
                             .ForeColor.RGB = RGB(254, 0, 0)
                        Case "Ju", "Ju<"
                             .ForeColor.RGB = RGB(0, 106, 254)
                        Case "Ve", "Ve<"
                             .ForeColor.RGB = RGB(254, 0, 249)
                        Case "Sa", "Sa<"
                             .ForeColor.RGB = RGB(56, 6, 184)
                        Case "Ra", "Ra<"
                             .ForeColor.RGB = RGB(109, 21, 78)
                        Case "Ke", "Ke<"
                             .ForeColor.RGB = RGB(65, 53, 84)
                        Case "As", "As<"
                             .ForeColor.RGB = RGB(11, 216, 41)
                   End Select
              End With
         Next CollectionIndex
    End Sub

  12. #12
    Forum Expert romperstomper's Avatar
    Join Date
    08-13-2008
    Location
    East Sussex, UK
    MS-Off Ver
    365, varying versions/builds
    Posts
    21,332

    Re: Use of many alternatives after elseif

    It may also be that you can reduce the checks to just the first two letters:

    Select Case Left$(Range("L4").Offset(RowOffset:=CollectionIndex).Value, 2)
                        Case "Su"
    and so on.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. IF and ElseIf
    By DCRAIG3389 in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 12-08-2016, 12:30 PM
  2. [SOLVED] When ElseIf condition is met, the statements under elseif are not happening.
    By excelkann in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 04-07-2016, 12:40 PM
  3. If / ElseIf within For Each?
    By pwells in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 06-26-2014, 10:35 AM
  4. Code stepping in to elseif statement when elseif is definitely not true
    By DFrank231 in forum Excel Programming / VBA / Macros
    Replies: 9
    Last Post: 02-13-2013, 05:31 PM
  5. If Elseif elseif problem
    By tjbillund in forum Excel General
    Replies: 3
    Last Post: 08-10-2011, 07:02 AM
  6. [SOLVED] if elseif
    By flow23 in forum Excel General
    Replies: 0
    Last Post: 11-14-2005, 10:15 AM
  7. [SOLVED] Re: IF..Then..ELSE.. ELSEIF
    By rjamison in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 06-13-2005, 08:05 PM

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1