|
[广告] VBA代码宝 - VBA编程加强工具 · VBA代码随查随用 · 内置多项VBA编程加强工具 ★ 免费下载 ★ ★ 使用手册★
- Sub MergeDemo()
- Dim dicMth As Object, dicYr As Object, rngData As Range
- Dim i As Long, iYr, iMth, vKey, aTmp(1), aTmp2
- Dim arrData
- Const START_COL = 3
- Sheet2.Copy , Sheets(Sheets.Count)
- Set dicMth = CreateObject("scripting.dictionary")
- Set dicYr = CreateObject("scripting.dictionary")
- Set rngData = Range("A3").CurrentRegion
- arrData = rngData.Value
- For i = START_COL To UBound(arrData, 2)
- iYr = CStr(arrData(1, i))
- iMth = arrData(1, i) & "|" & arrData(2, i)
- UpdateDic dicYr, iYr, 3, i
- UpdateDic dicMth, iMth, 4, i
- Next i
- Application.DisplayAlerts = False
- If dicYr.Count > 0 Then
- For Each vKey In dicYr.Keys
- dicYr(vKey)(0).Resize(1, dicYr(vKey)(1)).Merge
- Next
- End If
- If dicMth.Count > 0 Then
- For Each vKey In dicMth.Keys
- dicMth(vKey)(0).Resize(1, dicMth(vKey)(1)).Merge
- Next
- End If
- Application.DisplayAlerts = True
-
- End Sub
- Sub UpdateDic(ByRef oDic As Object, vKey, iRow, iCol)
- Dim aTmp(1), aTmp2
- If oDic.exists(vKey) Then
- aTmp2 = oDic(vKey)
- aTmp2(1) = aTmp2(1) + 1
- oDic(vKey) = aTmp2
- Else
- Set aTmp(0) = Cells(iRow, iCol)
- aTmp(1) = 1
- oDic(vKey) = aTmp
- End If
- End Sub
复制代码 |
|