有谁知道如何在 Excel 中编写 VBA 以将多行导出到一列。此外,我想在每个“行”的末尾添加“输入”。理想情况下,这将以 .txt 格式导出,但即使在同一文档中转换它也已经很棒了。
谢谢!
编辑:例如:我有包含一些文本/值 A1:A5、B1:B5 的行......我需要所有这些行才能在单个列 D 中“移动”。因此,A1 将转到 D1、A2-D2、A3-D3 ... B1 到 D7,依此类推。在每一端(A5、B5、C5、...)之后,我需要一个空单元格(当我将其转换为 .txt 时,它意味着输入)。我希望现在更清楚了。
下面的代码将执行您所描述的操作。使用开头的常量来设置应将多少列和行移动到一个单列,由TargetColNo指定。每个扫描的列后都会添加一个空单元格。
如果您更喜欢将其保存到文本文件中,您可以使用此代码并添加一个文本文件来添加结果而不是列。
Sub Rows2OneColumn()
Const StartColumnNo = 1 ' Start at column A
Const EndColNo = 3 ' End at column C
Const StartRowNo = 1 ' Start at row 1
Const EndRowNo = 5 ' End at row 5
Const TargetColNo = 5 ' Put result in column E
Dim source_row As Integer
Dim source_col As Integer
Dim target_row As Integer
target_row = 1
For source_col = StartColumnNo To EndColNo
For source_row = StartRowNo To EndRowNo
Cells(target_row, TargetColNo).Value = Cells(source_row, source_col).Value
target_row = target_row + 1
Next
target_row = target_row + 1 ' leave one cell empty
Next
End Sub
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句