尝试对多张纸进行排序,使用了一个数组,但不幸出现“无法设置数组”错误。不知道这里做错了什么。这是我的代码:
Sub Macro1()
Dim ws() As Variant
Dim wb As Workbook
Set wb = ThisWorkbook
Set ws() = wb.Sheets(Array("Sheet1", "Sheet2", "Sheet3"))
'
' Macro1 Macro
'
'
ws.Sort.SortFields.Clear
ws.Sort.SortFields.Add Key:=Range("A1"), _
SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
With ws.Sort
.SetRange Range("A1:B6")
.Header = xlNo
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
End Sub
最佳答案
记录的Sort语法比您实际需要的更多。
sub macro2()
dim w as long, lr as long, wss as variant
wss = Array("Sheet1", "Sheet2", "Sheet3")
for w = lbound(wss) to ubound(wss)
with thisworkbook.worksheets(wss(w))
lr = application.max(.cells(.rows.count, "a").end(xlup).row, _
.cells(.rows.count, "b").end(xlup).row)
with .range(.cells(1, "a"), .cells(lr, "b"))
.Cells.Sort Key1:=.Columns(1), Order1:=xlAscending, _
Orientation:=xlTopToBottom, Header:=xlNo
end with
end with
next w
end sub
关于arrays - 对相同范围的多张纸进行排序,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/50827638/