Wrox Programmer Forums
|
Excel VBA Discuss using VBA for Excel programming.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Excel VBA section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old March 25th, 2004, 11:32 AM
Registered User
 
Join Date: Mar 2004
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default Sorting Data

Hi

I am new to VBA. I am trying to write code to go through each worksheet to sort data. However, when I execute it is only sorting the current sheet and not looping though all of the worksheets.

Below is my code. Can someone help!!

Thanks.

Dim xlSheet As Excel.Worksheet
Set xlSheet = ActiveSheet
Dim Rng As Range
Set Rng = Range(Range("A2").End(xlToRight), Range("A2").End(xlDown))

    For Each xlSheet In ActiveWorkbook.Worksheets
        Rng.Sort Key1:=Range(Range("A2").End(xlToRight), Range("A2").End(xlDown)), Order1:=xlAscending, Header:=xlGuess, OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom, DataOption1:=xlSortNormal

Next
End Sub

 
Old March 25th, 2004, 01:22 PM
Authorized User
 
Join Date: Oct 2003
Posts: 10
Thanks: 0
Thanked 0 Times in 0 Posts
Default

2 things to try:

1 Make the first line in the For Each loop
       xlSheet.Activate

2 Put the Set rng line in the loop

A quick experiment suggests this will fix it.

Selby


 
Old May 21st, 2004, 02:24 PM
Authorized User
 
Join Date: May 2004
Posts: 14
Thanks: 0
Thanked 0 Times in 0 Posts
Default

This is what I did .

On the VB fourm they pick what column to sort on by a combo box

and the code to do the sort in excel is:

lnRow = last row in the spread sheet

lcStartRow = the row the reports data start on since I have a header on the spread sheet





   Select Case Me.cmbReportType.ListIndex
        Case Is = 0
            Select Case Me.cmbSortOn.ListIndex
                Case Is = 0
                    lcCellToSortOn = "A"
                Case Is = 1
                    lcCellToSortOn = "B"
                Case Is = 2
                    lcCellToSortOn = "C"
                Case Is = 3
                    lcCellToSortOn = "D"
                Case Is = 4
                    lcCellToSortOn = "E"
                Case Is = 5
                    lcCellToSortOn = "F"
            End Select

            lcStartRow = CStr(lnStartRow + 1)
            lcCellToSortOn = lcCellToSortOn & lcStartRow
            lcCellSelect = "A" & lcStartRow & ":F" & CStr(lnRow)

            With goExcel
                .Range(lcCellSelect).Sort .Range(lcCellToSortOn)
            End With
    End Select







Similar Threads
Thread Thread Starter Forum Replies Last Post
Data sorting dilipv .NET Framework 2.0 1 March 10th, 2008 09:19 AM
DataGridView - Sorting Data pencilneck Visual Studio 2005 0 October 23rd, 2007 04:35 PM
Sorting data in a DataSet pbyrum ADO.NET 5 May 18th, 2006 05:11 PM
sorting retrieved data don baroo Classic ASP Basics 2 November 23rd, 2004 08:31 AM
Sorting in Data Grid matt.fields ASP.NET 1.0 and 1.1 Basics 0 October 21st, 2003 12:39 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.