+ Reply to Thread
Results 1 to 7 of 7

Thread: VBA Sort Code - Changing Range

  1. #1
    Registered User
    Join Date
    09-05-2011
    Location
    St. John's, Canada
    MS-Off Ver
    Excel 2010
    Posts
    71

    VBA Sort Code - Changing Range

    Hey Everyone,

    I've been spending the better part of this day creating a Acronym search engine and "database" ( its an excel workbook). And I'm almost done with the help of everyone in the forum :D.

    The last thing I want to do is once I have added a new acronym, by clicking the button and filling out the form. I want to add a piece of code that sorts the "Acronym Database" sheet, with regards to Column A.

    I've been through the interet but can't find anything that does this with a changing range, since everytime I add a new acronym I add a new row.

    Any ideas on how I could do this? I've attached my Excel Work Book for clarification.

    Thanks
    Attached Files Attached Files
    Last edited by ashleys.nl; 10-14-2011 at 02:25 PM.

  2. #2
    Valued Forum Contributor MarvinP's Avatar
    Join Date
    07-23-2010
    Location
    Seattle, WA
    MS-Off Ver
    Excel 2010
    Posts
    5,300

    Re: VBA Sort Code - Changing Range

    Hi ashleys.nl,

    It sounds like you want a Dynamic Named Range to span all the rows of your data. Then use this range name in your sort formula.

    http://www.ozgrid.com/Excel/DynamicRanges.htm
    http://www.beyondtechnology.com/geeks007.shtml
    One test is worth a thousand opinions.
    Click the * below to say thanks.

  3. #3
    Registered User
    Join Date
    09-05-2011
    Location
    St. John's, Canada
    MS-Off Ver
    Excel 2010
    Posts
    71

    Re: VBA Sort Code - Changing Range

    Thanks Marvin,

    But I can't really use add in's on our corperate computers. I'd like to have something that's coded into my VBA code.

    Thanks for your input though

  4. #4
    Valued Forum Contributor MarvinP's Avatar
    Join Date
    07-23-2010
    Location
    Seattle, WA
    MS-Off Ver
    Excel 2010
    Posts
    5,300

    Re: VBA Sort Code - Changing Range

    Hi,

    Dynamic Named Ranges are not an Add-In to Excel. They live there but are just not used much. In VBA code you can find the last row that is used in a column with the following code:

    LastRow = Cells(Rows.Count,"A").End(xlUp).Row
    read http://www.tushar-mehta.com/excel/vb...and%20code.htm or
    http://www.ozgrid.com/forum/showthre...t=68874&page=1
    One test is worth a thousand opinions.
    Click the * below to say thanks.

  5. #5
    Forum Guru TMShucks's Avatar
    Join Date
    07-15-2010
    Location
    Manchester, England
    MS-Off Ver
    MSO 2003 & 2007
    Posts
    6,230

    Re: VBA Sort Code - Changing Range

    They're not references to Add-ins, they're explanations of how to define a Dynamic Named Range which caters for a variable number of rows.

    Regards

  6. #6
    Registered User
    Join Date
    09-05-2011
    Location
    St. John's, Canada
    MS-Off Ver
    Excel 2010
    Posts
    71

    Re: VBA Sort Code - Changing Range

    Ahhh Okay,

    I think I'm catching on,

    I'm trying to implement this now into my code but Im getting the error :

    "Compile Error : Method or data Member not Found"

    Any thoughts?

    Sub Sort()
    
    Dim LastRow As Long
    
    LastRow = Worksheets.Cells(Rows.Count, "A").End(xlUp).Row
    
    Worksheets("Acronym Database").Sort.SortFields.Clear
    Worksheets("Acronym Database").Sort.SortFields.Add Key:=Range( _
            "A1"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:= _
            xlSortNormal
        With Worksheets("Acronym Database").Sort
            .SetRange Range("A1:LastRow")
            .Header = xlNo
            .MatchCase = False
            .Orientation = xlTopToBottom
            .SortMethod = xlPinYin
            .Apply
        End With
    End Sub

  7. #7
    Forum Guru TMShucks's Avatar
    Join Date
    07-15-2010
    Location
    Manchester, England
    MS-Off Ver
    MSO 2003 & 2007
    Posts
    6,230

    Re: VBA Sort Code - Changing Range

    .SetRange = .Range("A1:A" & lastrow))

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Tags for this Thread

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.2.0