|
楼主 |
发表于 2019-4-8 19:13
|
显示全部楼层
[广告] Excel易用宝 - 提升Excel的操作效率 · Excel / WPS表格插件 ★ 免费下载 ★ ★ 使用帮助★
本帖最后由 autumnalRain 于 2019-5-4 21:00 编辑
- Sub Transform2()
- Dim brr(1 To 1000, 1 To 4)
- brr(1, 1) = "姓名": brr(1, 2) = "款号": brr(1, 3) = "序号": brr(1, 4) = "产量"
- arr = Sheets("源数据").Range("a1").CurrentRegion
- For j = 2 To UBound(arr, 1) '行
- For i = 3 To UBound(arr, 2) '列
- n = n + 1
- brr(n + 1, 1) = arr(j, 1)
- brr(n + 1, 2) = arr(j, 2)
- brr(n + 1, 3) = arr(1, i)
- brr(n + 1, 4) = arr(j, i)
- Next i
- Next j
- Sheets("转置后").Cells.ClearContents
- Sheets("转置后").Range("a1").Resize(UBound(brr), 4) = brr
- End Sub
复制代码
autumnalRain 发表于 2019-4-8 18:27
复杂一些的交叉数据格式转换
第一次逻辑不太清晰。不用排序,而且排序结果不一定符合要求。行列循环次序变下就可以实现。增加一列,见转换格式2- Sub Transform2()
- Dim brr(1 To 1000, 1 To 5)
- brr(1, 1) = "姓名": brr(1, 2) = "款式": brr(1, 3) = "款号": brr(1, 4) = "序号": brr(1, 5) = "产量"
- arr = Sheets("源数据").Range("a1").CurrentRegion
- For j = 2 To UBound(arr, 1) '行
- For i = 4 To UBound(arr, 2) '列
- n = n + 1
- brr(n + 1, 1) = arr(j, 1)
- brr(n + 1, 2) = arr(j, 2)
- brr(n + 1, 3) = arr(j, 3)
- brr(n + 1, 4) = arr(1, i)
- brr(n + 1, 5) = arr(j, i)
- Next i
- Next j
- Sheets("转置后").Cells.ClearContents
- Sheets("转置后").Range("a1").Resize(UBound(brr), 5) = brr
- End Sub
复制代码
|
|