Read a Range into an Array

S.H.A.D.O.

Well-known Member
Joined
Sep 6, 2005
Messages
1,915
Hi everyone,

In a Worksheet named "Input" and in Cells "B3:G?" I have 6 number groups. How can I ...

( 1 ) Put each of the 6 number groups into an array when I don't know what the last row number is.

( 2 ) Find the maximum value in any of those cells and attach it to a variable named MaxVal.

... so once I have done this I can then loop through each 6 number group and perform some calculations.

Thanks in Advance.
 
Hi dcardno,

Thanks for the reply.
I have so many different codes at the moment on this, is it possible for you to post the working code you used above please.

Thanks in Advance.
 
Upvote 0

Excel Facts

What does custom number format of ;;; mean?
Three semi-colons will hide the value in the cell. Although most people use white font instead.
Hi SHADO - I will post it when I get home, since I was working on it there over the weekend. I am GMT-8, so it will be sometime in the wee hours! If you want to send me a PM with an e-mail address I can send the s/sheet, including the test values: it might be that despite our descriptions of what we are doing, we are still laying them out differently...
 
Upvote 0
SHADO - with the spreadsheet containing:
MrExcelWheelFile.xls
BCDEFG
3123459
4135679
5245678
Sheet1

When I run the code below, I get:
Code:
For 1 - 9 there are 9 different combinations of 1 numbers.
For 1 - 9 there are 36 different combinations of 2 numbers.
For 1 - 9 there are 84 different combinations of 3 numbers.
For 1 - 9 there are 126 different combinations of 4 numbers.
For 1 - 9 there are 126 different combinations of 5 numbers.
For 1 - 9 there are 84 different combinations of 6 numbers.
For 1 - 9 there are 36 different combinations of 7 numbers.
For 1 - 9 there are 9 different combinations of 8 numbers.
For 1 - 9 there are 1 different combinations of 9 numbers.

Result        Covered       (Tested)
 2 if 2        24           ( 36 )
 2 if 3        80           ( 84 )
 3 if 3        36           ( 84 )
 2 if 4        126          ( 126 )
 3 if 4        105          ( 126 )
 4 if 4        29           ( 126 )
 2 if 5        126          ( 126 )
 3 if 5        125          ( 126 )
 4 if 5        81           ( 126 )
 5 if 5        12           ( 126 )
 2 if 6        84           ( 84 )
 3 if 6        84           ( 84 )
 4 if 6        80           ( 84 )
 5 if 6        34           ( 84 )
 6 if 6        2            ( 84 )
 2 if 7        36           ( 36 )
 3 if 7        36           ( 36 )
 4 if 7        36           ( 36 )
 5 if 7        30           ( 36 )
 6 if 7        6            ( 36 )
 7 if 7        0            ( 36 )

The code I am running is:
Code:
Option Explicit

Private Type Wheel
    A          As Currency
End Type

Private Type Digits
    B(0 To 7)  As Byte
End Type

Private BC(0 To 255) As Byte
'   Private WHL(0 To 20) As Wheel ' Do not use 0th item
Private WHL()  As Wheel    ' Do not use 0th item
Private Tested As Long

Const POOL = 9

Sub Form_Load()
Dim idx        As Currency, tly&, cmb&, pik&, win&, output&, p&, tlyn&
Dim iDataRows  As Long
Dim i          As Integer

' Build bit count lookup table
For idx = 0 To 255
    BC(idx) = Nibs(idx And 15) + Nibs(idx \ 16)
Next

' Enumerate different combinations
For cmb = 1 To POOL
    tly = 0
    For idx = 0 To (2 ^ POOL) - 1
        If BitCount(idx / 5000) = cmb Then
            tly = tly + 1

            'If BitCount(idx / 5000)< cmb Then
            '   tlyn = tlyn + 1
            ' End If

        End If

    Next
    Debug.Print "For 1 -"; POOL; "there are"; tly; "different combinations of"; cmb; "numbers."
Next

'  SetWheel 1, 1, 2, 3, 4, 5, 9
'  SetWheel 2, 1, 3, 5, 6, 7, 9
'  SetWheel 3, 2, 4, 5, 6, 7, 8

