[英]VBA: Sorting worksheets with macro
As shown in this image如这张图片所示
My sort syntax is wrong and I don't understand why.我的排序语法错误,我不明白为什么。
I get我得到
Run-time error '1004': The sort reference is not valid.
运行时错误“1004”:排序引用无效。 Make sure that it's within the data you want to sort, and the first Sort By box isn't the same or blank.
确保它在您要排序的数据内,并且第一个排序依据框不相同或不为空白。
Sub Sort()
'
' Sort Macro
Dim rowNum As Variant
Dim columnNum As Variant
Dim sortField As Range
Dim keySort As Range
rowNum = Worksheets("Updated 1.0").Range("A1").End(xlDown).row
MsgBox (rowNum)
columnNum = Worksheets("Updated 1.0").Range("A1").End(xlToRight).column
MsgBox (columnNum)
With Worksheets("Updated 1.0")
Set sortField = Range(.Cells(2, 1), .Cells(rowNum, columnNum))
Set keySort = Range("A1")
sortField.Sort Key1:=keySort, Order1:=xlDescending, MatchCase:=False,
Orientation:=xlSortRows
End With
You were missing some .
你错过了一些
.
inside the With
but also the sort was was not correct I think.里面
With
但也排序是不正确的我认为。
This worked for me:这对我有用:
Sub Sort()
Dim sht As Worksheet
Dim sortField As Range
Dim keySort As Range
Set sht = Worksheets("Updated 1.0")
With sht
Set sortField = .Range("A1").CurrentRegion
Set keySort = .Range("A1")
sortField.Sort Key1:=keySort, Order1:=xlDescending, MatchCase:=False, _
Orientation:=xlSortRows
End With
End Sub
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.