根据数字标题重新排列/重新排序 Excel 中的列

问题描述 投票:0回答:1

我有一个 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


excel vba sorting
1个回答
0
投票
  • Excel 内置排序功能非常适合您的情况
Sub SortRows()
    With Range("A2:F5") ' modify as needed
        .Sort key1:=.Rows(1), order1:=xlAscending, Header:=xlNo, Orientation:=xlSortRows
    End With
End Sub
© www.soinside.com 2019 - 2024. All rights reserved.