Results 1 to 1 of 1

Sorting from lowest to highest by specific column

Threaded View

  1. #1
    Registered User
    Join Date
    08-27-2012
    Location
    Santa Ana, CA USA
    MS-Off Ver
    Excel 2010
    Posts
    43

    Sorting from lowest to highest by specific column

    Greetings, all!

    In a nutshell, what I have is a command button which executes code that loops through rows on a different worksheet ("Work Data") and copy/pastes rows that fit the criteria (in this case, user-entered date) into a 3rd worksheet ("Query Output").

    This works fine, but I am trying to then sort the rows in ascending order by column "G" (which can have a varying number of rows in it).

    If I do this manually and record it as a macro I get the bottom portion of the code below. As it's own module run with a hotkey after the macro pastes data into the Query Output tab, it does the sorting just fine. However, if I stick the same code into the private sub as shown below, I get a "Run-time error '1004', Selected method of range class failed" error.

    Am I overlooking something?

    Private Sub CommandButton3_Click()
    
        Const csQuery As String = "Query Output"
        
        Dim sPartNumber As String
        Dim rFind As Range
        Dim rRows As Range
        Dim rData As Range
        Dim sFirstMatch As String
        Dim ws As Worksheet
        
        sPartNumber = InputBox("Please enter the Date (ex: 6/5/2013):")
        If sPartNumber = "" Then Exit Sub
        
        Set rData = ThisWorkbook.Worksheets("Work Data").Columns("A")
        Set rFind = rData.Find(sPartNumber)
        If rFind Is Nothing Then
            MsgBox "There were no matches.", vbInformation
        Else
            sFirstMatch = rFind.Address
            Do
                If rRows Is Nothing Then
                    Set rRows = rFind
                Else
                    Set rRows = Union(rRows, rFind)
                End If
                Set rFind = rData.FindNext(rFind)
            Loop While rFind.Address <> sFirstMatch
            On Error Resume Next
            Set ws = ThisWorkbook.Worksheets(csQuery)
            On Error GoTo 0
            If ws Is Nothing Then
                Set ws = ThisWorkbook.Sheets.Add
                ws.Name = csQuery
            End If
            ws.Cells.Delete
            ThisWorkbook.Worksheets("Work Data").Rows(1).Copy ws.Rows(1)
            rRows.EntireRow.Copy
            ws.Rows(2).PasteSpecial xlPasteValues
            rRows.Parent.Cells.Copy
            ws.Rows.PasteSpecial xlPasteFormats
        End If
        
    Worksheets("Query Output").Activate
    ActiveSheet.Range("A1").Select
    
    
    Application.CutCopyMode = False
    
    
    
    
    'Below is the code generated by the Macro Recorder
    
    
     Columns("G:G").Select
        ActiveWorkbook.Worksheets("Query Output").Sort.SortFields.Clear
        ActiveWorkbook.Worksheets("Query Output").Sort.SortFields.Add Key:=Range("G1" _
            ), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
        With ActiveWorkbook.Worksheets("Query Output").Sort
            .SetRange Range("A2:W4980")
            .Header = xlNo
            .MatchCase = False
            .Orientation = xlTopToBottom
            .SortMethod = xlPinYin
            .Apply
        End With
    Last edited by smaier69; 10-22-2013 at 12:20 PM.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Sorting a column of figures into highest to lowest
    By josand in forum Excel General
    Replies: 4
    Last Post: 10-02-2012, 09:53 AM
  2. [SOLVED] Excel 2007 : Sorting column by numbers, lowest to highest
    By elryp3000 in forum Excel General
    Replies: 4
    Last Post: 06-07-2012, 11:24 AM
  3. Proper number sorting. Lowest to Highest
    By cturtle in forum Excel General
    Replies: 5
    Last Post: 08-12-2008, 11:29 AM
  4. sorting a table of nunbers lowest to highest, but exclude zero
    By SRussell in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 05-06-2008, 08:20 AM
  5. sorting columns from lowest to highest totals
    By tvi569 in forum Excel Formulas & Functions
    Replies: 1
    Last Post: 06-06-2007, 08:41 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