|
代码稍加改动即可。
为增加可读性,多写了几行表达中间过程。
全部代码都加了详细注释。- Sub test()
- arr = [a1:c27] '原始数据范围直接赋值到数组arr
- n = UBound(arr) '获取最大行数n
-
- Set d = CreateObject("Scripting.Dictionary") '定义字典d
-
- For i = 0 To n / 3 - 1 '在原始数据中循环遍历
- j = i * 3 + 2 '获取待处理数据行位置j
- t = arr(j, 2) & "," & arr(j + 1, 2) '以该行以及下一行的两个年龄作为字典关键词t
-
- d(t) = d(t) & ";" & arr(j, 1) & "," & arr(j, 3) & ";" & arr(j + 1, 1) & "," & arr(j + 1, 3)
- '对每个关键词加入相对应的性别和身高属性
- Next
-
- p = d.keys '获取字典关键词keys结果数组p
- q = d.items '获取字典items结果数组q
-
- ReDim brr(1 To n, 1 To 3) '定义结果数组brr
- brr(1, 1) = "性别"
- brr(1, 2) = "年龄"
- brr(1, 3) = "身高"
-
- For i = 0 To d.Count - 1 '遍历字典
- k = k + 1 '序号+1,保证各年龄段隔开一行
- t = Split(q(i), ";") '每个年龄段的性别及身高属性拆分
- For j = 1 To UBound(t) '按拆分个数遍历
- k = k + 1 '序号+1,真正的段内序号递增
- brr(k, 1) = Split(t(j), ",")(0) '同一item中的性别和身高拆分,第1项为性别
- brr(k, 3) = Split(t(j), ",")(1) '同一item中的性别和身高拆分,第2项为身高
- brr(k, 2) = Split(p(i), ",")((k + i) Mod 2) '关键词即年龄的拆分
- Next
- Next
-
- [e:g] = "" 'E-G列清空
- [e1].Resize(n, 3) = brr '最终结果输出到E1开始的范围
-
- Sheet2.[a:c] = "" 'sheet2的A-C列清空
- Sheet2.[a1].Resize(n, 3) = brr '最终结果输出到sheet2的
-
- End Sub
复制代码 |
评分
-
1
查看全部评分
-
|