Auto Sort Column base on Value

mrsec

Board Regular
Joined
Jan 28, 2016
Messages
54
Office Version
  1. 2016
Platform
  1. Windows
Hello,
I was trying to record a macro to auto sort my data in custom order
1.Pending
2.Actioned

But I needed it to auto sort everytime I added data from userform to my worksheet.

I have 20 columns and the column I need my data to sort according to its values are based on Column 7(H)

VBA Code:
Sub Sort()
    ActiveWorkbook.Worksheets("Data").Sort.SortFields.Clear
    ActiveWorkbook.Worksheets("Data").Sort.SortFields.Add2 Key:=Range("G2:G73"), _
        SortOn:=xlSortOnValues, Order:=xlAscending, CustomOrder:="Pending", _
        DataOption:=xlSortNormal
    With ActiveWorkbook.Worksheets("Data").Sort
        .SetRange Range("A1:T73")
        .Header = xlYes
        .MatchCase = False
        .Orientation = xlTopToBottom
        .SortMethod = xlPinYin
        .Apply
    End With
    ActiveWindow.SmallScroll Down:=0
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
You have to put code behind the userform before you unload it

VBA Code:
Sub jec()
 With Sheets("Data")
    .Range("A:T73").Sort .Range("H2"), 1, , , , , , xlYes, "Pending"
 End With
End Sub
 
Upvote 0
You have to put code behind the userform before you unload it

VBA Code:
Sub jec()
 With Sheets("Data")
    .Range("A:T73").Sort .Range("H2"), 1, , , , , , xlYes, "Pending"
 End With
End Sub
[/QUOTE]
I cant seem to make it work..??
 
Upvote 0

Forum statistics

Threads
1,215,299
Messages
6,124,125
Members
449,142
Latest member
championbowler

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