Auto Sorting after inputting data using UserForm - excel

I've made a UserForm to input data to specific columns on a worksheet("Endorse"), and it works fine.
After that, I have used a code to automatically sort the sheet based on the the values on Column A, B and C. But the sheet does not sort based on the criteria:
Column A: oldest to recent (date)
Column B: ascending order by: "BSUH (September),Frimley (October),CWH (November),Kingston (December)"
Column C: ascending order by: Allen,Christine,Feri,Hubert,Paula"
here is the code that i have used for the sheet("Endorse"):
Private Sub Worksheet_Change(ByVal Target As Range)
LastRow = Range("L4000").End(xlUp).Row
ActiveWorkbook.Worksheets("Endorsed Candidates").Sort.SortFields.Clear
ActiveWorkbook.Worksheets("Endorsed Candidates").Sort.SortFields.Add Key:= _
Range("A2:A" & LastRow), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption _
:=xlSortNormal
ActiveWorkbook.Worksheets("Endorsed Candidates").Sort.SortFields.Add Key:= _
Range("B2:B" & LastRow), SortOn:=xlSortOnValues, Order:=xlAscending, CustomOrder _
:="BSUH (September),Frimley (October),CWH (November),Kingston (December)", _
DataOption:=xlSortNormal
ActiveWorkbook.Worksheets("Endorsed Candidates").Sort.SortFields.Add Key:= _
Range("C2:C4000"), SortOn:=xlSortOnValues, Order:=xlAscending, CustomOrder _
:="Allen,Christine,Feri,Hubert,Paula", DataOption:=xlSortNormal
With ActiveWorkbook.Worksheets("Endorsed Candidates").Sort
.SetRange Range("A2:L" & LastRow)
.Header = xlGuess
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
End Sub
Here is the sheet after using the UserForm:
[enter image description here][1]
*ps I can't post an image yet in my question, hope you guys understand. And many thanks to those who could help!

Related

VBA Find NAMED column and filter - Nearly working macro

