|
楼主 |
发表于 2024-7-16 10:27
|
显示全部楼层
[广告] VBA代码宝 - VBA编程加强工具 · VBA代码随查随用 · 内置多项VBA编程加强工具 ★ 免费下载 ★ ★ 使用手册★
感谢,完美解决,附上中文解释
- Sub 拆分abc123释义()
- '定义数组arr和brr
- Dim arr, brr
- '创建一个Scripting.Dictionary对象
- Set d = CreateObject("scripting.dictionary")
- '关闭显示警告和屏幕更新
- Application.DisplayAlerts = False
- Application.ScreenUpdating = False
- '在"源数据"工作表中进行操作
- With Worksheets("源数据")
- '获取C列最后一个有数据的行号
- r = .[c65536].End(xlUp).Row
- '将A1:M列的数据赋值给数组arr
- arr = .Range("a1:m" & r)
- '定义一个包含特定表头名称的数组
- Rng = Array("楼栋号", "建筑面积", "专业名称", "工程造价(元)", "建筑指标(元/㎡)")
- '从第3行开始循环到数组arr的上界
- For i = 3 To UBound(arr)
- If arr(i, 2) <> "" Then
- '如果第3列的单元格值包含"栋"字
- If InStr(arr(i, 2), ".") = 0 Then
- '将当前单元格值赋值给变量mx
- mx = arr(i, 3)
- Else
- '如果字典d中不存在mx这个键,新建字典,key为mx
- If Not d.exists(mx) Then
- '初始化计数器m为1,当key为新key时,并重新定义brr数组
- m = 1
- ReDim brr(1 To 5, 1 To m)
- Else
- '获取字典中mx对应的值(即brr数组)
- brr = d(mx)
- '更新计数器m
- m = UBound(brr, 2) + 1
- '重新调整brr数组大小
- ReDim Preserve brr(1 To 5, 1 To m)
- End If
- '为brr数组赋值
- brr(1, m) = mx
- brr(2, m) = arr(i, 12)
- brr(3, m) = arr(i, 3)
- brr(4, m) = arr(i, 4)
- brr(5, m) = arr(i, 13)
- '将brr数组存入字典d,键为mx
- d(mx) = brr
- End If
- End If
- Next
- End With
- '定义新的数组zrr
- Dim zrr(1 To 10000, 1 To 5)
- '遍历字典d的键
- For Each aa In d.keys
- '获取字典中对应键的值(即数组arr)
- arr = d(aa)
- '循环处理数组arr
- For j = 1 To UBound(arr, 2)
- '计数器n递增
- n = n + 1
- '将arr的值复制到zrr中,转置
- For i = 1 To UBound(arr)
- zrr(n, i) = arr(i, j)
- Next
- Next
- Next
- '在"目标表"工作表中进行操作
- With Worksheets("目标表")
- '在A1单元格写入表头
- .Range("a1").Resize(1, 5) = Rng
- '将zrr数组的值写入到指定区域
- .Range("a2").Resize(UBound(zrr), UBound(zrr, 2)) = zrr
- End With
- '恢复显示警告和屏幕更新
- Application.DisplayAlerts = True
- Application.ScreenUpdating = True
- '弹出消息框显示"ok!"
- MsgBox "ok!", 64
- End Sub
复制代码 |
|