gpt4 book ai didi

excel - 如何将多个 Excel 列的数据合并到一列中

转载 作者:行者123 更新时间:2023-12-01 17:53:48 24 4
gpt4 key购买 nike

假设我有一个 Excel 工作表,其中有 4 列数据,每列有 20,000 行数据。

最有效的获取方法是什么,以便我将所有数据合并到一列中(即 A 列中的 80,000 行数据,而不是分布在 4 列中的 20,000 行数据)。

此外,如何实现该解决方案。我的意思是,如果你的解决方案不是“公式”而是 VBA,我该如何实现该解决方案?

谢谢!

最佳答案

保存您的工作簿。如果此代码没有达到您想要的效果,返回的唯一方法是关闭而不保存并重新打开。

选择要在一列中列出的数据。必须是连续的列。可能包含空白单元格。

按 Alt+F11 打开 VBE

按 Control+R 查看项目资源管理器

导航到工作簿的项目并选择“插入 - 模块”

将此代码粘贴到代码 Pane 中

Sub MakeOneColumn()

Dim vaCells As Variant
Dim vOutput() As Variant
Dim i As Long, j As Long
Dim lRow As Long

If TypeName(Selection) = "Range" Then
If Selection.Count > 1 Then
If Selection.Count <= Selection.Parent.Rows.Count Then
vaCells = Selection.Value

ReDim vOutput(1 To UBound(vaCells, 1) * UBound(vaCells, 2), 1 To 1)

For j = LBound(vaCells, 2) To UBound(vaCells, 2)
For i = LBound(vaCells, 1) To UBound(vaCells, 1)
If Len(vaCells(i, j)) > 0 Then
lRow = lRow + 1
vOutput(lRow, 1) = vaCells(i, j)
End If
Next i
Next j

Selection.ClearContents
Selection.Cells(1).Resize(lRow).Value = vOutput
End If
End If
End If

End Sub

按F5运行代码

关于excel - 如何将多个 Excel 列的数据合并到一列中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4480227/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com