I am struggling to cover everything I need to do in one macro - I asked a similar question here a few hours ago and was answered however unfortunately I needed to add a few functions to my macro so I had to modify it slightly and now I require a tiny tweak that I can't get to work
Sub BoBTEST()
Dim c As Range
For Each c In Range("A1:BR1").Cells
If c.Value = "Plate Name (barcode)" Or c.Value = "Measurement Date" Or c.Value = "Measurement profile" Or c.Value = "pH" Or c.Value = "Count" Or c.Value <= 30 Then
c.EntireColumn.Hidden = False
Else: c.EntireColumn.Hidden = True
End If
Next c
ActiveWorkbook.Worksheets("Sheet1").Sort.SortFields.Clear
ActiveWorkbook.Worksheets("Sheet1").Sort.SortFields.Add2 Key:=Range( _
"AF2:AF1761"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:= _
xlSortNormal
ActiveWorkbook.Worksheets("Sheet1").Sort.SortFields.Add2 Key:=Range( _
"AN2:AN1761"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:= _
xlSortNormal
ActiveWorkbook.Worksheets("Sheet1").Sort.SortFields.Add2 Key:=Range( _
"J2:J1761"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:= _
xlSortNormal
With ActiveWorkbook.Worksheets("Sheet1").Sort
.SetRange Range("A1:BR1761")
.Header = xlYes
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
Range("A1:BR1761").Select
Range("AT9").Activate
Selection.AutoFilter
Selection.AutoFilter
ActiveSheet.Range("$A$1:$BR$1761").AutoFilter Field:=32, Criteria1:= _
"=Shear Rated(gamma)/dt = 4 1/s", Operator:=xlOr, Criteria2:="="
ActiveSheet.Range("$A$1:$BR$1761").AutoFilter Field:=40, Criteria1:= _
"=Viscosity", Operator:=xlOr, Criteria2:="="
End Sub
Basically what it does so far is takes data which is exported from a robot, hides the columns that I don't want and filters the values that I don't want
The problem is that these columns are dynamic and occasionally move around - the macro is able to hide all of the columns I don't want however I'm struggling to make it search for these columns and then filter, right now it is using a recording I did so it selects e.g. column J and then filters
ActiveWorkbook.Worksheets("Sheet1").Sort.SortFields.Add2 Key:=Range( _
"J2:J1761"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:= _
xlSortNormal
However it may not be on column J it might be at K as occasionally more columns are entered when exporting data
Is there any way to tweak the macro to make it so the auto-filter part actually searches for column headers as opposed to doing these functions on e.g. "J2:J2000" etc.
Thanks a lot apologies that it is long winded I wanted to cover everything

Excel Macro to perform multi column sort

I'm trying to create a macro in Excel that will sort spreadsheets by columns A, C and F. I found information online to get me started. Below is the code. The challenge I'm having is, the spreadsheet will contain a different number of rows each day. (The columns will always be the same but the row count will change). The below script will sort my data as long as the spreadsheet does not contain more than 9999 rows (that includes column headings). If I have 10,000 or more rows, the macro fails.
How do I update the below code to allow it to run regardless of the number of rows? Any help you can provide would be appreciated. Thank you
Sub Multi_Sort()
'
' Multi_Sort Macro
'
Dim lngLast As Long
lngLast = Range("A" & Rows.Count).End(xlUp).Row
With Worksheets("Sheet1").Sort
.SortFields.Clear
.SortFields.Add Key:=Range("A1:A1" & lngLast), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
.SortFields.Add Key:=Range("C1:C1" & lngLast), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
.SortFields.Add Key:=Range("F1:F1" & lngLast), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
.SetRange Range("A1:F" & lngLast)
.Header = xlYes
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
End Sub
I can't replicate your error in Excel 2013, but I'm guessing the extra "1" in your range assignments is the problem (i.e., change =Range("A1:A1" & lngLast) to =Range("A1:A" & lngLast), etc.).
Also, a good practice would be to be explicit in your range calls (include the worksheet).
Sub Multi_Sort()
Dim lngLast As Long
Dim ws As Worksheet
Set ws = ThisWorkbook.Worksheets("Sheet1")
lngLast = ws.Range("A" & .Rows.Count).End(xlUp).Row
With .Sort
.SortFields.Clear
.SortFields.Add Key:=ws.Range("A1:A" & lngLast), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
.SortFields.Add Key:=ws.Range("C1:C" & lngLast), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
.SortFields.Add Key:=ws.Range("F1:F" & lngLast), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
.SetRange ws.Range("A1:F" & lngLast)
.Header = xlYes
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
End Sub

Automatic Multi-level Sorting in VBA

I currently have the following code that works and every time something is typed in, it is automatically sorted by column I
Private Sub Worksheet_Change(ByVal Target As Excel.Range)
If Target.Column = 9 Then
Dim lastRow As Long
lastRow = ActiveSheet.Cells.Find(What:="*", LookIn:=xlValues,
SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
Range("A2:I" & lastRow).Sort Key1:=Range("I2:I" & lastRow),
Order1:=xlAscending, Header:=xlNo
End If
End Sub
I want it so that it is primarily sorted by item I, but also secondarily sorted by column A. Any help please?
The starting point for any request like this should be "Fire up the macro recorder, perform a sort, and see what code it spits out".
If you fire up the recorder and add a multilevel sort like so:
...then the Macro Recorder spits out this:
Sub Macro11()
'
' Macro11 Macro
'
'
ActiveWorkbook.Worksheets("Sheet1").Sort.SortFields.Clear
ActiveWorkbook.Worksheets("Sheet1").Sort.SortFields.Add Key:=Range("I2:I16") _
, SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
ActiveWorkbook.Worksheets("Sheet1").Sort.SortFields.Add Key:=Range("A2:A16") _
, SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
With ActiveWorkbook.Worksheets("Sheet1").Sort
.SetRange Range("A1:I16")
.Header = xlYes
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
End Sub
This gives you a clue as to the syntax you need to add multiple sort keys. Do you need help amending your original code, or is this enough to get you started?

How to sort filtered data in vba?

I have a worksheet populated with data. I need to filter the data that could only show the info w/in 5 miles. Once the data is filtered to w/in 5 miles, I need to sort the variance column in ascending order. I used a record macro and attempted to incorporate it. The filtering works fine but I get an error saying:
Compile Error: Expected Array
when I run it. Here is a snippet of my code. When the error pops up, the Range is highlighted on the 6th line of code.
.Worksheets("Market Work").Cells.Select
Selection.AutoFilter
.Worksheets("Market Work").Range("$A$1:$Q$" & RowLast2).AutoFilter Field:=5, Criteria1:= _
"w/in 5 miles"
ActiveWorkbook.Worksheets("Market Work").Sort.SortFields.Clear
ActiveWorkbook.Worksheets("Market Work").Sort.SortFields.Add Key:=Range( _
"G2:G112"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:= _
xlSortNormal
With ActiveWorkbook.Worksheets("Market Work").Sort
.SetRange Range("A1:Q112")
.Header = xlYes
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
Following code will help you...
Sheets("Finnet").Select
Sheets("Finnet").AutoFilterMode = False
Sheets("Finnet").Range("A1", Range("XFD1").End(xlToLeft)).Select
Sheets("Finnet").Range(Selection, Range("A" & Rows.Count).End(xlUp)).AutoFilter
Sheets("Finnet").AutoFilter.Sort.SortFields.Add Key:=Range("A1:A" & Range("A" & Rows.Count).End(xlUp).Row), SortOn:=xlSortOnValues, Order:=xlAscending
Sheets("Finnet").AutoFilter.Sort.Header = xlYes
Sheets("Finnet").AutoFilter.Sort.Apply
Sheets("Finnet").AutoFilterMode = False

Sort Numbers/Text in one column

I have wrote the following code to sort data on my workbook
With gwksSheet
With .Sort
.SortFields.Clear
.SortFields.Add Key:=Columns(glAssetTypeCol), _
SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
.SortFields.Add Key:=Columns(glFundCodeCol), _
SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
.SortFields.Add Key:=Columns(glOberonCol), _
SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
.SetRange Range(Cells(1, 1), Cells(glLastRow, glLastCol))
.Header = xlYes
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
In the column "glOberonCol" I have numbers (e.g. 2561236) & Text (e.g. 2561236R). When it sorts it sorts the numbers and then the text. I want to sort it so I get 2561236 & 2561236R beside each other. What do I need to do. Appreciate any help.
Thanks,
Ciaran.
Follow the steps bellow:
Create another column
Add formula "=TEXT(RC[-1],"###")"
Copy this value and override the old values forcing Excel to see numbers as strings.
Now you can use your macro or just use Filter button.

Resources