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
.
use:
Range(strDataRange).Sort Key1:=Range(strKeyRange), Order1:=xlDescending, Header:=xlNo
since Sort method wants Key1
parameter as "...sort field, either as a range name (String) or Range object"
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.
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.