Page 1 of 2 12 LastLast
Results 1 to 10 of 11

Thread: vba If condition help..
Thanks Thanks: 0 Likes Likes: 0

  1. #1
    Board Regular
    Join Date
    Feb 2009
    Posts
    182
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Default vba If condition help..

    Hi Team,


    Is there a way to shorten this code used in if condition.
    If Sheet1.Cells(i, 1).Value = "Green" Or Sheet1.Cells(i, 1).Value = "Red" Or Sheet1.Cells(i, 1).Value = "Blue" Then

    like If Sheet1.Cells(i, 1).Value = Array("Green", "Red", "Blue") Then


    Thanks in advance



    Regards,
    mg

  2. #2
    MrExcel MVP
    Moderator
    Peter_SSs's Avatar
    Join Date
    May 2005
    Location
    Macksville, Australia
    Posts
    41,408
    Post Thanks / Like
    Mentioned
    100 Post(s)
    Tagged
    21 Thread(s)

    Default Re: vba If condition help..

    There are many ways and the best one will depend on your individual circumstances (including for example whether the case of the text has to match exactly) & your preference but here are 3 ways to consider

    Code:
    If IsNumeric(Application.Match(Sheet1.Cells(i, 1).Value, Array("Green", "Red", "Blue"), 0)) Then
      MsgBox "Yes"
    Else
      MsgBox "No"
    End If
    
    
    If InStr(1, "|Green|Red|Blue|", "|" & Sheet1.Cells(i, 1).Value & "|", 1) > 0 Then
     MsgBox "Yes"
    Else
      MsgBox "No"
    End If
    
    
    Select Case LCase(Sheet1.Cells(i, 1).Value)
      Case "green", "red", "blue"
        MsgBox "Yes"
      Case Else
        MsgBox "No"
    End Select
    Last edited by Peter_SSs; Oct 13th, 2019 at 07:18 PM.
    Hope this helps, good luck.
    Peter
    Excel 365 - Windows 10
    - Want to help your helpers by posting a small, copyable, screen shot directly in your post? Look here
    - If posting vba code, please use Code Tags - like this [code]Paste code here[/code] - or use the # key in the Reply window
    - Read: Forum Rules, Forum Use Guidelines, & FAQ

  3. #3
    Board Regular
    Join Date
    Mar 2015
    Location
    Syria
    Posts
    331
    Post Thanks / Like
    Mentioned
    6 Post(s)
    Tagged
    0 Thread(s)

    Default Re: vba If condition help..

    Hi
    what about
    Code:
    If Sheet1.Cells(1, 1).Value = Green Or Red Or Blue Then

  4. #4
    MrExcel MVP
    Moderator
    Peter_SSs's Avatar
    Join Date
    May 2005
    Location
    Macksville, Australia
    Posts
    41,408
    Post Thanks / Like
    Mentioned
    100 Post(s)
    Tagged
    21 Thread(s)

    Default Re: vba If condition help..

    Quote Originally Posted by mohadin View Post
    Hi
    what about
    Code:
    If Sheet1.Cells(1, 1).Value = Green Or Red Or Blue Then
    Did you try that?
    Hope this helps, good luck.
    Peter
    Excel 365 - Windows 10
    - Want to help your helpers by posting a small, copyable, screen shot directly in your post? Look here
    - If posting vba code, please use Code Tags - like this [code]Paste code here[/code] - or use the # key in the Reply window
    - Read: Forum Rules, Forum Use Guidelines, & FAQ

  5. #5
    Board Regular
    Join Date
    Mar 2015
    Location
    Syria
    Posts
    331
    Post Thanks / Like
    Mentioned
    6 Post(s)
    Tagged
    0 Thread(s)

    Default Re: vba If condition help..

    Yep I did

  6. #6
    MrExcel MVP
    Moderator
    Peter_SSs's Avatar
    Join Date
    May 2005
    Location
    Macksville, Australia
    Posts
    41,408
    Post Thanks / Like
    Mentioned
    100 Post(s)
    Tagged
    21 Thread(s)

    Default Re: vba If condition help..

    Quote Originally Posted by mohadin View Post
    Yep I did
    Well in that case you would have found that it does not work.
    Hope this helps, good luck.
    Peter
    Excel 365 - Windows 10
    - Want to help your helpers by posting a small, copyable, screen shot directly in your post? Look here
    - If posting vba code, please use Code Tags - like this [code]Paste code here[/code] - or use the # key in the Reply window
    - Read: Forum Rules, Forum Use Guidelines, & FAQ

  7. #7
    Board Regular
    Join Date
    Mar 2015
    Location
    Syria
    Posts
    331
    Post Thanks / Like
    Mentioned
    6 Post(s)
    Tagged
    0 Thread(s)

    Default Re: vba If condition help..

    Nop
    It works OK

  8. #8
    MrExcel MVP
    Moderator
    Peter_SSs's Avatar
    Join Date
    May 2005
    Location
    Macksville, Australia
    Posts
    41,408
    Post Thanks / Like
    Mentioned
    100 Post(s)
    Tagged
    21 Thread(s)

    Default Re: vba If condition help..

    Quote Originally Posted by mohadin View Post
    Nop
    It works OK
    Really?? I think you should check again.

    Here's my test of your code line
    Code:
    Sub Test()
      Dim i As Long
      
      For i = 1 To 3
        If Sheet1.Cells(i, 1).Value = Green Or Red Or Blue Then
          Sheet1.Cells(i, 2).Value = "Green or Red or Blue"
        Else
          Sheet1.Cells(i, 2).Value = "XXX"
        End If
      Next i
    End Sub
    Here is the result

    Sheet1

    AB
    1GreenXXX
    2OrangeXXX
    3RedXXX


    Excel tables to the web >> Excel Jeanie HTML 4
    Hope this helps, good luck.
    Peter
    Excel 365 - Windows 10
    - Want to help your helpers by posting a small, copyable, screen shot directly in your post? Look here
    - If posting vba code, please use Code Tags - like this [code]Paste code here[/code] - or use the # key in the Reply window
    - Read: Forum Rules, Forum Use Guidelines, & FAQ

  9. #9
    Board Regular
    Join Date
    Feb 2009
    Posts
    182
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Default Re: vba If condition help..

    Hi Peter,
    Thank you so much for giving multiple option, I will practice on it and improve my coding.


    Regards,
    mg

  10. #10
    Board Regular
    Join Date
    Mar 2015
    Location
    Syria
    Posts
    331
    Post Thanks / Like
    Mentioned
    6 Post(s)
    Tagged
    0 Thread(s)

    Default Re: vba If condition help..

    Ooops
    I must have tested in a wrong way
    Terribly sorry
    And thank you Mr. peter for letting me know my fault

Some videos you may like

User Tag List

Tags for this Thread

Like this thread? Share it with others

Like this thread? Share it with others

Posting Permissions

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