sorting in a macro and defining last row into varible not working in

S

S Himmelrich

Dim LastRow99 As Long

LastRow99 = .Cells(.Rows.Count, "A").End(xlUp).Row

ActiveWorkbook.Worksheets("Working Sheet").Sort.SortFields.Clear
ActiveWorkbook.Worksheets("Working Sheet").Sort.SortFields.Add
Key:=Range( _
LastRow99), SortOn:=xlSortOnValues, Order:=xlAscending,
DataOption:= _
xlSortNormal


I get Run-time error '1004':
Method 'Range' of object '_Global' failed
 
N

NOPIK

You pass Long as argument to Range.
May be, you should redefine LastRow99, and use End(xlUp).EntireRow
range
 
D

Dave Peterson

You have to specify the range that you want sorted. I used A1 through X(lastrow
in column A). You'll want to change that to use the column you need.

Dim LastRow99 As Long

with activeworkbook.worksheets("working sheet")
lastRow99 = .Cells(.Rows.Count, "A").End(xlUp).Row
.Sort.SortFields.Clear
.Sort.SortFields.Add Key:=.Range("A1:X" & LastRow99), _
SortOn:=xlSortOnValues, Order:=xlAscending, _
DataOption:=xlSortNormal
 
S

S Himmelrich

needing to sort on column A, C, F & G

You have to specify the range that you want sorted.  I used A1 through X(lastrow
in column A).  You'll want to change that to use the column you need.

Dim LastRow99 As Long

with activeworkbook.worksheets("working sheet")
    lastRow99 = .Cells(.Rows.Count, "A").End(xlUp).Row
    .Sort.SortFields.Clear
    .Sort.SortFields.Add Key:=.Range("A1:X" & LastRow99), _
         SortOn:=xlSortOnValues, Order:=xlAscending, _
         DataOption:=xlSortNormal
 
D

Dave Peterson

Record a macro when you do it manually. Remember, since you're sorting on 4
fields, you'll have to do the sort twice.
 

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

Similar Threads


Top