VBA代码按列名sorting多个列,不同的位置

我是新来的VBA编码,并希望一个VBA脚本sorting多列。 我首先将列F从最小到最大sorting,然后对列K进行sorting。但是,我希望范围值是基于列名而不是位置的dynamic值(即列F中的值称为“名称”,但“名字“并不总是在F列)

我正在寻找更改macros中的所有范围值,并且正在考虑用一个FIND函数来replace它,我在正确的轨道上吗?

即改变范围_(“F1:F10695”)

到像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

我也看到一些使用Dim和Set函数创build列表的VBA脚本模板,即设置x =“Name”,然后在matrix中对X进行sorting。 这是一个更好的方法吗? 感谢您的帮助,我附上了下面的基本VBA脚本模板

 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 

UPD:

试试这个:

 Sub test() Dim rngName As Range Dim rngDate As Range Dim emptyDates As Range Dim ws As Worksheet Dim lastrow As Long Set ws = ThisWorkbook.Worksheets("Sheet1") With ws Set rngName = .Range("1:1").Find(What:="Name", MatchCase:=False) Set rngDate = .Range("1:1").Find(What:="Date", MatchCase:=False) If Not rngName Is Nothing Then lastrow = .Cells(.Rows.Count, rngName.Column).End(xlUp).Row On Error Resume Next Set emptyDates = .Range(rngDate, .Cells(lastrow, rngDate.Column)).SpecialCells(xlCellTypeBlanks) On Error GoTo 0 If Not emptyDates Is Nothing Then emptyDates.EntireRow.Delete End If End If With .Sort .SortFields.Clear If Not rngName Is Nothing Then .SortFields.Add Key:=rngName, _ SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal End If If Not rngDate Is Nothing Then .SortFields.Add Key:=rngDate, _ SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal End If .SetRange ws.Cells .Header = xlYes .MatchCase = False .Orientation = xlTopToBottom .SortMethod = xlPinYin .Apply End With End With End Sub 

笔记:

  1. ThisWorkbook.Worksheets("Sheet1")中的ThisWorkbook.Worksheets("Sheet1")更改为对您来说正确的表单名称
  2. 代码尝试在第一行中查找“Name”和“Date”,然后如果find这些项目,则添加与该列对应的SortFields
  3. 作为评论的后续,OP还希望删除空date的行

按照这个testing代码与dinam最后一个单元格的7列。 由RCC66

 Sub Auto_Open() ' Order by Dim LastRow As Integer With ActiveSheet intLastRow = .Cells(.Rows.Count, "B").End(xlUp).Row End With ActiveWorkbook.Worksheets("Invoices").Sort.SortFields.Clear ActiveWorkbook.Worksheets("Invoices").Sort.SortFields.Add Key:=Range( _ "Q3:Q" & intLastRow), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:= _ xlSortNormal ActiveWorkbook.Worksheets("Invoices").Sort.SortFields.Add Key:=Range( _ "L3:L" & intLastRow), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:= _ xlSortNormal ActiveWorkbook.Worksheets("Invoices").Sort.SortFields.Add Key:=Range( _ "O3:O" & intLastRow), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:= _ xlSortNormal ActiveWorkbook.Worksheets("Invoices").Sort.SortFields.Add Key:=Range( _ "J3:J" & intLastRow), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:= _ xlSortNormal ActiveWorkbook.Worksheets("Invoices").Sort.SortFields.Add Key:=Range( _ "B3:B" & intLastRow), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:= _ xlSortNormal ActiveWorkbook.Worksheets("Invoices").Sort.SortFields.Add Key:=Range( _ "H3:H" & intLastRow), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:= _ xlSortNormal ActiveWorkbook.Worksheets("Invoices").Sort.SortFields.Add Key:=Range( _ "E3:e" & intLastRow), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:= _ xlSortNormal With ActiveWorkbook.Worksheets("Invoices").Sort .SetRange Range("A1:R" & intLastRow) .Header = xlYes .MatchCase = False .Orientation = xlTopToBottom .SortMethod = xlPinYin .Apply End With End Sub