excel vba 将数据用逗号分割,并插入到不同行

如何将excel中数字分离到不同行
24.5,23.3,24
25,36,56,33,66
23,33
将以上不同行的数据用逗号分割开,然后插入到不同行
举例:
A列 B列
1 23.5(2)(1),34.33(a)
2 44,45,46,43,34
3 77
......
分割后变为:
A列 B列
1 23.5(2)(1)
1 34.33(a)
2 44
2 45
2 46
2 43
2 34
3 77
.....
实际中的表格数据有一万多条,所以要通用

第1个回答  2011-08-30
代码如下:

Sub aa()
Dim i, j, k As Integer
Dim s As String
For j = 1 To 3
s = Cells(j, 1) & ","
For i = 1 To Len(s) - Len(Replace(s, ",", ""))
k = InStr(1, s, ",")
Cells(j, i + 1) = 1 * Left(s, k - 1)
s = Mid(s, k + 1, Len(s) - k)
Next i
Next j
End Sub

A1:A3分别输入上面的三行数字后,运行即可达到要求。追问

这个分割后的数据是在一行啊,我需要的是在同列不同行。您有办法吗?

追答

修改后代码如下:

Sub aa()
Dim i, j, k, m, n As Integer
Dim s As String
For i = 1 To Sheet1.Cells(Rows.Count, 1).End(xlUp).Row
m = Len(Sheet1.Cells(i, 1)) - Len(Replace(Sheet1.Cells(i, 1), ",", "")) + 1
n = WorksheetFunction.CountA(Sheet2.Range("A:A"))
s = Sheet1.Cells(i, 1) & ","
For j = n + 1 To n + m
k = InStr(1, s, ",")
Sheet2.Cells(j, 1) = i
Sheet2.Cells(j, 2) = Replace(Left(s, k), ",", "")
s = Mid(s, k + 1, Len(s) - k)
Next j
Next i
End Sub

Sheet1A列中输入原始数据,运行程序后结果输出在Sheet2中。

第2个回答  2011-08-30
选中单元格 》 数据 》分列 》分隔符号 》下一步 》逗号 》完成追问

这个我知道的,这样完成后数据是在一行中显示

追答

无需那么复杂的代码,简单代码就可以解决了,假设原始数据在sheet1表(a列为编号,b列为数据),提取到sheet2表:

Sub aa()
Dim i, c, j, n, arr
i = 1
n = Sheets("sheet1").Range("b65536").End(xlUp).Row '假设原始数据放在sheet1工作表
MsgBox "原始数据共 " & n & " 行"
For Each c In Sheets("sheet1").Range("b1:b" & n)
If c = "" Then GoTo line1
a = Cells(c.Row, 1)
arr = Split(c, ",")
For j = 0 To UBound(arr)
Sheets("Sheet2").Cells(i + j, 1) = a '提取数据到sheet2工作表
Sheets("Sheet2").Cells(i + j, 2) = arr(j)
Next j
i = i + UBound(arr) + 1
line1:
Next c
End Sub

第3个回答  2011-08-30
Sub Chai_fen()
Dim Str1 As String
Dim Str2 As String

Dim i As Integer
Dim j As Integer
Dim k As Integer
Dim L As Integer
Dim t1 As Long
Dim t2 As Long
Dim t3 As Long
Dim t4 As Long
Dim f As Boolean
'''''''''''''''''''''''''''''''''''
Dim ALStr As String
Dim ARStr As String
Dim ALoop1 As Long
Dim ATmpStr As String
Dim ATmp1 As Long
Dim ATmp2 As Long
Dim Tx As Long

'''''''''''''''''''''''''''''''''''
On Error Resume Next

f = True
t1 = Selection.Row + UBound(Selection.Value2)
If t1 > 65535 Then
t1 = 65535
Else
t1 = Selection.Row
Tx = UBound(Selection.Value2)

GoTo S1
End If

t2 = Selection.Row
t3 = Selection.Column

If Selection(1, UBound(Selection.Value2)) = "" Then

