我几乎有一些代码,但需要一些帮助。现在,这段代码遍历第一行,然后识别任何换行符,并将它们全部放入A列的单独单元格中。我想要的代码要做的是将换行符分隔放在相应列的下面,而不是在A列中。例如,如果我有
A1: Red vbLf Blue vbLf Green
B1: 1 vbLf 2 vbLf 3 vbLf 4
我希望这个浮现为
A1: Red
A2: Blue
A3 Green
B1: 1
B2: 2
B3: 3
B4: 4
这是我当前拥有的代码。请记住,我的行是A-Z。
Sub Split_test()
Dim cell_value As Variant
Dim counter As Integer
'Row counter
counter = 1
'Looping through A column define max value
For i = 1 To 26
'Take cell one at the time
cell_value = ThisWorkbook.ActiveSheet.Cells(1, i).value
'Split cell contents
Dim WrdArray() As String
WrdArray() = Split(cell_value, vbLf)
'Place values to the A column
For Each Item In WrdArray
Sheets("Sheet4").Cells(counter, 1).value = Item
counter = counter + 1
Next Item
Next i
End Sub
答案 0 :(得分:2)
在第一个循环内移动counter = 1
,以便将其重置为每个循环1。
然后将Sheets("Sheet4").Cells(counter, 1)
更改为Sheets("Sheet4").Cells(counter, i)
,以便更改输出列。
Sub Split_test()
Dim cell_value As Variant
Dim counter As Integer
'Looping through A column define max value
For i = 1 To 26
'Row counter
counter = 1
'Take cell one at the time
cell_value = ThisWorkbook.ActiveSheet.Cells(1, i).Value
'Split cell contents
Dim WrdArray() As String
WrdArray() = Split(cell_value, vbLf)
'Place values to the A column
For Each Item In WrdArray
Sheets("Sheet4").Cells(counter, i).Value = Item
counter = counter + 1
Next Item
Next i
End Sub
答案 1 :(得分:2)
尝试拆分为一个变体,然后将转置的数组元素放回工作表中。
dim arr as variant, i as long
with worksheets(1)
for i=1 to 26
arr = split(.cells(1, i).value2, chr(10))
.cells(1, i).resize(ubound(arr) + 1, 1) = _
application.transpose(arr)
next i
end with