我有一个 Excel 文件,其中包含带有数字的标题,如下所示,我希望它们从左到右按升序重新排序,包括带有 Excel 宏的列数据。有人可以帮忙吗 在此输入图片描述
我有下面的代码,但它不符合目的,我需要从左到右按升序重新排列列
Sub Sort_Cols()
Dim rngAddress As Range
Dim myCols
Dim i As Long
myCols = Split("Obj 2|Obj 4|Obj 1|Obj 3", "|") '<- In the order you want them in columns A:D
With Sheets("Sheet1")
For i = 0 To UBound(myCols)
Set rngAddress = Nothing
Set rngAddress = .Range("A1:Z1").Find(myCols(i))
If rngAddress Is Nothing Then
MsgBox myCols(i) & " column was not found."
Exit Sub
Else
.Range(rngAddress, rngAddress.End(xlDown)).Cut
On Error Resume Next
.Columns(i + 1).Insert
On Error GoTo 0
End If
Next i
End With
End Sub
Sub SortRows()
With Range("A2:F5") ' modify as needed
.Sort key1:=.Rows(1), order1:=xlAscending, Header:=xlNo, Orientation:=xlSortRows
End With
End Sub