+ Reply to Thread
Results 1 to 8 of 8

Loop Macro with Counter

  1. #1
    Registered User
    Join Date
    10-05-2011
    Location
    Sydney, Australia
    MS-Off Ver
    Office 365 MS and mac
    Posts
    26

    Loop Macro with Counter

    Hi everybody,

    I am trying to write a macro that will repeat based on the number of records in a database. The macro itself will just call 2 other macros which are already written and tested.

    I can calculate the number of records in the database using a formula and put this in a field called "Max".

    The macro needs to start by putting 1 in a field called "Ref" then call the 2 macros (say Macro1 and Macro2) and then add 1 to "Ref" and repeat.

    This should all be repeated until "Ref" equals "Max" and then quit.

    Hope that is easy to understand.

    Thanks for any help.

    Rick

  2. #2
    Forum Expert
    Join Date
    12-14-2012
    Location
    London England
    MS-Off Ver
    MS 365 Office Suite.
    Posts
    8,448

    Re: Loop Macro with Counter

    Please Login or Register  to view this content.
    My General Rules if you want my help. Not aimed at any person in particular:

    1. Please Make Requests not demands, none of us get paid here.

    2. Check back on your post regularly. I will not return to a post after 4 days.
    If it is not important to you then it definitely is not important to me.

  3. #3
    Registered User
    Join Date
    10-05-2011
    Location
    Sydney, Australia
    MS-Off Ver
    Office 365 MS and mac
    Posts
    26

    Re: Loop Macro with Counter

    Thanks - that looks pretty easy. What about the part where I need to put 1 in "Ref", run the macros and then increment it by 1 each time until it gets to "Max"

  4. #4
    Forum Expert
    Join Date
    12-14-2012
    Location
    London England
    MS-Off Ver
    MS 365 Office Suite.
    Posts
    8,448

    Re: Loop Macro with Counter

    You are using ref as a counter. I always use count.

    Please Login or Register  to view this content.

    Does all your looping.

  5. #5
    Registered User
    Join Date
    10-05-2011
    Location
    Sydney, Australia
    MS-Off Ver
    Office 365 MS and mac
    Posts
    26

    Re: Loop Macro with Counter

    Sorry - I am either being unclear or stupid.

    I need to have a cell in the worksheet which is the record number as this is used to look up data from the database and generate and print a file.

    So the macro needs to start this off at 1 and increment it by 1 for each cycle of the macro until it gets to Max.

    Thanks

  6. #6
    Forum Expert
    Join Date
    12-14-2012
    Location
    London England
    MS-Off Ver
    MS 365 Office Suite.
    Posts
    8,448

    Re: Loop Macro with Counter

    Please Login or Register  to view this content.

  7. #7
    Registered User
    Join Date
    10-05-2011
    Location
    Sydney, Australia
    MS-Off Ver
    Office 365 MS and mac
    Posts
    26

    Re: Loop Macro with Counter

    Thanks for this. Sorry but I don't think this solves my problem. I will try and explain a little better.

    I have a workbook consisting of two sheets.

    One sheet contains a database consisting of rows of data. I use this data to populate an invoice in the second sheet which i then save as a pdf and print.

    So I need to cycle through each record in the database in turn until I get to the last record.

    The invoice has a reference field (named "ref") which is used to lookup data from the relevant record in the database. It also has a field named "max" which is the reference for the last record.

    I have sub macros to save the pdf and then print it.

    So the macro needs to do the following:

    1. Go to "ref" and set to zero.
    2. Go to "ref" and increment it by 1
    3. If "ref" is < "max" then run macro1 and macro 2.
    4. Loop back to 2

    Thanks

  8. #8
    Forum Expert
    Join Date
    06-12-2012
    Location
    Ridgefield Park, New Jersey
    MS-Off Ver
    Excel 2003,2007,2010
    Posts
    10,241

    Re: Loop Macro with Counter

    Maybe:

    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)

Similar Threads

  1. For loop counter reset
    By amphinomos in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 04-03-2014, 10:59 AM
  2. [SOLVED] Counter in a loop
    By jonathanrgz in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 01-11-2013, 05:46 PM
  3. loop counter not recognised
    By Dave H9 in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 05-13-2011, 02:53 AM
  4. How can I avoid changing a loop counter within a loop?
    By broro183 in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 10-26-2009, 07:59 PM
  5. [SOLVED] Counter variable in For Loop
    By [email protected] in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 06-08-2006, 02:00 PM

Tags for this Thread

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