问:如何使用 VBA 转置每第 n 行
Q. How to transpose every nth rows using VBA
有人可以建议我如何使用 VBA 将每第 n 行转置为多列。
我有大约 10000 行的类似数据(每个转置行都需要在“##”之后执行)
数据集
|-|
|-|
|##|
|文本1|
|文本2|
|文本3|
|文本4|
|##|
|文本5|
|文字6
|文字7
| ##
|文字8
|文本9
|文本10
|文本11
| ##
|文字12
|文本13
| ...
结果(使用“|”分隔 excel [A | B | C | D...] 中的每一列)
-
-
-
-
-
##
text1
text2
text3
text4
##
text5
text6
text7
##
text8
text9
text10
text11
##
text12
text13
...
试试这个代码
Sub Test()
Dim a, i As Long, m As Long, k As Long, mx As Long
a = Range("A1:A" & Cells(Rows.Count, 1).End(xlUp).Row).Value
ReDim b(1 To UBound(a, 1), 1 To 100)
i = 1
Do Until a(i, 1) = "##" And i >= UBound(a, 1)
If a(i, 1) = "##" Then m = m + 1: k = 1
b(m, k) = a(i, 1)
If mx < k Then mx = k
k = k + 1
If i = UBound(a, 1) Then Exit Do
i = i + 1
Loop
Range("D1").Resize(m, mx).Value = b
End Sub
有人可以建议我如何使用 VBA 将每第 n 行转置为多列。 我有大约 10000 行的类似数据(每个转置行都需要在“##”之后执行)
数据集 |-| |-| |##| |文本1| |文本2| |文本3| |文本4| |##| |文本5| |文字6 |文字7 | ## |文字8 |文本9 |文本10 |文本11 | ## |文字12 |文本13 | ...
结果(使用“|”分隔 excel [A | B | C | D...] 中的每一列)
- | - | - | - | - |
---|---|---|---|---|
## | text1 | text2 | text3 | text4 |
## | text5 | text6 | text7 | |
## | text8 | text9 | text10 | text11 |
## | text12 | text13 | ... |
试试这个代码
Sub Test()
Dim a, i As Long, m As Long, k As Long, mx As Long
a = Range("A1:A" & Cells(Rows.Count, 1).End(xlUp).Row).Value
ReDim b(1 To UBound(a, 1), 1 To 100)
i = 1
Do Until a(i, 1) = "##" And i >= UBound(a, 1)
If a(i, 1) = "##" Then m = m + 1: k = 1
b(m, k) = a(i, 1)
If mx < k Then mx = k
k = k + 1
If i = UBound(a, 1) Then Exit Do
i = i + 1
Loop
Range("D1").Resize(m, mx).Value = b
End Sub