Range(Cells(t1, t3), Cells(t1, t3)).Select
Selection.End(xlUp).Select
t4 = Selection.Row
Range(Cells(t2, t3), Cells(t4, t3)).Select

t1 = Selection.Row + UBound(Selection.Value2)
If t1 = 65535 Then t1 = 1
Tx = UBound(Selection.Value2)
End If

Start1:

S1:

Str1 = Selection(1, 1)
''''''''''''''''''''''''''''''''''''''''''''''

While InStr(1, Str1, "-")

ATmpStr = ""
ATmp1 = InStr(1, Str1, "-")
ALoop1 = ATmp1
ATmp2 = ATmp1

AL1:

ATmpStr = Mid(Str1, ATmp1 - 1, 1)
ATmp1 = ATmp1 - 1

If ATmp1 <> 0 And ATmpStr <> "," Then GoTo AL1
ALStr = Mid(Str1, ATmp1 + 1, ALoop1 - ATmp1 - 1)

AL2:

ATmpStr = Mid(Str1, ATmp2 + 1, 1)
ATmp2 = ATmp2 + 1

If ATmp2 <> Len(Str1) And ATmpStr <> "," Then GoTo AL2
If ATmpStr = "," Then ATmp2 = ATmp2 - 1
ARStr = Mid(Str1, ALoop1 + 1, ATmp2 - ALoop1)

Str1 = Left(Str1, ATmp1) + Chai(ALStr, ARStr) + Right(Str1, Len(Str1) - ATmp2)

Wend

'''''''''''''''''''''''''''''''''''''''''''''''''''
j = 0
i = 1
While i <> 0
i = InStr(i + 1, Str1, ",", vbTextCompare)
j = j + 1

Wend

k = j
k = k - 1
t1 = t1 + k
i = Selection.Row
j = Selection.Column

If k > 0 Then Rows(i + 1 & ":" & i + k).Insert Shift:=xlDown

k = 1
L = 0
Str1 = Replace(Str1, ",", Chr(13) & Chr(10))
Open "tmp.txt" For Output As #1
Print #1, Str1

Close 1

Open "tmp.txt" For Input As #2
While EOF(2) = False
Line Input #2, Str1
Cells(i, j) = Str1
i = i + 1
Wend
Close 2

End2:

Cells(i, j).Select

If i < t1 + Tx Then GoTo Start1

Cells(t1, j).Select

End Sub

Private Function Chai(LStr As String, RStr As String) As String
Dim Origin As String

Dim Loop1 As Integer
Dim Tmp As Long

Dim TmpStr As String
Dim LTmpStr As String
Dim AddZero As String

Dim r As Long
Dim t As Long

Origin = LStr
r = 1
L: If Cells(r, 1) <> "" Or Cells(r + 1, 1) <> "" Then r = r + 1: GoTo L
L2:
Cells(r, 1) = LStr
Range("A" & CStr(r)).AutoFill Destination:=Range("A" & CStr(r) & ":" & "A" & CStr(r + 1)), Type:=xlFillDefault
LStr = Cells(r + 1, 1)
Origin = Origin + "," + LStr

t = t + 1
If t = 100 Then
Cells(r, 1) = ""
Cells((r + 1), 1) = ""
MsgBox "拆分" & RStr & "错误,不能处理在-之间达到100组的数据"
End
End If

If LStr <> RStr Then GoTo L2
Cells(r, 1) = ""
Cells((r + 1), 1) = ""
Chai = Origin

End Function

操作方法:
先选取要操作的列(或列区域),再运行.追问

好棒啊!!!
我希望其它列数据不变,复制过去怎么弄。
例如:
列A 列B
1 23,34,35
2 33

拆分后列A的数据:
列A 列B
1 23
1 34
1 35
2 33
...
thank you !

追答

以上代碼是通用型,除逗號分開外,還可以分開帶"-"的數據,比如:1.5-1.9可以分開為:1.5,1.6,1.7,1.8,1.9
建議你用Excel公式.
1. 在序號與數據之間插入一例.
2. 使用公式:=if(左邊單元格="",上面單元格,左邊單元格) ----如果沒有"上面單元格"(可能在B1),請將第一個數據Copy過來,從第二個開始.
3. 公式往下拉就OK了

