• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 293
  • Last Modified:

VBA Excel SORT using CELLS(r,c) for key reference

I have an Excel macro that uses Cells(r,c) references extensively, because the layout of the spreadsheet is highly variable. At some point, I need to sort the data, and the columns to be used as sort keys are determined when the macro runs.

The following syntax (from a recorded macro) works fine: Key1:=Range("A2").
With this syntax, the sort appears to do nothing: Key1:=Range(Cells(2,1)).
For most other purposes, Range("A2") and Range(Cells(2,1)) are interchangeable.  But apparently not for SORT.
Ultimately, I need the sort key to be dynamic: Key1:=Range(Cells(2, iDocNum)), where iDocNum is a column number.
What am I missing?

Thanks, Dave
1 Solution
Range(Cells(2,1)) is not valid syntax, but Cells(2,1) is.

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now