Results 1 to 5 of 5

Thread: Sorting data excel

  1. #1

    Thread Starter
    New Member
    Join Date
    Jun 2006
    Posts
    6

    Sorting data excel

    I've been working with excel in visual basic .net 2002. The only thing I'm having trouble with is sorting data. What I do in excel is select the entire sheet and sort all the data by one of the columns. I want to do this in visual basic and have had some problems. For one I would like to define the header cell by number something like worksheet.cells(1,5), MSDN gives examples calling the sort function by string. I cannot get the worksheet.range.sort to work for me and I don't even know if this is what I should use.


    brwiese

  2. #2
    Addicted Member phenom's Avatar
    Join Date
    Apr 2006
    Location
    UAE
    Posts
    233

    Re: Sorting data excel

    Hi brwiese,

    Use this code, it shows you how to sort Data in Excel…
    VB Code:
    1. 'VB 2005
    2. Dim ObjExcel As New Microsoft.Office.Interop.Excel.Application
    3.         ObjExcel.Visible = True
    4.         ObjExcel.Workbooks.Add()
    5.         ObjExcel.Cells(1, 1) = "1000"
    6.         ObjExcel.Cells(2, 1) = "100"
    7.         ObjExcel.Cells(3, 1) = "500"
    8.         ObjExcel.Cells(4, 1) = "900"
    9.         'Sorting Data
    10.         ObjExcel.Cells.Range("A1:A4").Sort(Key1:=ObjExcel.Cells.Range("A1"), Order1:=XlSortOrder.xlAscending, Header:= _
    11.     XlYesNoGuess.xlGuess, OrderCustom:=1, MatchCase:=False, Orientation:=XlSortOrientation.xlSortColumns, _
    12.         DataOption1:=XlSortDataOption.xlSortNormal)
    13.         ObjExcel = Nothing
    Hope this helps...

    Regards,
    =======================================
    If I helped you, Kindly Rate my post. Thanks
    -----------
    PHENOM

  3. #3

    Thread Starter
    New Member
    Join Date
    Jun 2006
    Posts
    6

    Re: Sorting data excel

    Thanks for the quick response. I've gotten as far as sorting a single column, its extending this to sort all columns by the data in a single column that I'm not getting.

    brwiese

  4. #4
    Addicted Member phenom's Avatar
    Join Date
    Apr 2006
    Location
    UAE
    Posts
    233

    Re: Sorting data excel

    Hi brwiese,

    Sorry for late reply, I’m little busy cuz I’m traveling tonight for a vacation

    Here’s how to sort multiple columns

    VB Code:
    1. Dim ObjExcel As New Microsoft.Office.Interop.Excel.Application
    2.         ObjExcel.Visible = True
    3.         ObjExcel.Workbooks.Add()
    4.         ObjExcel.Cells(1, 1) = "Column A"
    5.         ObjExcel.Cells(2, 1) = "1000"
    6.         ObjExcel.Cells(3, 1) = "100"
    7.         ObjExcel.Cells(4, 1) = "6000"
    8.         ObjExcel.Cells(5, 1) = "20"
    9.         ObjExcel.Cells.Range("A1:A5").Sort(Key1:=ObjExcel.Cells.Range("A2"), Order1:=XlSortOrder.xlAscending, Header:=XlYesNoGuess.xlGuess, _
    10.                 OrderCustom:=1, MatchCase:=False, Orientation:=XlSortOrientation.xlSortColumns, _
    11.                 DataOption1:=XlSortDataOption.xlSortNormal)
    12.         ObjExcel.Cells(1, 2) = "Column B"
    13.         ObjExcel.Cells(2, 2) = "10"
    14.         ObjExcel.Cells(3, 2) = "200"
    15.         ObjExcel.Cells(4, 2) = "5000"
    16.         ObjExcel.Cells(5, 2) = "20"
    17.         ObjExcel.Cells.Range("B1:B5").Sort(Key1:=ObjExcel.Cells.Range("B2"), Order1:=XlSortOrder.xlAscending, Header:=XlYesNoGuess.xlGuess, _
    18.                 OrderCustom:=1, MatchCase:=False, Orientation:=XlSortOrientation.xlSortColumns, _
    19.                 DataOption1:=XlSortDataOption.xlSortNormal)
    20.         ObjExcel = Nothing

    I’m sure there is better way, I’ll try to find it out, but as you know you cannot sort all columns based on a specific one… anyway try this…

    Hope this helps…

    Regards,
    =======================================
    If I helped you, Kindly Rate my post. Thanks
    -----------
    PHENOM

  5. #5

    Thread Starter
    New Member
    Join Date
    Jun 2006
    Posts
    6

    Re: Sorting data excel

    Thanks phenom,

    Your first post helped me with the code I have below, I got it working before your last reply, but I'll definetly have a look at the code you posted. I think the biggest problem I seemed to have was how you select rows or columns for the range object. Anyway here's the code I have that selects the entire sheet (first 5000 rows is good enough for what I need) and sorts according to a specified column.

    Dim testRng, data As Range

    data = DirectCast(excelWorksheet.Rows("1:5000"), Excel.Range)
    testRng = excelWorksheet.Cells(1, ppColumn)
    data.Sort(key1:=testRng,_ Order1:=XlSortOrder.xlAscending,Header:=XlYesNoGuess.xlYes,_ Orientation:=Excel.XlSortOrientation.xlSortColumns)

    The ppColumn is just an integer to the column I'm sorting by.

    brwiese

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width