+ Reply to Thread
Results 1 to 12 of 12

loop a sub routine

  1. #1
    Registered User
    Join Date
    11-02-2013
    Location
    ajax
    MS-Off Ver
    Excel 2010
    Posts
    32

    loop a sub routine

    Here is what I am trying to do:
    I have 30 duplicate worksheets that I enter data every 3 months. I have a macro that clears certain fields and copies data from one field to other and I run this macro after selecting each sheet one at a time. How can I create a macro that will run this macro from the 1st sheet through to the 30th sheet in one click. My macro is called Clear_data.
    Any help would be greatly appreciated!

  2. #2
    Forum Contributor
    Join Date
    03-12-2013
    Location
    Chicago, USA
    MS-Off Ver
    Excel 2007
    Posts
    230

    Re: loop a sub routine

    Do you clear the exact same data on each worksheet? If so, you can write a loop to cycle through each worksheet and clear the same data.

  3. #3
    Registered User
    Join Date
    11-02-2013
    Location
    ajax
    MS-Off Ver
    Excel 2010
    Posts
    32

    Re: loop a sub routine

    Yes I do. How would write this code?

  4. #4
    Forum Contributor
    Join Date
    03-12-2013
    Location
    Chicago, USA
    MS-Off Ver
    Excel 2007
    Posts
    230

    Re: loop a sub routine

    Do you want to loop through EVERY single worksheet or only some of them (for example, the last 20 of 25 worksheets)?

  5. #5
    Registered User
    Join Date
    11-02-2013
    Location
    ajax
    MS-Off Ver
    Excel 2010
    Posts
    32

    Re: loop a sub routine

    on every worksheet.

  6. #6
    Forum Contributor
    Join Date
    03-12-2013
    Location
    Chicago, USA
    MS-Off Ver
    Excel 2007
    Posts
    230

    Re: loop a sub routine

    Insert your code into the area where it says "Perform code here"

    Please Login or Register  to view this content.

  7. #7
    Registered User
    Join Date
    11-02-2013
    Location
    ajax
    MS-Off Ver
    Excel 2010
    Posts
    32

    Re: loop a sub routine

    I tried it but it stayed on the first worksheet and kept repeating the macro.

  8. #8
    Forum Contributor
    Join Date
    03-12-2013
    Location
    Chicago, USA
    MS-Off Ver
    Excel 2007
    Posts
    230

    Re: loop a sub routine

    Did you be sure to include the "Option Explicit" part?

  9. #9
    Forum Contributor
    Join Date
    03-12-2013
    Location
    Chicago, USA
    MS-Off Ver
    Excel 2007
    Posts
    230

    Re: loop a sub routine

    Try adding this worksheet activate line:

    Please Login or Register  to view this content.

  10. #10
    Registered User
    Join Date
    11-02-2013
    Location
    ajax
    MS-Off Ver
    Excel 2010
    Posts
    32

    Re: loop a sub routine

    ws.activate did the trick.

    Thanks!!

  11. #11
    Registered User
    Join Date
    11-02-2013
    Location
    ajax
    MS-Off Ver
    Excel 2010
    Posts
    32

    Re: loop a sub routine

    One little problem, I have some hidden sheets that I don't want to involve so it is creating a problem. Is there a way to ignore these?

  12. #12
    Forum Contributor
    Join Date
    03-12-2013
    Location
    Chicago, USA
    MS-Off Ver
    Excel 2007
    Posts
    230

    Re: loop a sub routine

    I've never tried this before, but I found this possible solution:

    Please Login or Register  to view this content.

  13. #13
    Registered User
    Join Date
    11-02-2013
    Location
    ajax
    MS-Off Ver
    Excel 2010
    Posts
    32

    Re: loop a sub routine

    Its Ok, I figured it out. Here is what I used:
    Option Explicit

    Sub LoopThroughSheets()
    Dim ws As Worksheet
    For Each ws In ActiveWorkbook.Worksheets
    ws.Activate

    If ws.Visible = True Then

    Application.Run _
    "Start_New_Prove"
    End If

    On Error Resume Next
    Next ws
    End Sub

+ 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. Pass a variable from one sub-routine to another sub-routine
    By gowtham_pec in forum Excel Programming / VBA / Macros
    Replies: 12
    Last Post: 05-01-2013, 07:07 PM
  2. [SOLVED] Copy dynamically changing column and Paste using VBA Loop (Loop within Loop)
    By nixon72 in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 02-12-2013, 12:46 PM
  3. Add second private sub routine within a loop
    By rhudgins in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 06-29-2010, 03:02 PM
  4. VBA routine gets slower and slower on each iteration of the main loop
    By whitespaces in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 07-15-2009, 03:29 AM
  5. Loop routine fails after 10 cycles......
    By Tom in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 01-11-2006, 11:50 PM

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