+ Reply to Thread
Results 1 to 9 of 9

Have a loop macro loop for all visible worksheets

  1. #1
    Registered User
    Join Date
    06-18-2014
    Location
    England
    MS-Off Ver
    MS Pro 2010
    Posts
    6

    Have a loop macro loop for all visible worksheets

    Hi,

    I have managed to put together a short loop macro that inserts a number of rows between each account in a list. I need this macro to repeat in all the visible worksheets in my workbook. The workbook will not always have the same number of worksheets and their names will be different.

    Please Login or Register  to view this content.
    I have tried using the below but am coming up with error messages.

    Please Login or Register  to view this content.

    I don't know how to make it work, is it because I am running 2 loops?

    Any help would be much appreciated.

    Thanks,
    Graeme

  2. #2
    Forum Expert judgeh59's Avatar
    Join Date
    02-07-2013
    Location
    Boise, Idaho
    MS-Off Ver
    Excel 2016
    Posts
    2,310

    Re: Have a loop macro loop for all visible worksheets

    What's the error message?
    Ernest

    Please consider adding a * if I helped

    Nothing drives me crazy - I'm always close enough to walk....

  3. #3
    Registered User
    Join Date
    06-18-2014
    Location
    England
    MS-Off Ver
    MS Pro 2010
    Posts
    6

    Re: Have a loop macro loop for all visible worksheets

    I have had a few as I have tried tweaking the code. I'll update the code and show you what I've tried and also the error message.

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

    Re: Have a loop macro loop for all visible worksheets

    Maybe:

    Please Login or Register  to view this content.

  5. #5
    Registered User
    Join Date
    06-18-2014
    Location
    England
    MS-Off Ver
    MS Pro 2010
    Posts
    6

    Re: Have a loop macro loop for all visible worksheets

    Hi Judgeh59,

    when I use the below it loops the initial macro but it loops it through twice more, I have 3 worksheets open, on the first worksheet without going to the 2nd or 3rd.

    Please Login or Register  to view this content.
    I've tried other variations and come up with compile error 'for without next'.

    any thoughts?

    Thanks,
    Graeme

  6. #6
    Forum Expert judgeh59's Avatar
    Join Date
    02-07-2013
    Location
    Boise, Idaho
    MS-Off Ver
    Excel 2016
    Posts
    2,310

    Re: Have a loop macro loop for all visible worksheets

    I would give John's code a go....looks about right to me....I would imagine yours doesn't do what you want is because you aren't activating/selecting the next worksheet....

    IE: ws.Activate

  7. #7
    Registered User
    Join Date
    06-18-2014
    Location
    England
    MS-Off Ver
    MS Pro 2010
    Posts
    6

    Re: Have a loop macro loop for all visible worksheets

    John H Davies - you just nailed it.

    That is great. Was it to do with setting dim as anworksheet rather than an integer?

  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: Have a loop macro loop for all visible worksheets

    You're welcome. Glad to help out and thanks for the feedback. Each type of loop For i = , For each ws should work however you must indicate the sheet. If you use the i loop then With Sheets(i) or Sheets(i).activate. Please comply with Forum Rule No. 9.
    9. Acknowledge the responses you receive, good or bad. If your problem is solved, please say so clearly, and mark your thread as Solved: Click Thread Tools above your first post, select "Mark your thread as Solved". Or click the Edit button on your first post in the thread, Click Go Advanced, select [SOLVED] from the Prefix dropdown, then click Save Changes. If more than two days have elapsed, the Dropdown option or Edit button will not appear -- ask a moderator to mark it.

  9. #9
    Registered User
    Join Date
    06-18-2014
    Location
    England
    MS-Off Ver
    MS Pro 2010
    Posts
    6

    Re: Have a loop macro loop for all visible worksheets

    Thanks John,

    Not sure I fully understand but will be able to adopt the code in future.

    Just getting into VBA and seem to be picking bits up but mainly splicing bits of code from here there and everywhere!

+ 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. [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
  2. macro loop through worksheets
    By asdzxc in forum Excel General
    Replies: 4
    Last Post: 05-09-2012, 03:24 AM
  3. Call Macros before Next loop (loop creates new worksheets)
    By mantooth29 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 03-24-2012, 05:47 PM
  4. Macro Loop Broken. Detects Max but doesn't continue loop
    By herchenbach in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 06-21-2011, 12:17 PM
  5. VBA Loop (?) - macro to run through 33 worksheets!
    By lealea1982 in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 11-04-2010, 07:20 AM

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