Skip to content

Instantly share code, notes, and snippets.

@bleis-tift
Created July 18, 2011 06:37
Show Gist options
  • Save bleis-tift/1088691 to your computer and use it in GitHub Desktop.
Save bleis-tift/1088691 to your computer and use it in GitHub Desktop.
Option Explicit
Private xSource As Variant
Private xLbRow As Long
Private xUbRow As Long
Private xLbCol As Long
Private xUbCol As Long
Private xIndexes As Variant
Public Property Get Current() As Variant
Dim ret As Variant
ReDim ret(xLbCol To xUbCol)
Dim i As Long
For i = xLbCol To xUbCol: ret(i) = xSource(xIndexes(i), i): Next
Current = ret
End Property
Public Function Init(ByVal arr2D As Variant)
xSource = arr2D
xLbRow = LBound(xSource, 1)
xUbRow = UBound(xSource, 1)
xLbCol = LBound(xSource, 2)
xUbCol = UBound(xSource, 2)
Dim i As Long
ReDim xIndexes(xLbCol To xUbCol)
For i = xLbCol To xUbCol - 1: xIndexes(i) = xLbRow: Next
xIndexes(xUbCol) = xLbRow - 1
End Function
Public Function MoveNext() As Boolean
Dim i As Long
For i = xUbCol To xLbCol Step -1
If xIndexes(i) < xUbRow Then
xIndexes(i) = xIndexes(i) + 1
MoveNext = True
GoTo Escape
ElseIf xIndexes(i) = xUbRow Then
xIndexes(i) = xLbRow
End If
Next
MoveNext = False
Escape:
End Function
''' @seealso
''' http://d.hatena.ne.jp/bleis-tift/20110718/1310946373
''' http://supermab.com/wp/excel-dna-%E3%81%A7-xll-%E3%82%92%E3%81%A4%E3%81%8F%E3%82%8B%EF%BC%88%E3%81%9D%E3%81%AE17%EF%BC%89/
Option Explicit
Function AnsCount(rng As Range, trg As Long)
Dim arr2D As Variant: arr2D = rng
Dim goal As Long: goal = trg
Dim count As Long: count = 0
Dim iter As Iterator
Set iter = New Iterator
iter.Init arr2D
While iter.MoveNext()
Dim ans As Long: ans = 0
Dim n As Variant
For Each n In iter.Current: ans = ans + n: Next
If ans = goal Then count = count + 1
Wend
AnsCount = count
End Function
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment