Consulting

Results 1 to 7 of 7

Thread: Solved: Loop through sheets

  1. #1

    Solved: Loop through sheets

    I need this code altered to loop through all sheets after Sheet 2, the quantity of sheets will be ever changing. If any cleaning of the code can be done , that would be fine with me. I would also like to send each sheet to a recipient whose e-mail address would be in cell A1 of each sheet.


    [VBA]Sub SortForZeroFirst()'' SortForZeroFirst Macro' Macro recorded 3/9/2009 by David D'' Sheets(3).Activate Range("A5:I" _
    & Range("B2000").End(xlUp).Row).Select Selection.Sort Key1:=Range("I5"), Order1:=xlAscending, Key2:=Range("C5") _
    , Order2:=xlAscending, Header:=xlGuess, OrderCustom:=1, MatchCase:= _ False, Orientation:=xlTopToBottom, _
    DataOption1:=xlSortNormal, DataOption2 _ :=xlSortNormal Range("A1").Select Sheets(4).Activate Range("A5:I" & _
    Range("B2000").End(xlUp).Row).Select Selection.Sort Key1:=Range("I5"), Order1:=xlAscending, Key2:=Range("C5") _
    , Order2:=xlAscending, Header:=xlGuess, OrderCustom:=1, MatchCase:= _ False, Orientation:=xlTopToBottom, _
    DataOption1:=xlSortNormal, DataOption2 _ :=xlSortNormal Range("A1").Select
    End Sub[/VBA]


    Thanks in advance for any help.

  2. #2
    Trying to make the code read better
    Sub SortForZeroFirst()
    '
    ' SortForZeroFirst Macro
    ' Macro recorded 3/9/2009 by David D
    '
    '
       
        Sheets(3).Activate
       Range("A5:I" & Range("B2000").End(xlUp).Row).Select
        
        Selection.Sort Key1:=Range("I5"), Order1:=xlAscending, Key2:=Range("C5") _
            , Order2:=xlAscending, Header:=xlGuess, OrderCustom:=1, MatchCase:= _
            False, Orientation:=xlTopToBottom, DataOption1:=xlSortNormal, DataOption2 _
            :=xlSortNormal
        Range("A1").Select
        
        Sheets(4).Activate
       Range("A5:I" & Range("B2000").End(xlUp).Row).Select
        
        Selection.Sort Key1:=Range("I5"), Order1:=xlAscending, Key2:=Range("C5") _
            , Order2:=xlAscending, Header:=xlGuess, OrderCustom:=1, MatchCase:= _
            False, Orientation:=xlTopToBottom, DataOption1:=xlSortNormal, DataOption2 _
            :=xlSortNormal
        Range("A1").Select
    End Sub

  3. #3
    Administrator
    VP-Knowledge Base
    VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    Try
    [VBA]Sub SortForZeroFirst()
    '
    ' SortForZeroFirst Macro
    ' Macro recorded 3/9/2009 by David D
    '
    '

    For i = 3 To Sheets.Count
    With Sheets(i)
    rw = .Cells(Rows.Count, 2).End(xlUp).Row
    .Range("A5:I" & rw).Sort Key1:=.Range("I5"), Order1:=xlAscending, Key2:=.Range("C5") _
    , Order2:=xlAscending, Header:=xlGuess, OrderCustom:=1, MatchCase:= _
    False, Orientation:=xlTopToBottom, DataOption1:=xlSortNormal, DataOption2 _
    :=xlSortNormal
    End With
    Next
    End Sub
    [/VBA]
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  4. #4
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    [vba]

    Sub SortForZeroFirst()
    '
    ' SortForZeroFirst Macro
    ' Macro recorded 3/9/2009 by David D
    '
    '

    For i = 3 To Worksheets.Count

    With Worksheets(i)

    .Range ("A5:I" & Range("B2000").End(xlUp).Row)

    .Sort Key1:=.Range("I5"), Order1:=xlAscending, _
    Key2:=Range("C5"), Order2:=xlAscending, _
    Header:=xlGuess
    End With
    Next i
    End Sub
    [/vba]
    Last edited by mdmackillop; 03-09-2009 at 02:40 PM. Reason: Error fixed
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  5. #5
    Thank you very much gentlemen.
    xld, your code causes a compile error, "End With without With"
    mdm, works Great. Thanks

  6. #6
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    That is because the second End With should read Next i
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  7. #7
    xld,
    This produced another error, this time at the Range.

Posting Permissions

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