Sort by two columns using VB Script

  • Thread starter Thread starter Martin X.
  • Start date Start date
M

Martin X.

Hello,

I'm trying to sort an Excel spreadsheet by A1 then B1. I recorded a macro
and looked at the VBA code and it looks like I have the syntax correct, but
I get a "Reference is not valid" error. Below is some code with comments.
Thanks.

Set objExcelRange1 = objExcelWs.UsedRange
Set objExcelRange2 = objExcel.Range("A1")
Set objExcelRange3 = objExcel.Range("B1")
objExcelRange1.Sort objExcelRange2,1,,,,,,,1 '<< This works ok for sorting
by A1 only
objExcelRange1.Sort objExcelRange2,1,objExcelRange3,1,,,,,1 '<< I get an
error here
 
there is an additional argument between Key2 and Order2

expression.Sort(Key1, Order1, Key2, Type, Order2, Key3, Order3, Header,
OrderCustom, MatchCase, Orientation, SortMethod, DataOption1, DataOption2,
DataOption3)

it should be

objExcelRange1.Sort objExcelRange2,1,objExcelRange3,,1,,,,1
I would guess
 
Yep, that was it. Thanks.

--
Regards,
Martin X.
MCSA: M


there is an additional argument between Key2 and Order2

expression.Sort(Key1, Order1, Key2, Type, Order2, Key3, Order3, Header,
OrderCustom, MatchCase, Orientation, SortMethod, DataOption1, DataOption2,
DataOption3)

it should be

objExcelRange1.Sort objExcelRange2,1,objExcelRange3,,1,,,,1
I would guess
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top