追问

嗯,这个方法可以实现我最初的想法了~谢谢~

我的表格目前有20多列,这样的话,如果每列记录都插入新的一列,这样就有40多列了。
还能有简单的方法吗?

本回答被提问者采纳
第4个回答  2011-08-30
这种事情也要找VBA???追问

您有简单的方法?

excel vba 将数据用逗号分割,并插入到不同行
A1:A3分别输入上面的三行数字后,运行即可达到要求。

VBA excel 提取K列单元格逗号之间的数据,并把这些数据复制到F列的表...
1、如果有数据来源,你可以通过函数来实现。例如:=IF(A2="","",DATE(zdy(F2,0),zdy(F2,1),zdy(F2,2)))然后在【设置单元格格式】数字中自定义格式为:yyyy-mm-dd 2、如果没有数据来源,建议你用“数据有效性”来设置。打开【数据】菜单下的【有效性】对话框 1)有效性条件:允许:日期 ...

excel中如何将单元格值用逗号拆分?
=拆分(A1)输入公式下拉

想在excel里用VBA,点击按钮复制一行数据到另外一行,可是不知道该怎么写...
Private Sub ComboBox1_Change()Sheet2.Cells(1, 1).Value = ComboBox1.ValueDim i As Integeri = 2Do While Not i > 100000If Sheet3.Cells(i, 1) = "" ThenSheet3.Cells(i, 1).Value = Sheet2.Cells(1, 1).ValueSheet3.Cells(i, 2).Value = Now()GoTo lastlineElsei = i...

excel VBA 将1列数据,放到一个单元格中,数据之间用逗号隔开
AR = Range([A1], [A3])[A5] = Join(Application.Transpose(AR), ",")

Excel中如何将一个单元格中几行分内容拆分到不同的行,并保持同行其他单 ...
亲,这个用公式也许可以,不确定。但是VBA一定可以解决。演示效果和代码如下。打开你的Excel文件,按“Alt+F11”打开VBA编辑窗口,然后在左侧对应的Sheet上双击,右侧空白处粘贴下面的代码。关闭VBA窗口。然后按“Alt+F8”打开宏窗口,选择刚插入的宏,点击“执行”。Sub CF()Dim r, i As LongDim SAr...

在Excel里用vba或函数统计每一行的数据种类,并把数据分好逗号放到指定的...
VBA中复制以下代码,然后在excel中插入公式,选择自定义,选择tj函数,然后选择范围 Function tj(c As Range) Dim x, y y = "" For Each a In c.Cells x = a.Value If x = "" Then x = """ """ If y = "" Then tj = x y = tj Else If y <>...

求一段VBA语言可以将一个单元格的内容分成多行记录
Sub splitting()startRow = 3 '已知你的表里面待拆分数据是从第三行开始的。endrow = ActiveSheet.Range("A65535").End(xlUp).Row 'A列从第65535行开始倒数,直到不为空(这样找出A列最下一行有数据的) i = startRow rowx = 1Do While i <= endrow 'A列从第三行开始用循环遍历 nam...

EXCEL,怎么把用vba获取到的多个数据隔开?
WPS UE 操作方法 01 这些数据原先是按照一定格式分开的,比如说逗号、"|"、“\/”等等,只要是统一隔开的,如图所示是逗号隔开的一系列号码,单纯放到excel或者WPS中根本无法分列开来;02 我们把这些数据先拷贝到excel中的第一行第一列上,如图所示:03 然后,我们全部选中拷贝进来的数据,找到【数据】...

excel表格vba查找逗号分隔数据列中的值
下面是我根据你的题目写的,split里的逗号是半角的,你可以按照你的需求换成全角的 Sub test() D最简单的改法,将表1中的A6:A9都输入 王珊 将其购买4种物品,分别放入B6:B9单元格区域 如果要改表3,会反复用到VLOOKUP函数,比较繁琐。任选一个空的单元格,输入=TEXTJOIN(",",1,A2:A4)就行了...

相似回答