我有如下X代码中所示的excel数据。 X代码列包含不同行的数据。我需要实现的是X代码(结果)中所示。
X代码
2222, 3333, 4444, 5555
3458, 4532, 5463, 8976, 4538, 3244, 4538
2222, 4532, 3243, 3243 , 3243, EE44
WW21, EE33, 4532, 5690, 4573, 6758
X代码(结果)
2222
3333
4444
5555
3458
4532
5463
8976
4538
3244
4538
2222
4532
3243
3243
3243
EE44
WW21
4532
5690
6758
答案 0 :(得分:2)
这是一个使用数组的无循环解决方案;用逗号连接单元格,然后将它们拆分:
Sub joinThenSplit()
Dim ar: ar = Sheet3.Range("A2", Sheet3.Range("A999999").End(xlUp)).Value2
ar = Split(Join(Application.Transpose(ar), ","), ",")
Sheet3.Range("C2").Resize(UBound(ar) + 1).Value = Application.Transpose(ar)
End Sub
答案 1 :(得分:0)
要进一步详细说明Scott的建议,请使用Split命令。例如
Sub Example()
Dim SomeValue As String
Dim SplitValue As Variant
Dim ToSheet As Variant
SomeValue = ThisWorkbook.Sheets("Sheet3").Range("A2").Value
SplitValue = Split(SomeValue, ", ")
' SplitValue should now look something like:
' SplitValue(0) = "2222"
' SplitValue(1) = "3333"
' SplitValue(2) = "4444"
' SplitValue(3) = '5555"
' Now you can easily put these values into the next available spot on your sheet
' Here I use Range("C1000000") because it is very close to the max row count
' There are other ways of doing this as well.
' What is happening in the code below is I am finding my next entry location, and then
' resizing this to include the number of additional rows I need (the max dimension of
' the splitvalue array + 1 since Split creates 0 based arrays)
' I use a second array that is 2D so that I can directly assign the values from the array to the sheet range
' instead of assigning each cell value individually
ReDim ToSheet(1 To UBound(SplitValue) + 1, 1 To 1)
Dim i As Long
For i = LBound(SplitValue) To UBound(SplitValue)
ToSheet(i + 1, 1) = SplitValue(i)
Next
ThisWorkbook.Sheets("Sheet3").Range("C1000000").End(xlUp).Resize(UBound(ToSheet)).Value = ToSheet
End Sub
这应该让你朝着正确的方向前进。这不会为你做所有这些,但看看你是否可以弄清楚如何使用它作为构建你需要的例程的框架。 Split是一个非常有用的功能,与循环一起使用可以为您提供所需的功能。
祝你好运。