+ Reply to Thread
Results 1 to 10 of 10

Trying to Repeat an If, Than, ElseIf code

  1. #1
    Registered User
    Join Date
    03-21-2013
    Location
    BC, Canada
    MS-Off Ver
    Excel 2007
    Posts
    5

    Trying to Repeat an If, Than, ElseIf code

    I'm new to writing macro code and I'm trying to repeat an If, Than, ElseIf code over multiple rows.

    Part of the code I've written is below. The actual code is quite long and goes through multiple variables on the same two columns (E & F). The rows I'm trying to repeat them on are 11-75. Any help would be appreciated as I can't seem to figure this one quite out.

    Please Login or Register  to view this content.
    Moderators note: code tags added for you - this time
    Last edited by FDibbins; 03-21-2013 at 07:20 PM.

  2. #2
    Administrator FDibbins's Avatar
    Join Date
    12-29-2011
    Location
    Duncansville, PA USA
    MS-Off Ver
    Excel 7/10/13/16/365 (PC ver 2310)
    Posts
    52,938

    Re: Trying to Repeat an If, Than, ElseIf code

    Hi and welcome to the forum

    Your post does not comply with Rule 3 of our Forum RULES. Use code tags around code.

    Posting code in [CODE]Please [url=https://www.excelforum.com/login.php]Login or Register [/url] to view this content.[/CODE] tags makes your code much easier to read and copy for testing, it also maintains VBA formatting.

    Highlight your code and click the # icon at the top of your post window. More information about these and other tags can be found here
    1. Use code tags for VBA. [code] Your Code [/code] (or use the # button)
    2. If your question is resolved, mark it SOLVED using the thread tools
    3. Click on the star if you think someone helped you

    Regards
    Ford

  3. #3
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643

    Re: Trying to Repeat an If, Than, ElseIf code

    Perhaps you could use Select Case.
    Please Login or Register  to view this content.
    If posting code please use code tags, see here.

  4. #4
    Registered User
    Join Date
    03-21-2013
    Location
    BC, Canada
    MS-Off Ver
    Excel 2007
    Posts
    5

    Re: Trying to Repeat an If, Than, ElseIf code

    Sorry about that. Thanks for fixing the code tags for me.

  5. #5
    Registered User
    Join Date
    03-21-2013
    Location
    BC, Canada
    MS-Off Ver
    Excel 2007
    Posts
    5

    Re: Trying to Repeat an If, Than, ElseIf code

    Thanks for the response Norie.
    Because I'm running multiple calculations (3 calculation for each province and territory) the If, Than, ElseIf seems to work the best.

    From what I've discovered I need to create a loop, but I don't know of the right kind of loop that will run the same code each row (11 - 75). I've included two provinces so you can see better what I'm trying to do.

    Please Login or Register  to view this content.

  6. #6
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643

    Re: Trying to Repeat an If, Than, ElseIf code

    Sorry, how would Select Case not work?

    Did you try the code I posted?

    PS I would actually suggest nested selected casses, with the outer one being for each province/territory.

    Please Login or Register  to view this content.
    Last edited by Norie; 03-22-2013 at 01:32 PM.

  7. #7
    Registered User
    Join Date
    03-21-2013
    Location
    BC, Canada
    MS-Off Ver
    Excel 2007
    Posts
    5

    Re: Trying to Repeat an If, Than, ElseIf code

    Yes, I did try the first code you posted... I'm going to try the second one right now and I'll get back to you.

    PS I do like the way Select Case looks.

  8. #8
    Registered User
    Join Date
    03-21-2013
    Location
    BC, Canada
    MS-Off Ver
    Excel 2007
    Posts
    5

    Re: Trying to Repeat an If, Than, ElseIf code

    GFE Expense Report macro testing.xlsm

    I've been working with the second code you posted, while it works well on the first row (11) it comes up with the run-time error:5 when I try to run the macro without info in the first (row 11). If I have info on more than one row (say row 11&12) it only runs on row 11. I've attached the work book so you can see what it looks like. Thanks for any and all help on this.

  9. #9
    Forum Contributor
    Join Date
    01-13-2013
    Location
    Oklahoma
    MS-Off Ver
    Office 2007 / Office 2010
    Posts
    123

    Re: Trying to Repeat an If, Than, ElseIf code

    Try this, be warned it's untested but it should do what you want.
    Please Login or Register  to view this content.

  10. #10
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643

    Re: Trying to Repeat an If, Than, ElseIf code

    Try this.
    Please Login or Register  to view this content.

+ 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