+ Reply to Thread
Results 1 to 4 of 4

Macro for different sheets

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    11-20-2009
    Location
    Melbourne
    MS-Off Ver
    Excel 365
    Posts
    288

    Macro for different sheets

    I am trying to get a Macro to repeat a sort on the different sheets in my workbook.
    The headers are all the same on each Worksheet, just with different data (segregated by Age Groups)
    For example I need to sort Col's B1:B100, D1:D100 & then F1:F100
    Through searching I have found the start of the code, but I have no idea how to complete it.
    Dim ws As Worksheet
        For Each ws In Sheets
        ws.Activate
    That's as far as I can go.
    Could anyone please show me how to finish the code off? Thank you for any assistance you can offer.

  2. #2
    Valued Forum Contributor
    Join Date
    09-21-2011
    Location
    Birmingham UK
    MS-Off Ver
    Excel 2003/7/10
    Posts
    2,188

    Re: Macro for different sheets

    Record a macro doing one sheet, then loop, using your ws, instead of what ever will be recorded.
    Hope this helps

    Sometimes its best to start at the beginning and learn VBA & Excel.

    Please dont ask me to do your work for you, I learnt from Reading books, Recording, F1 and Google and like having all of this knowledge in my head for the next time i wish to do it, or wish to tweak it.
    Available for remote consultancy work PM me

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

    Re: Macro for different sheets

    Maybe:

    Sub coach_32()
    Dim x As Integer
    x = 2
    Dim ws As Worksheet
        For Each ws In ActiveWorkbook.Worksheets
        ws.Activate
                For x = 2 To 6 Step 2
                     Columns(x).Select
                        Selection.Sort Key1:=Cells(1, x), Order1:=xlAscending, Header:=xlGuess, _
                        OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom, _
                        DataOption1:=xlSortNormal
                Next x
        Next ws
    End Sub

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

    Re: Macro for different sheets

    Hi, coach.32,

    no need to activate each sheet:
    Sub foo()
    Dim lngCol As Long
    Dim ws As Worksheet
    For Each ws In ActiveWorkbook.Worksheets
      For lngCol = 2 To 6 Step 2
        With ws
          .Range(.Cells(1, lngCol), .Cells(.Cells(Rows.Count, lngCol).End(xlUp).Row, lngCol)).Sort _
              Key1:=.Cells(1, lngCol), _
              Order1:=xlAscending, _
              Header:=xlGuess, _
              OrderCustom:=1, _
              MatchCase:=False, _
              Orientation:=xlTopToBottom, _
             DataOption1:=xlSortNormal
        End With
      Next lngCol
    Next ws
    End Sub
    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

+ 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. Replies: 2
    Last Post: 09-19-2013, 04:22 AM
  2. [SOLVED] Macro to Unprotect sheets-unlock range of cells-protect sheets
    By jrace in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 05-09-2013, 10:45 AM
  3. Creating a macro to print only the sheets and portion of sheets that i select.
    By cadamhill in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 12-31-2012, 01:48 PM
  4. Extracting Data from 2 sheets & Placing in other 2 sheets of same workbook using VBA Macro
    By ukash.dost@gmail.com in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 10-10-2012, 08:56 AM
  5. Macro for filter on protected workbook that works for all sheets, no matter what sheets are named?
    By StargateFanFromWork in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 01-26-2006, 02:35 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