+ Reply to Thread
Results 1 to 3 of 3

Sorting from row A6 onwards

Hybrid View

  1. #1
    Registered User
    Join Date
    01-18-2007
    Posts
    22

    Sorting from row A6 onwards

    I want to set up a button on my spreadsheet so that when the user wants to sort the column of names it will do it.

    Problem is that my names start in row A8 - When I try to sort using the Ascending and descending arrows - the sort includes my headers etc in the first 7 rows.

    I don't want use the the Auto filter.

    Here is some code that I found but I can not get it to work.

    Private Sub cmdSort_Click()
    
    Dim iRow As Long
    Dim ws As Worksheet
    
    With Workbook.ws("WeightData")
       iRow = .Cells(Rows.Count, 1).End(xlUp).Offset(1).Row
    
            .Range("A6:A" & iRow).Sort , xlAscending, xlNo
    
        End With
    
    End Sub
    Last edited by VBA Noob; 05-18-2007 at 02:28 AM.

  2. #2
    Forum Expert
    Join Date
    12-10-2006
    Location
    Sydney
    MS-Off Ver
    Office 365
    Posts
    3,565
    Hi there,

    Using the recorder and some programming I've created the following code which (hopefully) meets your needs:

    Dim lngSortTo As Long
    Dim strActiveCell As String
    
    lngSortTo = Sheets("WeightData").Range("A65536").End(xlUp).Row
    strActiveCell = ActiveCell.Address(False, False)
    
    Range("A6:A" & lngSortTo).Select
    
    Selection.Sort Key1:=Range("A6"), Order1:=xlAscending, Header:=xlGuess, _
            OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom, _
            DataOption1:=xlSortNormal
            
    Range(strActiveCell).Select
    Let me know how it goes or if you have any queries.

    Cheers,

    Robert

  3. #3
    Valued Forum Contributor mudraker's Avatar
    Join Date
    11-10-2003
    Location
    Melbourne, Australia
    Posts
    3,983
    teebird

    In you post you said your names start at A8. In the code you posted you are trying to sort from A6.

    try this code

    Sub SortData()
       Dim lngSortTo As Long
       
       With Sheets("WeightData")
          lngSortTo = .Cells(Rows.Count, 1).End(xlUp).Row
          .Range("A8:A" & lngSortTo).Sort Key1:=.Range("A8"), Order1:=xlAscending, Header:=xlNo
       End With
    End Sub
    Please Read Forum Rules Before Posting
    Wrap VBA code by selecting the code and clicking the # icon or Read This
    How To Cross Post politely

    Top Excel links for beginners to Experts

    If you are pleased with a member's answer then use the Scales icon to rate it
    If my reply has assisted or failed to assist you I welcome your Feedback.

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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