Consulting

Results 1 to 6 of 6

Thread: Multiple similar If Statements

  1. #1

    Multiple similar If Statements

    If you have many if statements which are similar, would you use "Select Case", another function, or something else?
    My code is in progress but looks something like this.
    The only variation is the character, which may be "A", "B", "C", etc.

    If (CLoc > 0 And InStr(1, TextLeft, "A") <> 0 And CharLeft = "A") Then
        Cell.Characters(.SelStart + 1, 0).Insert "Test text"
    End If
    If (CLoc > 0 And InStr(1, TextLeft, "B") <> 0 And CharLeft = "B") Then
        Cell.Characters(.SelStart + 1, 0).Insert "Test text"
    End If
    If (CLoc > 0 And InStr(1, TextLeft, "C") <> 0 And CharLeft = "C") Then
        Cell.Characters(.SelStart + 1, 0).Insert "Test text"
    End If
    'etc
    
    'Attempt with Case Select
    Dim Char As String
    Select Case Char
        Case "A", "B", "C" 'etc
            If (CLoc > 0 And InStr(1, TextLeft, Char) <> 0 And CharLeft = Char) Then
               Cell.Characters(.SelStart + 1, 0).Insert "Test text"
           End If   
    Case Else
        Msgbox "No"
    End Select

  2. #2
    Moderator VBAX Master georgiboy's Avatar
    Joined
    Mar 2008
    Location
    Kent, England
    Posts
    1,194
    Location
    Think i would be tempted to create a function like below:
    Sub test()    
        Dim var As Variant
        var = Split("A,B,C,D", ",")
        
        For x = 0 To UBound(var)
            y = CheckString(Sheet1.Range("A1"), var(x))
        Next x
        
    End Sub
    
    
    Function CheckString(rng As Range, srchStr)
        If InStr(1, rng.Value, srchStr) <> 0 Then
            rng.Characters(InStr(1, rng.Value, srchStr) + 1, 0).Insert "Test text"
        End If
    End Function
    Hope this helps
    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

  3. #3
    Thanks. I will go in the direction of using a function then.

  4. #4
    Actually it appears that Select Case can be used. Is there a way to make the code work?

  5. #5
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,726
    Location
    How about something like this?


    Option Explicit
    
    Sub test()
        Dim i As Long, c As Long, CLoc As Long
        Dim s As String
        
        
        For c = 65 To 90    '   A to Z
            s = Chr(c)
            If CLoc > 0 Then
                If InStr(1, TextLeft, s) <> 0 And charleft = s Then
                    Cell.Characters(.SelStart + 1, 0).Insert "Test text"
                End If
            End If
        Next c
    
    End Sub
    ---------------------------------------------------------------------------------------------------------------------

    Paul


    Remember: Tell us WHAT you want to do, not HOW you think you want to do it

    1. Use [CODE] ....[/CODE ] Tags for readability
    [CODE]PasteYourCodeHere[/CODE ] -- (or paste your code, select it, click [#] button)
    2. Upload an example
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) / Upload Files / Done
    3. Mark the thread as [Solved] when you have an answer
    Thread Tools (on the top right corner, above the first message)
    4. Read the Forum FAQ, especially the part about cross-posting in other forums
    http://www.vbaexpress.com/forum/faq...._new_faq_item3

  6. #6
    Great, thanks, I'll give this a try too.

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
  •