|
本帖最后由 liulang0808 于 2017-12-18 18:31 编辑
昨天整理了相关字典的帖子http://club.excelhome.net/thread-1385473-1-1.html
今天处理了一个递归的问题,于是心血来潮,搞了这个字典+递归进行凑数,供大家参考
- Public d
- Public a
- Public arr
- Public m
- Sub lqxs_zd()
- Application.ScreenUpdating = False
- ActiveSheet.UsedRange.Offset(0, 2).ClearContents
- Set d = CreateObject("scripting.dictionary")
- arr = [a1].CurrentRegion
- a = 3
- m = Application.InputBox("请输入数字:", "凑数", 10, , , , , 1)
- For j = 2 To UBound(arr)
- If arr(j, 1) < m Then
- d(j) = arr(j, 1)
- dg j
- If d.Count > 0 Then d.Remove j
- Else
- If arr(j, 1) = m Then
- Cells(1, a) = arr(j, 1)
- a = a + 1
- End If
- End If
-
- Next j
- Application.ScreenUpdating = True
- End Sub
- Sub dg(y)
- For j = y + 1 To UBound(arr)
- sm = WorksheetFunction.Sum(d.items)
- If sm + arr(j, 1) = m Then
- d(j) = arr(j, 1)
- Cells(1, a).Resize(d.Count) = WorksheetFunction.Transpose(d.items)
- a = a + 1
- d.Remove j
- Else
- If sm + arr(j, 1) < m Then
- d(j) = arr(j, 1)
- dg j
- End If
- End If
- If d.exists(j) Then d.Remove j
- Next j
- End Sub
复制代码
|
评分
-
1
查看全部评分
-
|