I am new to VBA coding and would like a VBA script to sort multiple columns. First, I sort column F from smallest to largest, and then sort column K. However, I need the Range value to be dynamic based on the name of the column, not the location (that is, the value in column F is called "Name", the Name is not always will be in column F)
I want to change all Range values in a macro, and I'm thinking of replacing it with the FIND function, am I on the right track?
those. Change Range _ ("F1: F10695")
for something like Range (Find(What:="Name", After:=ActiveCell, LookIn:=xlFormulas, _
LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
MatchCase:=False, SearchFormat:=False).Activate
Range(Selection, Selection.End(xlDown)).Select
I also saw some VBA script templates that use the Dim and Set functions to create lists, i.e. set x = "Name" and then sort by X in the matrix. Is this a better approach? Thank you for your help. I attached a basic VBA script template below
Sub Macro2()
'
' Macro2 Macro
'
'
Selection.AutoFilter
Range("F1").Select
ActiveWorkbook.Worksheets("Sheet1").AutoFilter.Sort.SortFields.Clear
ActiveWorkbook.Worksheets("Sheet1").AutoFilter.Sort.SortFields.Add Key:=Range _
("F1:F10695"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:= _
xlSortNormal
With ActiveWorkbook.Worksheets("Sheet1").AutoFilter.Sort
.Header = xlYes
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
Range("K1").Select
ActiveWorkbook.Worksheets("Sheet1").AutoFilter.Sort.SortFields.Clear
ActiveWorkbook.Worksheets("Sheet1").AutoFilter.Sort.SortFields.Add Key:=Range _
("K1:K10695"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:= _
xlSortNormal
With ActiveWorkbook.Worksheets("Sheet1").AutoFilter.Sort
.Header = xlYes
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
End Sub
source
share