Sorting attempt results in 400 error

mcomp72

Active Member
Joined
Aug 14, 2016
Messages
275
Office Version
  1. 365
  2. 2019
  3. 2016
  4. 2011
Platform
  1. Windows
  2. MacOS
I am suddenly getting a 400 error while trying to sort data on a worksheet, and I can’t figure out why. Here’s my code:

Code:
Dim SortRange As String
Dim KeySortCol As String
Dim LastColumn As String
Dim LastRow as Integer
 
ThisWorkbook.Sheets("Sheet 2").Activate
 
LastColumn = ThisWorkbook.Sheets("Sheet 5").Range("B80").Value
 
LastRow = ThisWorkbook.Sheets("Sheet 2").Cells(Rows.Count, 1).End(xlUp).Row
 
SortRange = "A2:" & LastColumn & LastRow
 
KeySortCol = "A2:A" & LastRow
 
ThisWorkbook.Sheets("Sheet 2").Range(SortRange).Sort key1:=Range(KeySortCol), order1:=xlAscending, Header:=xlNo

The 400 error happens on the last line.

I stepped through the code and watched the variables, and here is what they are equal to:

LastColumn = “BU”
LastRow = 5
SortRange = “A2:BU5”
KeySortCol = “A2:A5”

I had something similar working in an earlier version of this workbook, so I can’t figure out what I did that would suddenly cause the 400 error. Anyone have any ideas?
 

Excel Facts

How to fill five years of quarters?
Type 1Q-2023 in a cell. Grab the fill handle and drag down or right. After 4Q-2023, Excel will jump to 1Q-2024. Dash can be any character.
I tried moving the sub with this code from a Sheet module to a regular module, and the problem went away. I'm guessing you cannot do a sort in VBA from within a Sheet module?
 
Upvote 0
I'm guessing you cannot do a sort in VBA from within a Sheet module?
NO !!
- in a sheet module all unqualified ranges refer to that sheet
- "ACTIVATING" another sheet does not change that rule
- you have (inadvertently) mixed "apples" and "pears"
- the KEY range (in red) is NOT in your sort data (it is on a different sheet) - hence the error

This does not work UNLESS it is in the sheet module for "Sheet 2"
Code:
ThisWorkbook.Sheets("Sheet 2").Range(SortRange).Sort key1:=[COLOR=#ff0000]Range(KeySortCol)[/COLOR], order1:=xlAscending, Header:=xlNo

But this will work inside ANY sheet module
Code:
ThisWorkbook.Sheets("Sheet 2").Range(SortRange).Sort key1:=[COLOR=#ff0000]ThisWorkbook.Sheets("Sheet 2").Range(KeySortCol)[/COLOR], order1:=xlAscending, Header:=xlNo
 
Last edited:
Upvote 0
Oh geez, I can't believe I missed that unqualified range. Sometimes when you stare at code too long, you miss the simplest things! :p
 
Upvote 0
The code does not contain an error. It is simply incomplete. So it does not jump out. We all fall foul of that problem! :oops:
 
Upvote 0

Forum statistics

Threads
1,214,585
Messages
6,120,390
Members
448,957
Latest member
Hat4Life

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