Sub ReverseColumnsAB_Array() Dim ws As Worksheet Dim lastRow As Long Dim arrA() As Variant, arrB() As Variant Dim i As Long, j As Long Set ws = ActiveSheet lastRow =
网页链接 (ws.Rows.Count, 1).End(xlUp).Row ' 获取A列最后一行 ' 边界检查 If lastRow < 2 Then MsgBox "数据行数不足2行,无需倒置", vbInformation Exit Sub End If ' 读取数据到数组 arrA = ws.Range("A1:A" & lastRow).Value arrB = ws.Range("B1:B" & lastRow).Value ' 创建临时数组存储倒置结果 ReDim tempA(1 To lastRow, 1 To 1) ReDim tempB(1 To lastRow, 1 To 1) For i = 1 To lastRow j = lastRow - i + 1 tempA(i, 1) = arrA(j, 1) tempB(i, 1) = arrB(j, 1) Next i ' 写回工作表 ws.Range("A1:A" & lastRow).Value = tempA ws.Range("B1:B" & lastRow).Value = tempB MsgBox "A、B列数据已成功倒置!共处理 " & lastRow & " 行", vbInformation End Sub