如何使用 VBA 在 Excel 中堆叠列?

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

目前我是第一次使用 VBA。

我收到了一项任务,即我需要使用 VBA 在 MS Excel 中堆叠以下列。

enter image description here

结果应该是这样的:

enter image description here

我在网上找到了以下VBA脚本(来源:https://www.extendoffice.com/documents/excel/4233-excel-stack-columns.html):

Sub ConvertRangeToColumn()
'UpdatebyExtendoffice
Dim Range1 As Range, Range2 As Range, Rng As Range
Dim rowIndex As Integer
xTitleId = "KutoolsforExcel"
Set Range1 = Application.Selection
Set Range1 = Application.InputBox("Source Ranges:", xTitleId, Range1.Address, Type:=8)
Set Range2 = Application.InputBox("Convert to (single cell):", xTitleId, Type:=8)
rowIndex = 0
Application.ScreenUpdating = False
For Each Rng In Range1.Rows
    Rng.Copy
    Range2.Offset(rowIndex, 0).PasteSpecial Paste:=xlPasteAll, Transpose:=True
    rowIndex = rowIndex + Rng.Columns.Count
Next
Application.CutCopyMode = False
Application.ScreenUpdating = True
End Sub

执行脚本结果如下 enter image description here

我没想到破折号会在“堆栈底部”。

如果有人能告诉我这是为什么以及我如何解决这个问题,我将不胜感激。

非常感谢!

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