Sub test1()
Dim h
Dim j As Integer
j = 0 '用於輔助循環的進行,可以在拆分行獲取下一個需要拆分單元格的行號
'Application.ScreenUpdating = False
'For i = 1 To Range("a65536").End(xlUp).Row
For i = 1 To 50 '循環結束值至少等於拆分后的行數,否則會沒有分完就跳出循環,導致最后部分無法拆分
'MsgBox i
i = i + j
h = Split(Cells(i, 1), ",") '把i行1列的單元格內容把逗號前后內容以數組的形式存儲到h
'MsgBox i
'MsgBox UBound(h)
If UBound(h) > 0 Then
Rows(i + 1).Resize(UBound(h)).Insert '在i+1行上插入UBound(h)行空行,UBound(h)獲取數組h的最大下標,數組默認下標從0開始,所以需要if判斷,當數組存在兩個值或以上才執行空行插入
Cells(i, 2).Resize(UBound(h) + 1, 1) = Application.Transpose(h) 'Transpose(h)轉置函數,把數組h按列填充,resize(n,m)函數把活動單元格變成n行m列
j = UBound(h)
'MsgBox UBound(h)
For num = 1 To j
Cells(i + num, 1) = Cells(i, 1) '通過循環輸入1列也就是被拆分行出現的空格,可達到分行后進行結果的追溯,是從哪個值拆出來額
Next
Else
Cells(i, 2) = Application.Transpose(h) ' cells函數第一個參數決定賦值到第幾行,第二個參數決定賦值到第幾列
j = 0
End If
'If i < 2 Then
'j = UBound(h)
'MsgBox "right"
'Else
'j = UBound(h)
'End If
Next
'Application.ScreenUpdating = True
End Sub