VBA,为sorting数据编码一个dynamic范围(列)

我是一个vba中的新手,我被困在一些应该相对容易的东西:我有一个marco指定标题名称的列号:

Dim onomata As Integer 'find column named Name of ship Set acell = rows(1).Find(What:="Name of ship", LookIn:=xlValues, _ LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, _ MatchCase:=False, SearchFormat:=False) If Not acell Is Nothing Then onomata = acell.Column End If 

现在我想根据这个列对数据进行sorting:

 Cells.Select ActiveWorkbook.ActiveSheet.Sort.SortFields.Clear ActiveWorkbook.ActiveSheet.Sort.SortFields.Add Key:=Range( *this is where I want to introduce the column* ), _ SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:= _ xlSortTextAsNumbers With ActiveWorkbook.ActiveSheet.Sort .SetRange Range("A1:AR100000") .Header = xlYes .MatchCase = False .Orientation = xlTopToBottom .SortMethod = xlPinYin .Apply End With 

但是,我的variables是整数,而命令请求范围值。 任何人都可以帮助我如何编码?

ActiveSheet就足够了,使用ActiveWorkbook.ActiveSheet没有意义。

使用下面的代码根据你得到的数字结果进行onomata

 With ActiveSheet .Sort.SortFields.Clear .Sort.SortFields.Add Key:=Columns(onomata), SortOn:=xlSortOnValues, _ Order:=xlAscending, DataOption:=xlSortTextAsNumbers End With 

你只需要这个…

 Dim acell As Range 'find column named Name of ship Set acell = Rows(1).Find(What:="Name of ship", LookIn:=xlValues, _ LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, _ MatchCase:=False, SearchFormat:=False) If Not acell Is Nothing Then ActiveSheet.Sort.SortFields.Clear ActiveSheet.Range("A1").Sort key1:=acell.Offset(1, 0), order1:=xlAscending, Header:=xlYes End If 
 ActiveSheet.Sort.SortFields.Add Key:= ActiveSheet.Cells(onomata).EntireColumn