我想根据姓氏(第1列)对我的工作表进行排序。工作表上有一个form control
按钮,可根据按钮名称过滤信息。每次点击这些form control
按钮时,我都想按字母顺序对信息进行排序。所以我在宏上使用了record
看看excel做了什么来熟悉它,但我很困惑......问题是r
和countA
它给了我错误type mismatch
总共我有17列(A到Q - 但我不想真正设置列的范围,以防我以后添加更多列),其中包含与last name
相关的信息是第1列,从第3行开始
Sub btnPrinceRupert()
Dim ws As Worksheet
Dim r As Range
Set r = ws.Cells(2, 1)
Set ws = Worksheets("Master")
Call filterMyTable(xPrinceRupert)
Call changeTitle("PrinceRupert")
r.Select
ws.Sort.SortFields.Clear
ws.Sort.SortFields.Add Key:=r, _
SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
With ws.sort
.SetRange (WorksheetFunction.CountA(Range("A:A")))
'essentially, i want the range to be up to the last entry of the table
.Header = xlNo
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
End Sub
答案 0 :(得分:1)
对于第一个问题,您需要在设置r值之前设置ws; 第二个问题,WorksheetFunction.CountA(Range(“A:A”))的结果是一个数字,而不是Range。所以代码应该这样写:
Sub btnPrinceRupert()
Dim ws As Worksheet
Dim r As Range
Set ws = Worksheets("Master")
Set r = ws.Cells(2, 1)
Call filterMyTable(xPrinceRupert)
Call changeTitle("PrinceRupert")
r.Select
ws.Sort.SortFields.Clear
ws.Sort.SortFields.Add Key:=r, _
SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
With ws.sort
.SetRange (Range("1:" & WorksheetFunction.CountA(Range("A:A")))
'essentially, i want the range to be up to the last entry of the table
.Header = xlNo
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
End Sub