+ Reply to Thread
Results 1 to 11 of 11

Running Macro on multiple sheets

  1. #1
    Forum Contributor
    Join Date
    11-11-2012
    Location
    Muscat, Oman
    MS-Off Ver
    Office 365
    Posts
    521

    Running Macro on multiple sheets

    Dear Experts,
    I have a set of few macros and a master macro to run all of them this cleans up the data file provided to me by some other department. I run this file and get the desired results. What I want to do is add one macro which will:
    Run the set on first sheet after finishing moves on to the second sheet and then third so on and so forth till the last work sheet (Which may vary between 3 to 12)and after cleaning individual sheets it consolidates all individual sheets (lets say called consolidated data).
    Currently I am manually running it on the individual sheet and then manually consolidating all the sheets in one place. P.S: After running the macro all the sheets have same headers and same structure.
    Best Regards/VKS

  2. #2
    Forum Guru HaHoBe's Avatar
    Join Date
    02-19-2005
    Location
    Hamburg, Germany
    MS-Off Ver
    work: 2016 on Win10 (notebook), private: 2019 on Win10 (desktop), 2019 on Win11 (notebook)
    Posts
    8,197

    Re: Running Macro on multiple sheets

    Hi, VKS,

    regarding the first question:
    Please Login or Register  to view this content.
    Second question:
    have you tried recording a macro during that consolidating as a start?

    Ciao,
    Holger
    Use Code-Tags for showing your code: [code] Your Code here [/code]
    Please mark your question Solved if there has been offered a solution that works fine for you

  3. #3
    Forum Moderator - RIP Richard Buttrey's Avatar
    Join Date
    01-14-2008
    Location
    Stockton Heath, Cheshire, UK
    MS-Off Ver
    Office 365, Excel for Windows 2010 & Excel for Mac
    Posts
    29,464

    Re: Running Macro on multiple sheets

    Hi,

    We'd need to see the workbook in question before being able to advise with any specific code. Upload the workbook along with some notes which clearly explain which sheets should be processed by your clean up macro, which sheets should be processed by the new macro and what the consolidated sheet should look like.
    Richard Buttrey

    RIP - d. 06/10/2022

    If any of the responses have helped then please consider rating them by clicking the small star icon below the post.

  4. #4
    Forum Contributor
    Join Date
    11-11-2012
    Location
    Muscat, Oman
    MS-Off Ver
    Office 365
    Posts
    521

    Re: Running Macro on multiple sheets

    Hello HaHobe, I did try but i got the run time error what i did was
    Please Login or Register  to view this content.

  5. #5
    Forum Contributor
    Join Date
    11-11-2012
    Location
    Muscat, Oman
    MS-Off Ver
    Office 365
    Posts
    521

    Re: Running Macro on multiple sheets

    Hello Richard, Attached, please find a sample file i.e. after running the clean up macro. PS I have put only 2 sheets but number of sheets may go upto 12 at times.
    Best Regards/VKS
    Attached Files Attached Files

  6. #6
    Forum Moderator - RIP Richard Buttrey's Avatar
    Join Date
    01-14-2008
    Location
    Stockton Heath, Cheshire, UK
    MS-Off Ver
    Office 365, Excel for Windows 2010 & Excel for Mac
    Posts
    29,464

    Re: Running Macro on multiple sheets

    Hi,

    Try:
    Please Login or Register  to view this content.

  7. #7
    Forum Contributor
    Join Date
    11-11-2012
    Location
    Muscat, Oman
    MS-Off Ver
    Office 365
    Posts
    521

    Re: Running Macro on multiple sheets

    Thanks Richard, Tried the macro but it doesnt move could this coz of sheet name in real file is not sheet1, sheet2.......
    Best Regards/VKS

  8. #8
    Forum Moderator - RIP Richard Buttrey's Avatar
    Join Date
    01-14-2008
    Location
    Stockton Heath, Cheshire, UK
    MS-Off Ver
    Office 365, Excel for Windows 2010 & Excel for Mac
    Posts
    29,464

    Re: Running Macro on multiple sheets

    Hi,
    Difficult to say without seeing the real file.

    However it's important not to confuse the sheet TAB name and the name that VBA knows the sheet by. The two can be completely different. When a new workbook is created then they start out the same. However and as in your case, the sheet tab names have been changed to something more meaningful. But just because the Tab name changes doesn't mean the VB name changes, it doesn't. That will only change if you go to the VB environment and change it. In your example case the Tab called "Consolidated Sheet" has the VB code name of 'Sheet1'.

    I suggest you check your real file and see what the code name is for the Consolidated sheet and change the macro I gave you accordingly.

  9. #9
    Forum Contributor
    Join Date
    11-11-2012
    Location
    Muscat, Oman
    MS-Off Ver
    Office 365
    Posts
    521

    Re: Running Macro on multiple sheets

    Thanks Richard, Will work bit more on the code provided.
    Best Regards/VKS

  10. #10
    Forum Guru MarvinP's Avatar
    Join Date
    07-23-2010
    Location
    Woodinville, WA
    MS-Off Ver
    Office 365
    Posts
    16,167

    Re: Running Macro on multiple sheets

    Hi VKS,

    Your worksheets are indexed and you can access them without using their names.

    Worksheets(1) is the first one and looks like you are using it for the consolidation sheet. Worksheets.Count is the last worksheet.

    Instead of using the code of "For Each WS", I like to use the For Next type of code...

    Please Login or Register  to view this content.
    I hope using worksheet indexes helps instead of using the For Each construct.
    One test is worth a thousand opinions.
    Click the * Add Reputation below to say thanks.

  11. #11
    Forum Contributor
    Join Date
    11-11-2012
    Location
    Muscat, Oman
    MS-Off Ver
    Office 365
    Posts
    521

    Re: Running Macro on multiple sheets

    Thanks a lot Marvin

+ 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