+ Reply to Thread
Results 1 to 6 of 6

Calling a Variable Subroutine

Hybrid View

  1. #1
    Registered User
    Join Date
    09-27-2007
    Posts
    3

    Calling a Variable Subroutine

    Greetings fellow VBA'ers. I am relatively new to VBA programming but am working on some VBA code for work. I am using Excel 2003, and have really been struggling with structuring nested if/then/else logic properly. I often get "End If without Block If" errors such as in the case below. Can somebody help me with the proper structure? Thanks in advance.

    ----------------------------------------
    Yes, I know that the code is probably very inefficient. Right now, I'm just trying to get something that works.

    Sub Adjust()
    Dim Pattern As Integer
    Pattern = Sheets("Info").Range("R7").Value
    If Pattern = 1 Then Call OneCrew Else End If
    If Pattern = 2 Then Call TwoCrew Else End If
    If Pattern = 3 Then Call ThreeCrew Else End If
    If Pattern = 4 Then Call OneCrew Else End If
    If Pattern = 5 Then Call TwoCrew Else End If
    If Pattern = 6 Then Call ThreeCrew Else End If
    End Sub
    Last edited by WayneFontes; 09-27-2007 at 03:25 PM.

  2. #2
    Forum Contributor VBA Noob's Avatar
    Join Date
    04-25-2006
    Location
    London, England
    MS-Off Ver
    xl03 & xl 07(Jan 09)
    Posts
    11,988
    Please read forum rules below and then wrap your code (rule 5)



    VBA Noob
    _________________________________________


    Credo Elvem ipsum etian vivere
    _________________________________________
    A message for cross posters

    Please remember to wrap code.

    Forum Rules

    Please add to your signature if you found this link helpful. Excel links !!!

  3. #3
    Registered User
    Join Date
    09-27-2007
    Posts
    3
    Thanks, I'll edit my original post.

  4. #4
    Forum Expert mikerickson's Avatar
    Join Date
    03-30-2007
    Location
    Davis CA
    MS-Off Ver
    Excel 2011
    Posts
    6,229
    This should fix your code:
    Sub Adjust()
    Dim Pattern As Integer
    Pattern = Sheets("Info").Range("R7").Value
    If Pattern = 1 Then Call OneCrew 
    If Pattern = 2 Then Call TwoCrew 
    If Pattern = 3 Then Call ThreeCrew 
    If Pattern = 4 Then Call OneCrew
    If Pattern = 5 Then Call TwoCrew 
    If Pattern = 6 Then Call ThreeCrew 
    End Sub
    another approach would use Select Case
    Sub Adjust()
    Dim Pattern As Integer
    Pattern = Sheets("Info").Range("R7").Value
    Select Case Pattern
        Case Is = 1, 4
            Call OneCrew
        Case Is = 2, 5
            Call TwoCrew
        Case Is = 3, 6
            Call threecrew
    End Select
    End Sub
    Rather than trying to understand all the variations of Block If's (I'm not sure what an ElseIf statment does), I stick to the basic sytles that I understand.

    If condition Then instruction (non-block)
    
    AND 
    
    If ..... Then
       instructions one
    (optional)
    Else
       instructions two
    (halt optional)
    End If
    Everything else can be done with these two.

  5. #5
    Registered User
    Join Date
    09-27-2007
    Posts
    3
    A huge Thank You. With nobody here (at work) who can help me, you just saved me hours of banging my head on my desk. The Select/Case method works perfect.

  6. #6
    Valued Forum Contributor
    Join Date
    04-11-2006
    Posts
    407
    Since your If-Statements are on one line no "End if" is required. Since you are not doing anything for "else" else is also not required. This should work for you:
    Sub Adjust()
    Dim Pattern As Integer
    Pattern = Sheets("Info").Range("R7").Value
    If Pattern = 1 Then Call OneCrew
    If Pattern = 2 Then Call TwoCrew
    If Pattern = 3 Then Call ThreeCrew
    If Pattern = 4 Then Call OneCrew
    If Pattern = 5 Then Call TwoCrew
    If Pattern = 6 Then Call ThreeCrew
    End Sub
    Alternatively, you could do this:
    Sub Adjust()
      Dim Pattern As Integer
      Pattern = Sheets("Info").Range("R7").Value
      If Pattern = 1 or Pattern = 4 Then
          Call OneCrew
        ElseIf Pattern = 2 or Pattern = 5 Then
          Call TwoCrew
        ElseIf Pattern = 3 or Pattern = 6Then
          Call ThreeCrew
      End If
    End Sub
    A Select Case would also be good for this:
    Sub Adjust()
      Dim Pattern As Integer
      Pattern = Sheets("Info").Range("R7").Value
      Select Case Pattern
      Case 1, 4
        Call OneCrew
      Case 2, 5
        Call TwoCrew
      Case 3, 6
        Call ThreeCrew
      Case Else
        'None of the above, so continue without doing anything.
      End Select
    End Sub
    Last edited by Ikaabod; 09-27-2007 at 03:46 PM.

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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