如何重新排列列标题名称的Excel列

我怎样才能重新排列的列标题名称的列。 我需要每周处理一个报告,原始工作表有23列。我将其中的一部分作为例子。

原始列顺序: QTY Payment Terms Contract No. etc
期望的列顺序: Contract No. Payment terms QTY etc

任何想法如何使用VBA代码自动化列重新排列? 提前谢谢了。

这对你如何工作? 我尝试了四列,它为我工作。

 Sub rearrange_Columns() Dim correctOrder() As Variant Dim lastCol As Long Dim headerRng As Range, cel As Range Dim mainWS As Worksheet Set mainWS = ActiveWorkbook.Worksheets("Sheet1") ' Edit this to be the correct order you need correctOrder() = Array("Column A", "Column B", "Column C", "Column D") ' Now, we know that the number of headers you have to rearrange are `UBound(CorrectOrder)+1` With mainWS lastCol = .Cells(1, .Columns.Count).End(xlToLeft).Column Set headerRng = .Range(.Cells(1, 1), .Cells(1, lastCol)) End With Dim newWS As Worksheet Set newWS = ActiveWorkbook.Sheets.Add newWS.Name = "Rearranged Sheet" Dim col As Long With newWS For col = 1 To lastCol For Each cel In headerRng If cel.Value = correctOrder(col - 1) Then mainWS.Columns(cel.Column).Copy .Columns(col) Exit For End If Next cel Next col End With End Sub 

注意:没有真正的error handling,所以如果你的头文件需要TRIM()编辑或检查错误的信息,你需要添加一些这样做。

在这里输入图像描述