Forum Discussion
AKuma0411
Nov 20, 2024Brass Contributor
Macro to sort data
Hello, I want help with macro code: Here's the macro I'm using: Sub ClearAndSortDataAutomatically() Dim ws As Worksheet Set ws = ThisWorkbook.Sheets("Review") ' Ensure this matches your a...
- Nov 20, 2024
The problem is that the Set# column contains some numbers, some pure text values and some text values that look like numbers. This is the nearest I can get:
Sub ClearAndSortDataAutomatically() Dim ws As Worksheet Set ws = ThisWorkbook.Sheets("Review") ' Ensure this matches your actual sheet name ' Unmerge any merged cells in the specified range if necessary 'ws.Range("A8:P84").UnMerge ' Adjust the range as needed ' Perform the sort specifically for the range K9 to K84 With ws.Sort ' Clear existing sort settings .SortFields.Clear ' Sort by Segment (Column F) Z to A .SortFields.Add Key:=ws.Range("F8:F84"), _ SortOn:=xlSortOnValues, Order:=xlDescending, DataOption:=xlSortNormal ' Sort by Indicator (Column E) A to Z .SortFields.Add Key:=ws.Range("E8:E84"), _ SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal ' Sort by Total Procedure (Column P) Largest to Smallest .SortFields.Add Key:=ws.Range("P8:P84"), _ SortOn:=xlSortOnValues, Order:=xlDescending, DataOption:=xlSortNormal ' Sort by Set# (Column K) A to Z .SortFields.Add Key:=ws.Range("K8:K84"), _ SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortTextAsNumbers ' Perform the actual sort limited to rows 9 through 84 .SetRange ws.Range("B8:P84") ' Adjust range as needed within the specified limits .Header = xlYes ' Indicates that the first row is a header .MatchCase = False .Orientation = xlTopToBottom .SortMethod = xlPinYin .Apply End With ' After sorting, merge the cells from A9 to A44 'ws.Range("A9:A44").Merge ' Merge the specified range End Sub
HansVogelaar
Nov 20, 2024MVP
The problem is that the Set# column contains some numbers, some pure text values and some text values that look like numbers. This is the nearest I can get:
Sub ClearAndSortDataAutomatically()
Dim ws As Worksheet
Set ws = ThisWorkbook.Sheets("Review") ' Ensure this matches your actual sheet name
' Unmerge any merged cells in the specified range if necessary
'ws.Range("A8:P84").UnMerge ' Adjust the range as needed
' Perform the sort specifically for the range K9 to K84
With ws.Sort
' Clear existing sort settings
.SortFields.Clear
' Sort by Segment (Column F) Z to A
.SortFields.Add Key:=ws.Range("F8:F84"), _
SortOn:=xlSortOnValues, Order:=xlDescending, DataOption:=xlSortNormal
' Sort by Indicator (Column E) A to Z
.SortFields.Add Key:=ws.Range("E8:E84"), _
SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
' Sort by Total Procedure (Column P) Largest to Smallest
.SortFields.Add Key:=ws.Range("P8:P84"), _
SortOn:=xlSortOnValues, Order:=xlDescending, DataOption:=xlSortNormal
' Sort by Set# (Column K) A to Z
.SortFields.Add Key:=ws.Range("K8:K84"), _
SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortTextAsNumbers
' Perform the actual sort limited to rows 9 through 84
.SetRange ws.Range("B8:P84") ' Adjust range as needed within the specified limits
.Header = xlYes ' Indicates that the first row is a header
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
' After sorting, merge the cells from A9 to A44
'ws.Range("A9:A44").Merge ' Merge the specified range
End Sub