+ Reply to Thread
Results 1 to 5 of 5

Using IF to restart macro when cell is a particular value

Hybrid View

  1. #1
    Registered User
    Join Date
    05-12-2009
    Location
    Udevalla, Sweden
    MS-Off Ver
    Excel 2007
    Posts
    85

    Using IF to restart macro when cell is a particular value

    Hi.

    Sorry for the bad thread title but I didnt know what else to write

    Anyway, here is my problem.
    I have a macro that runs through a bunch of external .xls files based on a list in my primary workbook.
    What I want to do happen is that if I put an "x" in the adjacent cell of my list, the macro should change a cell value and then go to the next item in the list.

    My code is becoming quite big so I wont post all of it but I think this will be enough.
    The blablablabla lines shows where I have omitted some code

    Do
    
    'Defines the name of the file for running external macro
    Dim sName As String
        sName = ActiveCell.Value
    
    blablabla
    
    If ActiveCell.Offset(0, -1).Value = "x" Then
    Sheets("S01-01-0103").Select
        Range("B3").Select
        ActiveCell.Offset(0, Sheets("Macros").Range("G1") + 1).Select
        ActiveCell.Offset(-1, 0).Select
        ActiveCell.Value = sName
    
        Sheets("Macros").Range("G1") = Sheets("Macros").Range("G1") + 1
        
        Sheets("Macros").Select
        Range("F1") = Range("F1") + 1
        
        ActiveCell.Offset(1, 0).Select
    
       Else
         
    End If
    
    
    blablalblalbla
    
    
    'Loops this macro until the list is empty
    Loop Until IsEmpty(ActiveCell.Offset(0, 0))

    All I want to know is what do I write after
    ActiveCell.Offset(1, 0).Select
    for the macro to restart, in other words, go back to "Do".
    I have everything else working fine,
    Last edited by Carlsbergen; 03-15-2010 at 04:17 AM. Reason: Corrected error

  2. #2
    Registered User
    Join Date
    05-12-2009
    Location
    Udevalla, Sweden
    MS-Off Ver
    Excel 2007
    Posts
    85

    Re: Using IF to restart macro when cell is a particular value

    Like so often, just after one posts the problem the solution comes to you.
    Maybe it is because you have to really formulate the problem in order to post and that helps your problem solving

    Anyway, for anyone interested, here is what I did:


    I added "StartMacro:" at the top of the macro.

    StartMacro:
    
    Do
    
    'Defines the name of the file for running external macro
    Dim sName As String
        sName = ActiveCell.Value

    Then I simply used the GoTo command like this:

        ActiveCell.Offset(1, 0).Select
    
    GoTo StartMacro
    
       Else
         
    End If

  3. #3
    Forum Guru (RIP) Marcol's Avatar
    Join Date
    12-23-2009
    Location
    Fife, Scotland
    MS-Off Ver
    Excel '97 & 2003/7
    Posts
    7,216

    Re: Using IF to restart macro when cell is a particular value

    Hi

    Something on this lines should work

     
         If Not someFlag Then
            'someFlag = True
            'Your macro name
        End If
    Where someFlag is a known condition.
    You could use a simple boolean but you must declare it outside your macro


    Dim someFlag as Boolean
    
    Sub Test()
          If Not someFlag Then
            'someFlag = True
            'Your macro name
        End If
    End Sub
    Beware of setting up an endless loop Test using a break line!

    Hope this helps

  4. #4
    Forum Guru (RIP) Marcol's Avatar
    Join Date
    12-23-2009
    Location
    Fife, Scotland
    MS-Off Ver
    Excel '97 & 2003/7
    Posts
    7,216

    Re: Using IF to restart macro when cell is a particular value

    Re: Your solution

    Unless you change some other condition, will that not just set up an endless loop?

  5. #5
    Registered User
    Join Date
    05-12-2009
    Location
    Udevalla, Sweden
    MS-Off Ver
    Excel 2007
    Posts
    85

    Re: Using IF to restart macro when cell is a particular value

    Quote Originally Posted by Marcol View Post
    Re: Your solution

    Unless you change some other condition, will that not just set up an endless loop?
    Hi.

    It doesnt loop like that with me at least.
    There is a whole lot more code in my macro than what I have written in this thread!

    There is another problem with this however.
    If the last cell in the list is such that the macro wants to jump to the next cell and restart, the macro will crash.
    If the last cell doesnt meet this condition, the macro will end in a normal way.
    This isnt a big deal with me since I want the macro to end there anyways, but if anyone else wanna use this method they should watch out for that.


    Macrol's solution is probably better
    Last edited by Carlsbergen; 03-19-2010 at 09:36 AM.

+ 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