Consulting

Results 1 to 6 of 6

Thread: Loop record macro though all worksheets

  1. #1

    Loop record macro though all worksheets


    Hi,

    I am trying to loop a one worksheet (name of that worksheet was Booking)recorded macro through all the sheets in the workbook.

    The recording was to sort data within a range alphabetically.

    No security issues I am aware of.

    If anyone could amened the following code to this, that'd be great! Thanks inadvance.

    --------------------------------------------------------


    Sub ShootSort()
    '
    ' ShootSort Macro
    ‘
    '
      Range("E8:J27").Select
    
      ActiveWorkbook.Worksheets("Booking").Sort.SortFields.Clear
    
      ActiveWorkbook.Worksheets("Booking").Sort.SortFields.AddKey:=Range("J8:J27") _
    
      , SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
    
      ActiveWorkbook.Worksheets("Booking").Sort.SortFields.AddKey:=Range("E8:E27") _
    
      , SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
    
      With ActiveWorkbook.Worksheets("Booking").Sort
    
      .SetRange Range("E8:J27")
    
      .Header = xlGuess
    
      .MatchCase = False
    
      .Orientation = xlTopToBottom
    
      .SortMethod = xlPinYin
    
      .Apply
    
      End With
    
    End Sub
    Last edited by Paul_Hossler; 09-28-2018 at 11:26 AM.

  2. #2
    Knowledge Base Approver VBAX Wizard p45cal's Avatar
    Joined
    Oct 2005
    Location
    Surrey UK
    Posts
    5,875
    try:
    Sub ShootSort()
    For Each sht In ActiveWorkbook.Sheets
      With sht.Sort
        .SortFields.Clear
        .SortFields.Add Key:=Range("J8:J27"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
        .SortFields.Add Key:=Range("E8:E27"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
        .SetRange Range("E8:J27")
        .Header = xlGuess
        .MatchCase = False
        .Orientation = xlTopToBottom
        .SortMethod = xlPinYin
        .Apply
      End With
    Next sht
    End Sub
    (untested)
    p45cal
    Everyone: If I've helped and you can't be bothered to acknowledge it, I can't be bothered to look at further posts from you.

  3. #3
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,728
    Location
    @scott_86_

    I added CODE tags to your post to format the macro -- you can use the [#] incon to insert CODE and \CODE tags and paste your macro between

    p45cal's macro will work, but if the data to be sorted on all sheets is NOT the same layout (i.e. not always J8:J27 and E8:E27) some generalization is required
    ---------------------------------------------------------------------------------------------------------------------

    Paul


    Remember: Tell us WHAT you want to do, not HOW you think you want to do it

    1. Use [CODE] ....[/CODE ] Tags for readability
    [CODE]PasteYourCodeHere[/CODE ] -- (or paste your code, select it, click [#] button)
    2. Upload an example
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) / Upload Files / Done
    3. Mark the thread as [Solved] when you have an answer
    Thread Tools (on the top right corner, above the first message)
    4. Read the Forum FAQ, especially the part about cross-posting in other forums
    http://www.vbaexpress.com/forum/faq...._new_faq_item3

  4. #4
    Knowledge Base Approver VBAX Wizard p45cal's Avatar
    Joined
    Oct 2005
    Location
    Surrey UK
    Posts
    5,875
    oh groan…
    cross posted https://chandoo.org/forum/threads/lo...ksheets.39866/
    wasted my time.

  5. #5
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,728
    Location
    @scott_86_ -- if you come back to read this, please read http://www.vbaexpress.com/forum/faq...._new_faq_item3

    All forums like to know if questions have been posted on other forums so that people don't waste time answering questions that have already been answered
    ---------------------------------------------------------------------------------------------------------------------

    Paul


    Remember: Tell us WHAT you want to do, not HOW you think you want to do it

    1. Use [CODE] ....[/CODE ] Tags for readability
    [CODE]PasteYourCodeHere[/CODE ] -- (or paste your code, select it, click [#] button)
    2. Upload an example
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) / Upload Files / Done
    3. Mark the thread as [Solved] when you have an answer
    Thread Tools (on the top right corner, above the first message)
    4. Read the Forum FAQ, especially the part about cross-posting in other forums
    http://www.vbaexpress.com/forum/faq...._new_faq_item3

  6. #6
    Knowledge Base Approver VBAX Guru macropod's Avatar
    Joined
    Jul 2008
    Posts
    4,435
    Location
    Cheers
    Paul Edstein
    [Fmr MS MVP - Word]

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •