Sorting a Non-contiguous range

Sorting a Non-contiguous range

nbauer-SCS
Explorer Explorer
285 Views
1 Reply
Message 1 of 2

Sorting a Non-contiguous range

nbauer-SCS
Explorer
Explorer

Hello all,

 

I'm wondering how I possibly write iLogic code to sort a non-contiguous range in Excel. 

 

Before my companies most recent revision to this code, we had 2 ranges in our Excel output that were sorted separately based on their own first columns. Because of other changes, I now have 3 separate ranges, say 1, 2, & 3, separated by empty columns. With 1 & 3 being an old continuous range. Yet, 1 & 3 need to be sorted TOGETHER as one range based on the first column of range 1. 

 

The code I'm trying to use is: 

'Declare ranges
oRng1
= .Workbooks(wb).Sheets(ws).Range("A" & subTitleRow + 1 & ":" & "G" & lastRow1 & "," & "N" & subTitleRow + 1 & ":" & "O" & lastRow1) oRng2 = .Workbooks(wb).Sheets(ws).Range("I" & subTitleRow+1 & ":" & "L" & lastRow2) 'Sort ranges by Part Number
oRng1.Sort(key1 :=.Range("A" & subTitleRow + 1 & ":" & "G" & lastRow1 & "," & "N" & subTitleRow + 1 & ":" & "O" & lastRow1), Header :=xlNo) oRng2.Sort(key1:=.Range("I" & subTitleRow+1 & ":" & "L" & lastRow2), Header:=xlNo)  

I end up getting this error:

nbauer_0-1667228554066.png

 

Note: line 625 is the oRng1.Sort line.

 

I only included these 4 lines of a 1200 line code that is otherwise working perfectly. As a matter of fact, if I change the first line to oRng1 = .Workbooks(wb).Sheets(ws).Range("A" & subTitleRow + 1 & ":" & "G" & lastRow1) everything works fine, EXCEPT columns N & O are not sorted. 

 

Any help would be appreciated. 

 

TlDR: How do you sort a non-contiguous range in Excel using iLogic?

0 Likes
286 Views
1 Reply
Reply (1)
Message 2 of 2

Michael.Navara
Advisor
Advisor

In my opinion this is not possible.

Your chance is to copy PartNumber column values near to the area N:O and sort this two areas separately. Later on you can this temp column delete.

 

Or if you don't need to sort source excel data, you can read the table to the memory (as list of cell values) and than you can sort this "virtual table". 

0 Likes