Consulting

Results 1 to 4 of 4

Thread: Solved: Sorting a non-active sheet

  1. #1
    VBAX Mentor
    Joined
    Jan 2009
    Posts
    304
    Location

    Solved: Sorting a non-active sheet

    Is there a way to sort a non-active sheet?

    Below is some code that adds a Totals row at the bottom.

    I need to sort the rows before addding the Totals row.

    Can this code be easily modified to do this?

    Thanks...

    Jim

    [vba]
    Sub AddTotals()
    'This macro sums both the columns In columns B:J
    'The column sums are placed after the last row.
    'The last row is determined by the longest column of data.

    Dim LastRow As Long
    Dim iCol As Integer
    Dim sht As Worksheet
    LastRow = 0

    Application.ScreenUpdating = False


    For Each sht In ActiveWorkbook.Sheets
    If Left(sht.Name, 6) = "Totals" Then

    LastRow = 0

    'Find last row in Columns B:J
    For iCol = 2 To 10
    iRow = sht.Cells(65536, iCol).End(xlUp).Row
    If iRow > LastRow Then LastRow = iRow
    Next iCol

    With Application.WorksheetFunction

    'Place column totals in row after current last row
    For iCol = 2 To 10
    sht.Cells(LastRow + 1, iCol) = .Sum(Range(sht.Cells(2, iCol), sht.Cells(LastRow, iCol)))
    Next iCol

    sht.Cells(LastRow + 1, 1) = "Totals:"
    End With

    End If

    Next sht

    Application.ScreenUpdating = True

    End Sub
    [/vba]

  2. #2
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Try this, but change the sort to suit

    [vba]

    Sub AddTotals()
    'This macro sums both the columns In columns B:J
    'The column sums are placed after the last row.
    'The last row is determined by the longest column of data.

    Dim LastRow As Long
    Dim iCol As Integer
    Dim sht As Worksheet
    LastRow = 0

    Application.ScreenUpdating = False


    For Each sht In ActiveWorkbook.Sheets

    If Left(sht.Name, 6) = "Totals" Then

    LastRow = 0

    'Find last row in Columns B:J
    For iCol = 2 To 10
    iRow = sht.Cells(sht.Rows.Count, iCol).End(xlUp).Row
    If iRow > LastRow Then LastRow = iRow
    Next iCol

    With sht

    Range(.Cells(2, iCol), .Cells(LastRow, iCol)).Sort _
    Key1:=.Cells(2, iCol), Order1:=xlAscending, _
    Key2:=.Cells(2, iCol + 1), Order2:=xlAscending, _
    Header:=xlYes

    'Place column totals in row after current last row
    For iCol = 2 To 10

    .Cells(LastRow + 1, iCol) = Application.Sum(Range(.Cells(2, iCol), .Cells(LastRow, iCol)))
    Next iCol

    .Cells(LastRow + 1, 1) = "Totals:"
    End With

    End If

    Next sht

    Application.ScreenUpdating = True

    End Sub
    [/vba]
    ____________________________________________
    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

  3. #3
    VBAX Mentor
    Joined
    Jan 2009
    Posts
    304
    Location
    Got it to work perfectly, Thanks...
    Last edited by JimS; 07-16-2009 at 05:34 PM.

  4. #4
    Moderator VBAX Wizard Aussiebear's Avatar
    Joined
    Dec 2005
    Location
    Queensland
    Posts
    5,059
    Location
    You did or was it the response offered by Bob? Can you also please mark the thread as solved by using the Thread Tools options.
    Remember To Do the Following....
    Use [Code].... [/Code] tags when posting code to the thread.
    Mark your thread as Solved if satisfied by using the Thread Tools options.
    If posting the same issue to another forum please show the link

Posting Permissions

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