+ Reply to Thread
Results 1 to 6 of 6

simplifying macro to repeat steps in every sheet

Hybrid View

  1. #1
    Registered User
    Join Date
    02-27-2009
    Location
    ma
    MS-Off Ver
    Excel 2003
    Posts
    35

    simplifying macro to repeat steps in every sheet

    i have a book set up with a few sheets with info seperated out and then a sheet contianing everything. i have a macro set up to copy and paste from one sheet into a collection sheet, and then go thru and sort all the sheets.

    in my macro, i have a section for each indivitual sheet. i want to make it so it goes thru and just performes the task for all of them. i need this done so if we add more sheets in later, the macro doesnt have to be edited...

    below is my macro





    Sub Macro1()
    ' Keyboard Shortcut: Ctrl+z
    
        ActiveSheet.Cells(Rows.Count, "A").End(xlUp).EntireRow.Copy
        Worksheets("ALL").Cells(Rows.Count, "A").End(xlUp).Offset(1).PasteSpecial
        Application.CutCopyMode = False
        
        Sheets("ALL").Select
        Cells.Select
        Selection.Sort Key1:=Range("D2"), Order1:=xlAscending, Header:=xlGuess, _
        OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom
        
        Sheets("BMI").Select
        Cells.Select
        Selection.Sort Key1:=Range("D2"), Order1:=xlAscending, Header:=xlGuess, _
        OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom
    
        Sheets("ODC").Select
        Cells.Select
        Selection.Sort Key1:=Range("D2"), Order1:=xlAscending, Header:=xlGuess, _
        OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom
    
        Sheets("SNR").Select
        Cells.Select
        Selection.Sort Key1:=Range("D2"), Order1:=xlAscending, Header:=xlGuess, _
        OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom
    
        Sheets("XAC").Select
        Cells.Select
        Selection.Sort Key1:=Range("D2"), Order1:=xlAscending, Header:=xlGuess, _
        OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom
    
        Sheets("DPY").Select
        Cells.Select
        Selection.Sort Key1:=Range("D2"), Order1:=xlAscending, Header:=xlGuess, _
        OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom
        
    End Sub

  2. #2
    Forum Expert royUK's Avatar
    Join Date
    11-18-2003
    Location
    Derbyshire,UK
    MS-Off Ver
    Xp; 2007; 2010
    Posts
    26,200

    Re: simplifying macro to repeat steps in every sheet

    You don't need to select the sheet or range
    Dim ws As Worksheet
    
       For Each ws In ThisWorkbook.Worksheets
       ws.UsedRange.Sort Key1:=Range("D2"), Order1:=xlAscending, Header:=xlGuess, _
        OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom
      Next ws
    Last edited by royUK; 05-28-2009 at 10:21 AM.
    Hope that helps.

    RoyUK
    --------
    For Excel Tips & Solutions, free examples and tutorials why not check out my web site

    Free DataBaseForm example

  3. #3
    Registered User
    Join Date
    02-27-2009
    Location
    ma
    MS-Off Ver
    Excel 2003
    Posts
    35

    Re: simplifying macro to repeat steps in every sheet

    compile error: invalid or unquilifed reference

    .UsedRange is highlighted

  4. #4
    Forum Expert royUK's Avatar
    Join Date
    11-18-2003
    Location
    Derbyshire,UK
    MS-Off Ver
    Xp; 2007; 2010
    Posts
    26,200

    Re: simplifying macro to repeat steps in every sheet

    I've amended the previous code

  5. #5
    Registered User
    Join Date
    02-27-2009
    Location
    ma
    MS-Off Ver
    Excel 2003
    Posts
    35

    Re: simplifying macro to repeat steps in every sheet

    run-time error 1104
    debug

       ws.UsedRange.Sort Key1:=Range("D2"), Order1:=xlAscending, Header:=xlGuess, _
        OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom
    is highlighted yellow...

    help says:

    Ambiguous selection

    Either you have not selected a keyword or you have requested Help on a component of the integrated development environment (IDE). If you were trying to select a keyword, try reselecting a single keyword. If you were trying to get information on the IDE, click one of the following:

  6. #6
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,258

    Re: simplifying macro to repeat steps in every sheet

    Hello roth_nj,

    The named argument Key1 needs to be qualified with the worksheet variable ws, otherwise Excel assumes the range is on the active sheet. Also the active sheet should be excluded from the sorting loop.
    Sub Macro1()
    
      Dim ws As Worksheet
    
        ActiveSheet.Cells(Rows.Count, "A").End(xlUp).EntireRow.Copy
        Worksheets("ALL").Cells(Rows.Count, "A").End(xlUp).Offset(1).PasteSpecial
        Application.CutCopyMode = False
        
        For Each ws In ThisWorkbook.Worksheets
          If ws.Name <> ActiveSheet.Name Then
            ws.UsedRange.Sort Key1:=ws.Range("A2"), Order1:=xlAscending, Header:=xlGuess, _
                             OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom
          End If
        Next ws
       
    End Sub
    Sincerely,
    Leith Ross

    Remember To Do the Following....

    1. Use code tags. Place [CODE] before the first line of code and [/CODE] after the last line of code.
    2. Thank those who have helped you by clicking the Star below the post.
    3. Please mark your post [SOLVED] if it has been answered satisfactorily.


    Old Scottish Proverb...
    Luathaid gu deanamh maille! (Rushing causes delays!)

+ 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