+ Reply to Thread
Results 1 to 6 of 6

Running macro across multiple worksheets

  1. #1
    Registered User
    Join Date
    11-17-2014
    Location
    Hartford, CT
    MS-Off Ver
    Excel 2010
    Posts
    7

    Running macro across multiple worksheets

    Hi there,
    I am currently running this macro and wanted to run it for multiple worksheets in the same workbook. Wanted to have this work for multiple worksheets in the workbook and not just one.
    Thanks in advance.

    Sub parse_data()

    Dim lr As Long
    Dim ws As Worksheet
    Dim vcol, i As Integer
    Dim icol As Long
    Dim myarr As Variant
    Dim title As String
    Dim titlerow As Integer
    vcol = 3
    Set ws = Sheets("Sheet1")
    lr = ws.Cells(ws.Rows.Count, vcol).End(xlUp).Row
    title = "A2:O2"
    titlerow = ws.Range(title).Cells(1).Row
    icol = ws.Columns.Count
    ws.Cells(1, icol) = "Unique"
    For i = 2 To lr
    On Error Resume Next
    If ws.Cells(i, vcol) <> "" And Application.WorksheetFunction.Match(ws.Cells(i, vcol), ws.Columns(icol), 0) = 0 Then
    ws.Cells(ws.Rows.Count, icol).End(xlUp).Offset(1) = ws.Cells(i, vcol)
    End If
    Next

    myarr = Application.WorksheetFunction.Transpose(ws.Columns(icol).SpecialCells(xlCellTypeConstants))
    ws.Columns(icol).Clear
    For i = 2 To UBound(myarr)
    ws.Range(title).AutoFilter field:=vcol, Criteria1:=myarr(i) & ""
    If Not Evaluate("=ISREF('" & myarr(i) & "'!A1)") Then
    Sheets.Add(after:=Worksheets(Worksheets.Count)).Name = myarr(i) & ""
    Else
    Sheets(myarr(i) & "").Move after:=Worksheets(Worksheets.Count)
    End If
    ws.Range("A" & titlerow & ":A" & lr).EntireRow.Copy Sheets(myarr(i) & "").Range("A1")
    Sheets(myarr(i) & "").Columns.AutoFit
    Next
    ws.AutoFilterMode = False
    ws.Activate
    End Sub

    Any thoughts?
    Last edited by pswiatek; 11-17-2014 at 01:45 PM.

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

    Re: Running macro across multiple worksheets

    Paste the following code into your workbook.

    There are four macros here.

    Contents creates an index sheet for you which lists all your sheet names in column A.

    Put "Y" into column B for every sheet that you want to run the Macro in.

    Optimise and Deoptimise are subroutines that I use to speed up excel and to slow it back down.

    Macro_Overlay is the working part of the Macro.

    It will look down column B of the index sheet and will run your macro on the selected sheet,
    whenever it sees a "Y"


    Please Login or Register  to view this content.
    Please Login or Register  to view this content.

  3. #3
    Registered User
    Join Date
    11-17-2014
    Location
    Hartford, CT
    MS-Off Ver
    Excel 2010
    Posts
    7

    Re: Running macro across multiple worksheets

    Thanks metmechik, but it seems it gets stuck on the "Set ws = Sheets ("Sheet1)" command in my macro when I run the Macro_Overlay.
    Can't remove it because it seems to break right after. not sure how to get reid of that command and move throught the rest of the macro.

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

    Re: Running macro across multiple worksheets

    You need to change the sheet names to the ones in your workbook.

    Do you have a sheet1?

  5. #5
    Registered User
    Join Date
    11-17-2014
    Location
    Hartford, CT
    MS-Off Ver
    Excel 2010
    Posts
    7

    Re: Running macro across multiple worksheets

    No I don't have a sheet1. Each Sheet I would like the macro to run in actually has a different name. Do I list each sheet I would like it to run?

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

    Re: Running macro across multiple worksheets

    Do you need to use a sheet name? are you swapping between sheets? if not then why mention the sheets name?

+ 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. Running a single macro in multiple worksheets
    By robinson032 in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 10-06-2014, 01:54 PM
  2. Running Macro through multiple worksheets
    By ALexcell47 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 11-01-2012, 08:52 PM
  3. Running Macros on multiple worksheets
    By shivrklfc in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 10-30-2012, 11:11 AM
  4. Running One Macro in Multiple Worksheets & Loop
    By mjali001 in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 11-10-2011, 04:57 PM
  5. [SOLVED] Running Macro/VBA in multiple worksheets.
    By YellowBird in forum Excel Programming / VBA / Macros
    Replies: 9
    Last Post: 04-09-2006, 12:30 AM

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