excel如何将多行多列转置?下图表1转换为表2的形式,表1列数不固定,表2列数固定为25列,求大神赐教

2025-03-15 00:32:54
推荐回答(1个)
回答1:

Sub 测试()
'
Dim arr(), count
x = Selection.Rows.count
y = Selection.Columns.count

a = Selection.Value

count = 0
ReDim arr(1 To Selection.count)
For i = 1 To x '优先按行
For j = 1 To y
If a(i, j) <> "" Then
count = count + 1
arr(count) = a(i, j)
End If
Next j
Next i

Set tar = Application.InputBox(prompt:="请选择存放结果的单元格(存放不重复序列,按列)。", Title:="结果存放", Type:=8)

If tar Is Nothing Then
Exit Sub
End If

tar.Resize(1, count) = arr '按行写入

End Sub