如何使用 VBA 转置然后堆叠其他列?

问题描述 投票:0回答:0

第一次在 VBA 中编码,对如何在一个步骤中同时转置和堆叠列感到困惑。

我有一个 14 列宽的数据集,行数不确定。我需要按照 1 到 14 的图像示例中显示的顺序对它们进行排序

我已经尝试运行提供的代码HERE.

但是,我无法根据自己的需要重新配置它,或者如何将索引数据粘贴为新列

编辑:可以对整数进行排序的代码

Sub Depth_Width_Sort()

Dim rngSource As Range
Dim rng As Range
Dim MiMatriz() As Long
Dim MiPos As Long
Dim i As Integer

Set rngSource = Range("A1").CurrentRegion

ReDim MiMatriz(1 To 1, 1 To rngSource.Cells.Count)

For Each rng In rngSource.Cells
    MiPos = (((rng.Row - rngSource.Cells(1, 1).Row + 1) - 1) * rngSource.Columns.Count) + (rng.Column - rngSource.Cells(1, 1).Column + 1)
    MiMatriz(1, MiPos) = rng.Value
Next rng

For i = 1 To rngSource.Cells.Count Step 2
    Cells((i + 1) / 2, 16).Value = MiMatriz(1, i)
Next i

For i = 2 To rngSource.Cells.Count Step 2
    Cells(i / 2, 17).Value = MiMatriz(1, i)
Next i

Erase MiMatriz

Set rngSource = Nothing

这里是数据排序的例子 Sorted data

excel vba sorting
© www.soinside.com 2019 - 2024. All rights reserved.