VBA Help

Miya

Well-known Member
Joined
Nov 29, 2008
Messages
662
Hi, the below recorded macro sorts data in ascending order in Col C, I need this code to be cleaned up so that it is flexible with the ranges as the data will not always finish at row 650

.SetRange Range("A5:J650")
:=Range("C6:C650"), _


Code:
Sub Macro2()
'
' Macro2 Macro
'
'
    Range("A5").Select
    Range(Selection, Selection.End(xlToRight)).Select
    Range(Selection, Selection.End(xlDown)).Select
    ActiveWorkbook.Worksheets("Rec").Sort.SortFields.Clear
    ActiveWorkbook.Worksheets("Rec").Sort.SortFields.Add Key:=Range("C6:C650"), _
        SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
    With ActiveWorkbook.Worksheets("Rec").Sort
        .SetRange Range("A5:J650")
        .Header = xlYes
        .MatchCase = False
        .Orientation = xlTopToBottom
        .SortMethod = xlPinYin
        .Apply
    End With
End Sub
 

Excel Facts

What do {} around a formula in the formula bar mean?
{Formula} means the formula was entered using Ctrl+Shift+Enter signifying an old-style array formula.

matthew230

Board Regular
Joined
Jan 2, 2006
Messages
152
Will you have continous data ?

How are you planning to determine which cell will be the last one if is not 650 ?

Is their any data in the cells beyond the last cell required ?
 

Miya

Well-known Member
Joined
Nov 29, 2008
Messages
662
Will you have continous data ?

How are you planning to determine which cell will be the last one if is not 650 ?

Is their any data in the cells beyond the last cell required ?

There isnt any data after the last cell, but when i do CTRL * for current region it selects the data beyond the data range, i do have formatting beyond the data range, but i thought that would not effect it. I did CTRL End and the cursor pointed Cell V1433, i deleted all rows after the range and its still points to V1433, so when i run the sort manually i get blank rows and the date is futher down.
 

lenze

Legend
Joined
Feb 18, 2002
Messages
13,690
Code:
Range("$C$6:$J" & Range("$J$65536").End(xlUp).Row)

lenze
 

Miya

Well-known Member
Joined
Nov 29, 2008
Messages
662
Code:
Range("$C$6:$J" & Range("$J$65536").End(xlUp).Row)

lenze

lenze where in the code do i put your line, is there any lines that can be taken out?

Code:
Range("A5").Select
    Range(Selection, Selection.End(xlToRight)).Select
    Range(Selection, Selection.End(xlDown)).Select
    ActiveWorkbook.Worksheets("Rec").Sort.SortFields.Clear
    ActiveWorkbook.Worksheets("Rec").Sort.SortFields.Add Key:=Range("C6:C650"), _
        SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
    With ActiveWorkbook.Worksheets("Rec").Sort
        .SetRange Range("A5:J650")
        .Header = xlYes
        .MatchCase = False
        .Orientation = xlTopToBottom
        .SortMethod = xlPinYin
        .Apply
    End With
 

lenze

Legend
Joined
Feb 18, 2002
Messages
13,690
Replace what you have
Code:
.SetRange Range("A5:J650")
becomes
Code:
.setRange Range("$C$6:$J" & Range("$J$65536").End(xlUp).Row)
same for your columns C
lenze
 

Miya

Well-known Member
Joined
Nov 29, 2008
Messages
662
Replace what you have
Code:
.SetRange Range("A5:J650")
becomes
Code:
.setRange Range("$C$6:$J" & Range("$J$65536").End(xlUp).Row)
same for your columns C
lenze

Ok i did that, and i got a run time error 1004, the sort ref is not valid

.Apply

Code:
 Range("A5").Select
    Range(Selection, Selection.End(xlToRight)).Select
    Range(Selection, Selection.End(xlDown)).Select
    ActiveWorkbook.Worksheets("Rec").Sort.SortFields.Clear
    ActiveWorkbook.Worksheets("Rec").Sort.SortFields.Add Key:=Range("$C$6:$J" & Range("$J$65536").End(xlUp).Row), _
        SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
    With ActiveWorkbook.Worksheets("Rec").Sort
        .SetRange Range("$C$6:$J" & Range("$J$65536").End(xlUp).Row)
        .Header = xlYes
        .MatchCase = False
        .Orientation = xlTopToBottom
        .SortMethod = xlPinYin
        .Apply
    End With
 

Miya

Well-known Member
Joined
Nov 29, 2008
Messages
662
ok i tried to trim the code down to this, but when i run the code i am still getting blank rows and the data somewhere in the middle, what have i done wrong?

With .Range("$A$5:$M" & Range("$M$65536").End(xlUp).Row)
.Sort Key1:=.Range("$D$6:$M" & Range("$M$65536").End(xlUp).Row), Order1:=xlAscending, Header:=xlYes, _
OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom, _
DataOption1:=xlSortNormal
End With
 

Forum statistics

Threads
1,171,864
Messages
5,877,961
Members
433,304
Latest member
niresh28

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Top