0

I have checked all of the forums questions that relate to this topic. I am unable to get my code to work.

Here is the code. Please let me know what I need to correct.

Dim strDataRange As String
Dim strKeyRange As String

strDataRange = "B" & strStartRow & ":M" & strLastRow
strKeyRange = "B" & strStartRow & ":B" & strLastRow

Range(strDataRange).Sort Key1:=strKeyRange, Order1:=xlDescending,   Header:=xlNo

The strDataRange is B5:M18.

The strKeyRange is B5:B18.

2 Answers 2

1

use:

Range(strDataRange).Sort Key1:=Range(strKeyRange), Order1:=xlDescending, Header:=xlNo

since Sort method wants Key1parameter as "...sort field, either as a range name (String) or Range object"

Sign up to request clarification or add additional context in comments.

3 Comments

I changed to be "Range(strDataRange).Sort Key1:=Range(strKeyRange), Order1:=xlDescending, Header:=xlNo" This is still giving me an error 1004: "This operation requires the merged cells to be identically sized." I don't have any merged cells in the sort range.
Do you have any merged cell in B5:M18?
No. No merged cells at all in the sort range as stated above.
0

I found the issue. Since the VBA codes was on a different worksheet than where I was sorting, I needed to add the workbook reference; which is "ws".

here is the corrected code

 ws.Range(strDataRange).Sort Key1:=ws.Range(strKeyRange), Order1:=xlDescending, Header:=xlNo

Thanks for all your help.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.