VBA to sort worksheet

depcdivr

Active Member
Joined
Jan 21, 2008
Messages
349
Office Version
  1. 365
Platform
  1. Windows
This one has me stumped. I am trying to sort a data range in a spreadsheet in descending order, extract a few names and then resort on a different column and repeat. I rand the program the first time and it worked. Now it errors out on the sort line saying "the sort reference s not valid. Make sure that it's within the data you want to sort and the first sort box isn't the same or blank."

Here is the code i'm using.


VBA Code:
                Set wb = Workbooks.Open(strx & "\" & MyFile)
                Set ws = wb.Sheets("Rep Summary")
                Set wsPOS = wb.Sheets("POS Summary")

                    wsPOS.Sort.SortFields.Clear
                    irow = wsPOS.Cells(Rows.Count, 1).End(xlUp).Row
                    

                    wsPOS.Range(wsPOS.Cells(8, 1), wsPOS.Cells(irow - 1, 161)).Sort Key1:=Range("BC8"), Header:=xlNo, order1:=xlDescending
                    ws.Cells(3, 9) = wsPOS.Cells(8, 1)
                    ws.Cells(4, 9) = wsPOS.Cells(9, 1)
                    ws.Cells(5, 9) = wsPOS.Cells(10, 1)
                                
                    wsPOS.Range(wsPOS.Cells(8, 1), wsPOS.Cells(irow - 1, 161)).Sort Key1:=Range("DE8"), Header:=xlNo, order1:=xlDescending
                    ws.Cells(3, 10) = wsPOS.Cells(8, 1)
                    ws.Cells(4, 10) = wsPOS.Cells(9, 1)
                    ws.Cells(5, 10) = wsPOS.Cells(10, 1)
 

Excel Facts

Wildcard in VLOOKUP
Use =VLOOKUP("Apple*" to find apple, Apple, or applesauce
I'm not exactly sure of what the problem is, but if you shared the code where it errors out, then try this. ( I see redundant assignments)
VBA Code:
Set wb = Workbooks.Open(strx & "\" & MyFile)
                Set ws = wb.Sheets("Rep Summary")
                Set wsPOS = wb.Sheets("POS Summary")

                    wsPOS.Sort.SortFields.Clear
                    irow = wsPOS.Cells(Rows.Count, 1).End(xlUp).Row
                    

                    wsPOS.Range(Cells(8, 1), Cells(irow - 1, 161)).Sort Key1:=Range("BC8"), Header:=xlNo, order1:=xlDescending
                    ws.Cells(3, 9) = wsPOS.Cells(8, 1)
                    ws.Cells(4, 9) = wsPOS.Cells(9, 1)
                    ws.Cells(5, 9) = wsPOS.Cells(10, 1)
                                
                    wsPOS.Range(Cells(8, 1), Cells(irow - 1, 161)).Sort Key1:=Range("DE8"), Header:=xlNo, order1:=xlDescending
                    ws.Cells(3, 10) = wsPOS.Cells(8, 1)
                    ws.Cells(4, 10) = wsPOS.Cells(9, 1)
                    ws.Cells(5, 10) = wsPOS.Cells(10, 1)
 
Upvote 0
You need to qualify the Key ranges as well, like
VBA Code:
                Set wb = Workbooks.Open(strx & "\" & myFile)
                Set ws = wb.Sheets("Rep Summary")
                Set wsPos = wb.Sheets("POS Summary")

                    wsPos.Sort.SortFields.Clear
                    iRow = wsPos.Cells(Rows.Count, 1).End(xlUp).Row
                    

                    wsPos.Range(wsPos.Cells(8, 1), wsPos.Cells(iRow - 1, 161)).Sort Key1:=wsPos.Range("BC8"), Header:=xlNo, order1:=xlDescending
                    ws.Cells(3, 9) = wsPos.Cells(8, 1)
                    ws.Cells(4, 9) = wsPos.Cells(9, 1)
                    ws.Cells(5, 9) = wsPos.Cells(10, 1)
                                
                    wsPos.Range(wsPos.Cells(8, 1), wsPos.Cells(iRow - 1, 161)).Sort Key1:=wsPos.Range("DE8"), Header:=xlNo, order1:=xlDescending
                    ws.Cells(3, 10) = wsPos.Cells(8, 1)
                    ws.Cells(4, 10) = wsPos.Cells(9, 1)
                    ws.Cells(5, 10) = wsPos.Cells(10, 1)
 
Upvote 0
Solution

Forum statistics

Threads
1,214,981
Messages
6,122,566
Members
449,089
Latest member
Motoracer88

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top