+ Reply to Thread
Results 1 to 6 of 6

Calling a Variable Subroutine

  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:
    Please Login or Register  to view this content.
    another approach would use Select Case
    Please Login or Register  to view this content.
    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.

    Please Login or Register  to view this content.
    Everything else can be done with these two.

  5. #5
    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:
    Please Login or Register  to view this content.
    Alternatively, you could do this:
    Please Login or Register  to view this content.
    A Select Case would also be good for this:
    Please Login or Register  to view this content.
    Last edited by Ikaabod; 09-27-2007 at 03:46 PM.

  6. #6
    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.

+ 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