在excel表中,我有以下数据:
Name Age
------- -------
XYZ 24
ABC 25
CDE 26
GHI 27
我希望在Excel工作表中将此数据转换为以下内容。
Name1 Age1 Name2 Age2 Name3 Age3 Name4 Age4
XYZ 24 ABC 25 CDE 26 GHI 27
这可能吗?请帮忙。谢谢!
答案 0 :(得分:4)
对于非VBA解决方案 - 您可以通过自动填充和排序的一些创造性使用来实现此目的:
进行一些创意自动填充和排序:
复制并粘贴转置:
你做完了:
答案 1 :(得分:0)
将其粘贴到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