将此粘贴到您的 vba 编辑器(标准模块)中并将代码添加到按钮。
代码假定表格从范围“A1”开始。
第一行包含标题,其他行包含示例中的值。
我粘贴了第 8 行的输出。
如果有帮助,请告诉我。
Option Explicit
Sub Name_Age()
Dim oRange As Excel.Range
Dim iRange_Cols As Integer
Dim lRange_Rows As Long
Dim lCnt As Long
Dim vArray As Variant
Dim vArray_Dest As Variant
Dim lUbound_Rows As Long
Dim lUBound_Cols As Long
Set oRange = ThisWorkbook.Sheets(1).UsedRange
iRange_Cols = oRange.Columns.Count
lRange_Rows = oRange.Rows.Count - 1
ReDim vArray(1 To lRange_Rows, 0 To iRange_Cols)
ReDim vArray_Dest(1 To iRange_Cols, 1 To (lRange_Rows * iRange_Cols))
vArray = oRange
lCnt = 0
For lCnt = 1 To lRange_Rows
vArray_Dest(1, (lCnt * 2) - 1) = CStr("Name" & lCnt)
vArray_Dest(1, (lCnt * 2)) = CStr("Age" & lCnt)
vArray_Dest(2, (lCnt * 2) - 1) = vArray(1 + lCnt, 1)
vArray_Dest(2, (lCnt * 2)) = vArray(1 + lCnt, 2)
Next lCnt
lUbound_Rows = UBound(vArray_Dest, 1)
lUBound_Cols = UBound(vArray_Dest, 2)
Set oRange = Nothing
Set oRange = ThisWorkbook.Sheets(1).Range(Cells(8, 1), Cells(8 + lUbound_Rows - 1, lUBound_Cols))
oRange = vArray_Dest
End Sub