+ Reply to Thread
Results 1 to 6 of 6

Sorting variable amounts of data

Hybrid View

oriecat Sorting variable amounts of... 05-06-2012, 03:21 PM
oriecat Re: Sorting variable amounts... 05-06-2012, 03:33 PM
AlvaroSiza Re: Sorting variable amounts... 05-07-2012, 01:58 AM
oriecat Re: Sorting variable amounts... 05-07-2012, 04:46 PM
vandan_tanna Re: Sorting variable amounts... 05-07-2012, 04:59 PM
oriecat Re: Sorting variable amounts... 05-07-2012, 06:33 PM
  1. #1
    Registered User
    Join Date
    05-06-2012
    Location
    OR
    MS-Off Ver
    Excel 2007
    Posts
    4

    Sorting variable amounts of data

    Hi, I am trying to write a macro that will look at a section of data, sort the data until it hits a blank cell and then move the newly sorted data to another location. Column C is random numbers for random sorting. The data is in column D. The data could range from 1 cell to 35 cells. I can't figure out how to make the sort variable, like I can the moving using the xlDown thing.

    Here's what I have so far. It is currently showing C65:D87 because that's what ended up there when I recorded the macro. Thanks for any help on this!

    Sub Macro9()
    '
    ' Macro9 Macro
    '
    
    '
        range("C65:D65").Select
        range("D65:D65").Activate
        range(Selection, Selection.End(xlDown)).Select
        ActiveWorkbook.Worksheets("SkillCards").Sort.SortFields.Clear
        ActiveWorkbook.Worksheets("SkillCards").Sort.SortFields.Add Key:=range( _
            "C65:C87"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:= _
            xlSortNormal
        With ActiveWorkbook.Worksheets("SkillCards").Sort
            .SetRange range("C65:D87")
            .Header = xlGuess
            .MatchCase = False
            .Orientation = xlTopToBottom
            .SortMethod = xlPinYin
            .Apply
        End With
        range("D65").Select
        range(Selection, Selection.End(xlDown)).Select
        Selection.Copy
        range("D3").Select
        ActiveSheet.Paste
        range("D65").Select
        range(Selection, Selection.End(xlDown)).Select
        Application.CutCopyMode = False
        Selection.ClearContents
        range("D3").Select
    End Sub

  2. #2
    Registered User
    Join Date
    05-06-2012
    Location
    OR
    MS-Off Ver
    Excel 2007
    Posts
    4

    Re: Sorting variable amounts of data

    Note: I don't want to sort the whole 35 each time, because then with the random sorting, blanks will get sorted in and I want it to be contiguous when it gets moved to the new location. Unless there is some way to strip those blanks out at that time.

  3. #3
    Valued Forum Contributor AlvaroSiza's Avatar
    Join Date
    09-19-2007
    Location
    Staffordshire
    MS-Off Ver
    2007
    Posts
    591

    Re: Sorting variable amounts of data

    Please post a sample workbook.
    Perhaps it was the Noid who should have avoided me...
    If you are satisfied with my solution click the small star icon on the left. Thanks
    1. Make a copy of your workbook and run the following code on your copy (just in case)
    2. With excel open, press ALT+F11 to open the Visual Basic Editor (VBE). From the "Insert" menu, select "Module".
    3. Paste the code from above into the empty white space. Close the VBE.
    4. From the developer tab, choose "Macros", select the Sub Name, and click "Run".

  4. #4
    Registered User
    Join Date
    05-06-2012
    Location
    OR
    MS-Off Ver
    Excel 2007
    Posts
    4

    Re: Sorting variable amounts of data

    I hope I did this right.

    Book12.xlsm

  5. #5
    Valued Forum Contributor
    Join Date
    05-07-2012
    Location
    USA
    MS-Off Ver
    Excel 2007
    Posts
    354

    Re: Sorting variable amounts of data

    Hi:

    I haven't looked at your excel file, but I think if you change "=range("C65:C87")" in your code to "Selection" it will work and become variable like you want.

    That said, I think you should search the internet for examples of vba sorting code. Your code can be a lot more efficient and more manageable

  6. #6
    Registered User
    Join Date
    05-06-2012
    Location
    OR
    MS-Off Ver
    Excel 2007
    Posts
    4

    Re: Sorting variable amounts of data

    Thank you, I will try that. I did try searching and couldn't find anything like what I want. I got that code from the macro recorder and then cleaned it up a little. I will see what else I can find though.

+ 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