我正在尝试根据每个工作表中的单元格值对工作表进行排序。我遇到困难的主要区域是将排序应用于仅选择的纸张。以下是我到目前为止的情况。对VBA来说很新鲜
Sub SortWksByCell()
Dim i As Integer
Dim j As Integer
For i = 1 To ActiveWindow.SelectedSheets.Count
For j = i + 1 To ActiveWindow.SelectedSheets.Count
If UCase(Worksheets(i).Range("q1")) <= _
UCase(Worksheets(j).Range("q1")) Then
Worksheets(j).Move Before:=Worksheets(i)
End If
Next
Next
End Sub
答案 0 :(得分:1)
每当您Move
工作表时,您都会忽略工作表的选择,因此您必须在开始排序过程之前修复它
Option Explicit
Sub SortWksByCell()
Dim i As Integer
Dim j As Integer
Dim wss As Sheets
Set wss = ActiveWindow.SelectedSheets
For i = 1 To wss.Count
For j = i + 1 To wss.Count
If UCase(wss(i).Range("q1")) <= _
UCase(wss(j).Range("q1")) Then
wss(j).Move Before:=wss(i)
End If
Next
Next
End Sub