+ Reply to Thread
Results 1 to 7 of 7

Running or bypassing code in a macro based on a certain condition...

Hybrid View

  1. #1
    Registered User
    Join Date
    01-11-2010
    Location
    tracy, ca
    MS-Off Ver
    Excel 2003
    Posts
    39

    Running or bypassing code in a macro based on a certain condition...

    I want to be able to run a certain chunk of code in the macro if a certain condition is true, or if that condition is not true then I want to bypass that chunk of code. It doesn't matter if this is accomplished with an if... then... else statement, or some other way. I'm just looking for a general method for accomplishing this. Thank you!
    Last edited by kenab1; 02-17-2010 at 03:55 PM.

  2. #2
    Forum Guru
    Join Date
    08-26-2007
    Location
    London
    Posts
    4,606

    Re: Running or bypassing code in a macro based on a certain condition...

    Your question is too vague to be able to provide a specific answer. But If/Then/Else sounds very sensible. If you want more tailored advice, post specific details and a workbook.

  3. #3
    Registered User
    Join Date
    01-11-2010
    Location
    tracy, ca
    MS-Off Ver
    Excel 2003
    Posts
    39

    Re: Running or bypassing code in a macro based on a certain condition...

    Ok here's the code:

    
        Cells(4, 3).Value = 1
        newNum = myNum
        For newNum = newNum To (o - 7)
            Cells(4, 3 + newNum).Value = 1
            newNum = newNum + myNum - 1
        Next newNum
        
        Cells(4, 4).Value = 2
        newNum = myNum
        For newNum = newNum To (o - 7)
            Cells(4, 4 + newNum).Value = 2
            newNum = newNum + myNum - 1
        Next newNum
        
        Cells(4, 5).Value = 3
        newNum = myNum
        For newNum = newNum To (o - 7)
            Cells(4, 5 + newNum).Value = 3
            newNum = newNum + myNum - 1
        Next newNum
    The first and second parts will always be run no matter what. I only want the third part to run if myNum is 3 or greater, otherwise I want the macro to skip it entirely. Thanks!

  4. #4
    Registered User
    Join Date
    01-11-2010
    Location
    tracy, ca
    MS-Off Ver
    Excel 2003
    Posts
    39

    Re: Running or bypassing code in a macro based on a certain condition...

    Actually I figured it out. Thanks though!

  5. #5
    Forum Guru
    Join Date
    08-26-2007
    Location
    London
    Posts
    4,606

    Re: Running or bypassing code in a macro based on a certain condition...

    I don't know but instructions are at the top of the page. Perhaps you could post your solution for the benefit of others.

  6. #6
    Registered User
    Join Date
    01-11-2010
    Location
    tracy, ca
    MS-Off Ver
    Excel 2003
    Posts
    39

    Re: Running or bypassing code in a macro based on a certain condition...

    I just added two If statements within the third section of the code, did the job nicely. Completely negates that section from running at all.

     
    
        If myNum = 3 Then Cells(4, 5).Value = 3
        newNum = myNum
        For newNum = newNum To (o - 7)
            If myNum = 3 Then Cells(4, 5 + newNum).Value = 3
            newNum = newNum + myNum - 1
        Next newNum

  7. #7
    Forum Guru
    Join Date
    08-26-2007
    Location
    London
    Posts
    4,606

    Re: Running or bypassing code in a macro based on a certain condition...

    Good work. Thanks.

+ 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