Newbie : Help with sort via Excel code

  • Thread starter Thread starter Rich
  • Start date Start date
R

Rich

I keep getting this error message with my spreadsheet and I do not
know what it means. Can anyone help.

Error Message :
The sort reference is not valid. Make sure that it's within the data
you want to sort, and the first SORT BY box isn't the same or blank


Below is my source code:
For Each w In Worksheets
w.Activate
ActiveCell.SpecialCells(xlLastCell).Select
lnRange = CInt(Mid(ActiveCell.Address(RowAbsolute:=False), 3, 5))
lcCellRange = "A3:G" & Trim(Str(lnRange - 1))
w.Range(lcCellRange).Select
Selection.Sort Key1:=Range("C3"), Order1:=xlAscending,
Header:=xlNo, _
OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom

w.Range("A1").Select
Next w
 
Try this where:
n = First Row to be sorted
a = Last Row to be sorted
c = the number of the column to sort by (ie. Column "C" = 3, Column "E"
= 5 etc.)

For x = n To a
aNam = bp.Cells(x, 3).Value
z = x
For y = x + 1 To a
If bp.Cells(y, 3).Value < aNam Then
aNam = bp.Cells(y, 3).Value
z = y
End If
Next y

If z <> x Then
bp.Rows(z).Cut
bp.Rows(x).Insert
End If
Next x
 

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