VBA实现相同行合并

帮人捣鼓了个VBA代码用来实现多行合并,具体需求为:列2/列3/列4 相同的情况下,则对应的行合并为一行,且列1用空格隔开,列5则相加;

(对大多数办公室职员,VBA还算是提高效率的一个利器吧)

最终效果如下:

VBA实现相同行合并

 1 Sub test()
2 Dim r%, i%
3 Dim arr, brr
4 Dim d As Object
5 Set d = CreateObject("scripting.dictionary")
6 With Worksheets("sheet1")
7 r = .Range("a1").End(xlDown).Row
8 arr = .Range("a2:e" & r)
9 For i = 1 To UBound(arr)
10 If Not d.exists(arr(i, 2) & arr(i, 3) & arr(i, 4)) Then
11 ReDim brr(1 To UBound(arr, 2))
12 For j = 1 To UBound(arr, 2)
13 brr(j) = arr(i, j)
14 Next
15 Else
16 brr = d(arr(i, 2) & arr(i, 3) & arr(i, 4))
17 brr(1) = brr(1) & " " & arr(i, 1)
18 brr(5) = brr(5) + arr(i, 5)
19
20 End If
21 d(arr(i, 2) & arr(i, 3) & arr(i, 4)) = brr
22 Next
23 .Range("h2").Resize(d.Count, UBound(brr)) = Application.Transpose(Application.Transpose(d.items))
24 End With
25 End Sub

代码以字典作为实现功能,把列2、列3、列4合并起来作为Key值,列1、列5为Value值,列1则用空格合并,列5则相加。

上一篇:centos7下安装docker(5镜像命名)


下一篇:spring mvc --自己定义converse