簡體   English   中英

對相同范圍的多張紙進行排序

[英]Sorting Multiple Sheets With the Same Range

嘗試對多張紙進行排序,使用了一個數組,但不幸出現“無法設置數組”錯誤。 不知道這里做錯了什么。 這是我的代碼:

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

您所指的是數組,而您應指的是圖紙。 您將ws聲明為Variant,並且此變量沒有諸如.Sort屬性

請注意,該數組可用作工作表名稱的快捷方式,而不能用作.Sheets("")對象的快捷方式。

Sub Macro1()

Dim ws() As Variant
Dim wb As Workbook
Set wb = ThisWorkbook
ws() = Array("Sheet1", "Sheet2", "Sheet3")
Dim i as Integer

For i = 0 To 2
    wb.Sheets(ws(i)).Sort.SortFields.Clear
    wb.Sheets(ws(i)).Sort.SortFields.Add Key:=Range("A1"), _
    SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
With wb.Sheets(ws(i)).Sort
    .SetRange Range("A1:B6")
    .Header = xlNo
    .MatchCase = False
    .Orientation = xlTopToBottom
    .SortMethod = xlPinYin
    .Apply
End With
Next i

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM