gpt4 book ai didi

excel - 如何在Excel中将表格转换为3列

转载 作者:行者123 更新时间:2023-12-04 19:45:38 24 4
gpt4 key购买 nike

我正在使用 Excel 中的提取文件。它基本上是多列,每列都有几行数据。

 A   | B   | C    | D   | E   | F    |
1 | 2 | 3 | 1 | 2 | 3 |
4 | 5 | 5 | 4 | 5 | 5 |
我想将其展平为 3 列,如下所示:
 A   | B   | C    |
1 | 2 | 3 |
4 | 5 | 5 |
D | E | F |
1 | 2 | 3 |
4 | 5 | 5 |
我想使用 VBA 来做,但我对这种语言真的很陌生,这是我到目前为止所做的:
Sub test()
Dim Key, Dic As Object, cl As Range, Data As Range, i&, n&
Set Dic = CreateObject("Scripting.Dictionary")
Dic.CompareMode = vbTextCompare
i = Cells(Rows.Count, "A").End(xlUp).Row
n = 1
Set Data = Range("B2:B" & i & "," & "D2:D" & i & "," & "F2:F" & i & "," & "H2:H" & i)
Dic.Add "|ID", "Date|Thing"
For Each cl In Data
If Cells(cl.Row, "A") <> "" Then
Dic.Add n & "|" & Cells(cl.Row, "A"), cl.Text & "|" & cl.Offset(, 1).Text
n = n + 1
End If
Next cl
n = 1
For Each Key In Dic
Cells(n, "K") = Split(Key, "|")(1)
Cells(n, "L") = Split(Dic(Key), "|")(0)
Cells(n, "M") = Split(Dic(Key), "|")(1)
n = n + 1
Next Key
End Sub
它给了我这个结果:
 A   | A   | A    |
B | B | B |
C | C | C |
1 | 1 | 1 |
2 | 2 | 2 |
3 | 3 | 3 |
4 | 4 | 4 |
5 | 5 | 5 |
6 | 6 | 6 |
D | D | D |
E | E | E |
F | F | F |
1 | 1 | 1 |
2 | 2 | 2 |
3 | 3 | 3 |
4 | 4 | 4 |
5 | 5 | 5 |
6 | 6 | 6 |
请问你能帮帮我吗 ?

最佳答案

除非我遗漏了一些东西,否则你把事情复杂化了。
如果你有这个:
screenshot
...然后使用这个:

Range("D1:F3").Cut Range("A4")
...得到这个:
screenshot
以下是有关 Range.Cut method 的更多信息.
方便学习如何自动执行基本任务,请参阅 "Recording a Macro to Generate Code" . "Getting started with VBA in Office" 中也有很好的信息.

关于excel - 如何在Excel中将表格转换为3列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/71436125/

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