|
[广告] VBA代码宝 - VBA编程加强工具 · VBA代码随查随用 · 内置多项VBA编程加强工具 ★ 免费下载 ★ ★ 使用手册★
Sub ykcbf_Array() '//2024.12.9
Dim ws As Worksheet
Set ws = Sheets("分列")
Application.ScreenUpdating = False
Dim r As Long
Dim arr As Variant
Dim st() As String
Dim i As Long, j As Long
Dim maxCols As Long
Dim outputArr() As Variant
' 获取最后一行
r = ws.Cells(ws.Rows.Count, 1).End(xlUp).Row
' 将数据读入数组
arr = ws.Range(ws.Cells(1, 1), ws.Cells(r, 2)).Value
' 初始化输出数组的第一维大小
ReDim outputArr(1 To r, 1 To 1)
' 遍历数组并拆分
For i = 1 To UBound(arr, 1)
st = Split(arr(i, 2), ",") ' 按逗号拆分
If UBound(st) + 1 > UBound(outputArr, 2) Then
' 如果当前拆分结果的列数超过outputArr的第二维大小,则调整大小
ReDim Preserve outputArr(1 To r, 1 To UBound(st) + 1)
End If
For j = LBound(st) To UBound(st)
outputArr(i, j + 1) = st(j)
Next j
Next i
' 确定最大列数
maxCols = 1
For i = 1 To UBound(outputArr, 1)
If UBound(outputArr, 2) > maxCols Then
maxCols = UBound(outputArr, 2)
End If
Next i
' 将拆分后的数据一次性写入工作表
ws.Range(ws.Cells(1, 3), ws.Cells(r, maxCols)).Value = outputArr
Application.ScreenUpdating = True
MsgBox "OK!"
End Sub
能跑的代码都是好代码,我参与修改下 |
评分
-
1
查看全部评分
-
|