简体   繁体   中英

Sorting two ranges in Excel from VB.NET not working

I'm creating an Excel workbook from VB.NET. Two ranges of data are entered into each sheet. The ranges are three columns wide by a variable number of rows. After the data gets entered it gets sorted. The sorting works on the first range, but not the second - it error's out.

The error: COMException was unhandled. The sort reference is not valid. make sure that it's within the data you want to sort, and the first Sort By box isn't the same or blank.

My code:

Dim xlApp As New Excel.Application
Dim xlWB As Excel.Workbook
Dim xlSht As Excel.Worksheet
Dim Rng As Excel.Range
Dim Rng2 As Excel.Range
'
Dim dic As Dictionary(Of UInteger, cVM)
Dim irow As UInteger
'
' create a new Excel Workbook instance
xlWB = xlApp.Workbooks.Add
'
' create a worksheet for each group
For Each key In dGroup.Keys 
    '
    ' create worksheet
    xlSht = xlWB.Sheets.Add 
    xlSht.Name = key 
    '
    '
    ' =======================================================
    irow = 4 
    '
    ' output critical data, by Group
    dic = New Dictionary(Of UInteger, cVM)
    dic = dGroup(key).Dat
    For Each lc In dic.Keys
        xlSht.Cells(irow, 1) = lc
        xlSht.Cells(irow, 2) = dic(lc).VM
        xlSht.Cells(irow, 3) = dic(lc).EID
        irow += 1
    Next
    '
    ' sort the output
    Rng = xlSht.Range("A4", "C10000")
    Rng.Select()
    Rng.Sort(Key1:=Rng.Range("B4"), _
             Order1:=Excel.XlSortOrder.xlDescending, _
             Orientation:=XlSortOrientation.xlSortColumns) ' <== THIS WORKS !!!
    '
    '
    '
    ' =======================================================
    irow = 4 
    ' output critical data, by Group
    dic = New Dictionary(Of UInteger, cVM)
    dic = dGroup2(key).Dat
    For Each lc In dic.Keys
        xlSht.Cells(irow, 5) = lc
        xlSht.Cells(irow, 6) = dic(lc).VM
        xlSht.Cells(irow, 7) = dic(lc).EID
        irow += 1
    Next
    '
    ' sort the output
    Rng2 = xlSht.Range("E4", "G10000")
    Rng2.Select()
    Rng2.Sort(Key1:=Rng2.Range("F4"), _
              Order1:=Excel.XlSortOrder.xlDescending, _
              Orientation:=XlSortOrientation.xlSortColumns) ' <== THIS IS WHERE IT ERRS OUT
'
Next key        

The first range is from A4:C10000. The second range is from E4:G10000.

Sorting the first range works fine. Sorting the second range does not work. What I don't understand is why? It's the same code except I changed the range. What did I miss?

Rng2.Range("F4") is actually J7 (because the F4 is relative to Rng2, not to the parent worksheet) and outside of the range you're trying to sort.

You probably want

Key1:=xlSht.Range("F4") 

instead.

Similar for your first sort: you didn't get an error on there because Rng.Range("B4") (B7) is still within your sort range.

Try this code:

Rng2 = xlSht.Range("E4", "G10000")

Rng2.Sort(Key1:=Rng2.Cells(1,2), _ Order1:=Excel.XlSortOrder.xlDescending, _ Orientation:=XlSortOrientation.xlSortColumns)

The reason your code fails: Rng2.Range("F4") does not refer to the cell "F4" in the sheet, it refers to cell "F4" in the Rng2. "F4" is not an absolute reference - it's relative to the upper left corner of the Rng2 (in your case, "F4" refers to the absolute cell "J7" (from cell "E4", offset 6 columns to the right, 4 columns down). Your first sort works because it starts in column "A", and (accidently) absolute reference and relative reference coinside.

BTW, there is no need to "select" range, just use the object sort method: range.sort()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM