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

How to change case of text in Excel?
Use =UPPER() for upper case, =LOWER() for lower case, and =PROPER() for proper case. PROPER won't capitalize second c in Mccartney
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 ?
 
Upvote 0
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.
 
Upvote 0
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
 
Upvote 0
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
 
Upvote 0
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
 
Upvote 0
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
 
Upvote 0

Forum statistics

Threads
1,214,944
Messages
6,122,384
Members
449,080
Latest member
Armadillos

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
Back
Top