iDataRows = [datastart].End(xlDown).Row - [datastart].Row
ReDim WHL(0 To iDataRows + 1)
For i = 0 To iDataRows
    SetWheel2 i + 1
Next i

Debug.Print
Debug.Print "Result", "Covered", "(Tested)"

' Find matches
win = 7
For pik = 2 To win
    For cmb = 2 To pik
        Tested = 0
        tly = Matching(cmb, pik, win)
        Debug.Print cmb; "if"; pik, tly, "("; Tested; ")"
    Next
Next
End Sub

Private Sub SetWheel2(Index As Long)
Dim vlu
Dim cell       As Long
Dim bit        As Long
Dim dgt        As Digits
Dim Wh         As Wheel
Dim i          As Long
For i = 0 To 5
    cell = [datastart].Offset(Index - 1, i) \ 8
    bit = [datastart].Offset(Index - 1, i) And 7
    dgt.B(cell) = dgt.B(cell) Or (2 ^ bit)
Next i
LSet Wh = dgt
WHL(Index).A = Wh.A
End Sub

Private Sub SetWheel(ByVal Index As Long, ParamArray Num())

Dim vlu, cell As Long, bit As Long
Dim dgt As Digits, Wh As Wheel
For Each vlu In Num
    cell = vlu \ 8
    bit = vlu And 7
    dgt.B(cell) = dgt.B(cell) Or (2 ^ bit)
Next
LSet Wh = dgt
WHL(Index).A = Wh.A
End Sub

Private Function Matching(ByVal match As Long, ByVal pick As Long, ByVal win As Long) As Long
Dim op1 As Wheel, op2 As Wheel
Dim idx1 As Long, idx2 As Long

' Loop through all possible combinations
For idx1 = 0 To (2 ^ POOL)
    ' Limit to the 'if X' value
    If BitCount(idx1 / 5000) = pick Then
        op1.A = idx1 / 5000
        DoEvents
        Tested = Tested + 1
        ' Loop through items in wheel
        idx2 = 1
        While idx2< UBound(WHL) And WHL(idx2).A > 0
            op2.A = WHL(idx2).A
            idx2 = idx2 + 1
            ' Check for matching numbers
            If BitCount(BigAnd(op1, op2)) >= match Then
                Matching = Matching + 1
                ' Point to 0th item in wheel to exit loop
                idx2 = 0
            End If
        Wend
    End If
Next

End Function

Private Function BigAnd(W1 As Wheel, W2 As Wheel) As Currency
Dim d1 As Digits, d2 As Digits, d3 As Digits
Dim idx        As Long
LSet d1 = W1
LSet d2 = W2
For idx = 0 To 7
    d3.B(idx) = d1.B(idx) And d2.B(idx)
Next
LSet W2 = d3
BigAnd = W2.A
End Function

Private Function BitCount(ByVal X As Currency) As Long
Dim W As Wheel, d As Digits
Dim idx As Long, cnt As Long
W.A = X
LSet d = W
For idx = 0 To 7
    cnt = cnt + BC(d.B(idx))
Next
BitCount = cnt
End Function

Private Function Nibs(ByVal Value As Byte) As Long
Select Case Value
    Case 0
        Exit Function
    Case 1, 2, 4, 8
        Nibs = 1
        Exit Function
    Case 3, 5, 6, 9, 10, 12
        Nibs = 2
        Exit Function
    Case 7, 11, 13, 14
        Nibs = 3
        Exit Function
    Case 15
        Nibs = 4
End Select
End Function

Note the change to the Matching function, which now tests idx2 to ensure that it does not exceed the UBound of the WHL array. When I run the code with the hard-coded initial values active (remmed out in the code above) and calling the original SetWheel routine (with the call to SetWheel2 remmed out) I get the same result. Note that the same modification is still in place in the Matching function, and that the call to SetWheel has to follow the lines that determine iDataRows and ReDim WHL().
 
Upvote 0

Forum statistics

Threads
1,215,159
Messages
6,123,351
Members
449,097
Latest member
thnirmitha

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top