+ Reply to Thread
Results 1 to 19 of 19

I Need Numbers to Be Duplicated

  1. #1
    Registered User
    Join Date
    02-14-2013
    Location
    Cleveland, Ohio
    MS-Off Ver
    Excel 2010
    Posts
    11

    I Need Numbers to Be Duplicated

    So the problem I am having is that the spreadsheet I am using gives me the possible combinations of an array that could total 1000 with using a max of 8 values in the array. The problem is that the combinations do not include duplicate numbers.

    Using the numbers below and only picking 8 values total you can see that you would be able to pick 114, 8 times to get a total of 912. But this macro doesn't give this result. How can I get all possible values?

    708
    443
    355
    289
    127
    126
    114
    99
    61
    60
    51
    50
    27
    19
    15
    Sub btn_GetCombos_Click()

    Dim rngNumbers As range
    Dim i As Long, j As Long, k As Long
    Dim colResults As New Collection
    Dim arrResults() As String
    Dim arrComboLoc As Variant
    Dim LocIndex As Long
    Dim dTot As Double
    Dim str As String
    Dim dTargetSum As Double
    Dim bAdvanced As Boolean

    Set rngNumbers = range("A2", Cells(Rows.Count, "A").End(xlUp))
    range("F2:F" & Rows.Count).ClearContents

    If Not IsNumeric(range("D2").Value) _
    Or Len(Trim(range("D2").Value)) = 0 Then
    range("D2").Select
    MsgBox "Must provide a Target SUM number"
    Exit Sub
    End If

    If Not IsNumeric(range("D3").Value) _
    Or Len(Trim(range("D3").Value)) = 0 Then
    range("D3").Select
    MsgBox "Must provide the number of cells to use"
    Exit Sub
    ElseIf range("D3").Value > rngNumbers.Cells.Count Then
    range("D3").Select
    MsgBox "Number of cells may not exceed total amount of cells"
    Exit Sub
    ElseIf range("D3").Value < 1 Then
    range("D3").Select
    MsgBox "Number of cells may not be less than 1"
    Exit Sub
    End If

    dTargetSum = range("D2").Value
    arrComboLoc = Application.Transpose(Evaluate("Index(Row(1:" & range("D3").Value & "),)"))

    On Error Resume Next
    For i = 1 To WorksheetFunction.Combin(rngNumbers.Count, range("D3").Value)
    dTot = 0
    str = vbNullString
    For LocIndex = LBound(arrComboLoc) To UBound(arrComboLoc)
    dTot = dTot + rngNumbers.Cells(arrComboLoc(LocIndex)).Value
    str = str & ", " & rngNumbers.Cells(arrComboLoc(LocIndex)).Value
    Next LocIndex
    If dTot <= dTargetSum Then
    str = Mid(str, 3)
    colResults.Add str, str
    End If

    bAdvanced = False
    For j = UBound(arrComboLoc) To LBound(arrComboLoc) Step -1
    If arrComboLoc(j) < rngNumbers.Cells.Count - (UBound(arrComboLoc) - j) Then
    arrComboLoc(j) = arrComboLoc(j) + 1
    For k = j + 1 To UBound(arrComboLoc)
    arrComboLoc(k) = arrComboLoc(j) + k - j
    Next k
    bAdvanced = True
    Exit For
    End If
    If bAdvanced = True Then Exit For
    Next j
    Next i

    If colResults.Count > 0 Then
    ReDim Preserve arrResults(1 To colResults.Count)
    For i = 1 To colResults.Count
    arrResults(i) = colResults(i)
    Next i
    range("F2").Resize(colResults.Count).Value = Application.Transpose(arrResults)
    Else
    MsgBox "No valid combinations found to be less than or equal to " & dTargetSum & " when using " & range("D3").Value & " cells."
    End If

    End Sub

  2. #2
    Registered User
    Join Date
    02-14-2013
    Location
    Cleveland, Ohio
    MS-Off Ver
    Excel 2010
    Posts
    11

    Re: I Need Numbers to Be Duplicated

    I think it has something to do with the Ubound, but I really don't know.
    I am using Microsoft Office 2010.

    Thank you.
    Last edited by beardmoen; 08-05-2016 at 10:13 AM.

  3. #3
    Registered User
    Join Date
    02-14-2013
    Location
    Cleveland, Ohio
    MS-Off Ver
    Excel 2010
    Posts
    11

    Re: I Need Numbers to Be Duplicated

    Any takers?
    I tried putting rem in various places in the code but it just errors out.

  4. #4
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2003, 2010
    Posts
    40,678

    Re: I Need Numbers to Be Duplicated

    There's a workbook at https://app.box.com/s/cj3cjtij207amp5q583161f97g6y8n8s that will generate the 319,770 results of 15 multichoose 8 in col C:

    B
    C
    D
    E
    F
    G
    H
    I
    J
    K
    2
    Balls Drawn
    8
    3
    Symbols
    0123456789ABCDE
    4
    # Symbols
    15
    5
    Combos
    319,770
    6
    7
    Output
    Sum
    Symbol
    Value
    Value
    Count
    8
    00000000
    120
    D8: =SUM(LOOKUP(MID(C8, {1;2;3;4;5;6;7;8}, 1), $G$8:$H$22)) 0
    15
    1000
    204
    9
    00000001
    124
    1
    19
    10
    00000002
    132
    2
    27
    11
    00000003
    155
    3
    50
    12
    00000004
    156
    4
    51
    13
    00000005
    165
    5
    60
    14
    00000006
    166
    6
    61
    15
    00000007
    204
    7
    99
    16
    00000008
    219
    8
    114
    17
    00000009
    231
    9
    126
    18
    0000000A
    232
    A
    127
    19
    0000000B
    394
    B
    289
    20
    0000000C
    460
    C
    355
    21
    0000000D
    548
    D
    443
    22
    0000000E
    813
    E
    708
    23
    00000011
    128
    24
    00000012
    136
    25
    00000013
    159
    26
    00000014
    160


    From there, I added a lookup table for your values, a formula to calculate the sum for each result using the table, and a formula to count the number of results that total 1000.
    Entia non sunt multiplicanda sine necessitate

  5. #5
    Forum Expert mikerickson's Avatar
    Join Date
    03-30-2007
    Location
    Davis CA
    MS-Off Ver
    Excel 2011
    Posts
    6,229

    Re: I Need Numbers to Be Duplicated

    ...possible combinations of an array that could total 1000
    ..
    pick 114, 8 times to get a total of 912. But this macro doesn't give this result.
    why should it give you that result, the sum doesn't total 1000.

    Putting the OP numbers in A1:A15, I used this routine to get 331 combinations that sum to 1000.
    Including 708 61 61 61 60 19 15 15 which has doubles.

    Please Login or Register  to view this content.
    Last edited by mikerickson; 08-06-2016 at 01:15 PM.
    _
    ...How to Cross-post politely...
    ..Wrap code by selecting the code and clicking the # or read this. Thank you.

  6. #6
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2003, 2010
    Posts
    40,678

    Re: I Need Numbers to Be Duplicated

    When I run that code, Mike, I get a bunch of rows with zero:

    G
    H
    I
    J
    K
    L
    M
    N
    2
    15
    15
    15
    15
    15
    127
    355
    443
    3
    15
    15
    15
    15
    19
    99
    114
    708
    4
    15
    15
    15
    15
    27
    27
    443
    443
    5
    15
    15
    15
    19
    50
    51
    127
    708
    6
    15
    15
    15
    19
    50
    443
    443
    0
    7
    15
    15
    15
    19
    51
    51
    126
    708
    8
    15
    15
    15
    19
    99
    127
    355
    355
    9
    15
    15
    15
    19
    114
    114
    708
    0
    10
    15
    15
    15
    27
    60
    61
    99
    708
    11
    15
    15
    15
    27
    61
    289
    289
    289
    12
    15
    15
    15
    60
    60
    127
    708
    0
    13
    15
    15
    15
    60
    61
    126
    708
    0
    14
    15
    15
    19
    19
    19
    27
    443
    443
    15
    15
    15
    19
    19
    50
    60
    114
    708


    When I get rid of those, there are 204 results left.
    Last edited by shg; 08-06-2016 at 01:51 PM.

  7. #7
    Forum Expert mikerickson's Avatar
    Join Date
    03-30-2007
    Location
    Davis CA
    MS-Off Ver
    Excel 2011
    Posts
    6,229

    Re: I Need Numbers to Be Duplicated

    The OP was looking for "... array that could total 1000 with using a max of 8 values in the array..."

    Those combinations with 0's are the combinations of less than 8 terms that sum to 1000.

    I suppose I should have added this to the end of my routine
    Please Login or Register  to view this content.

  8. #8
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2003, 2010
    Posts
    40,678

    Re: I Need Numbers to Be Duplicated

    using a max of 8 values
    Misread that, thank you.

  9. #9
    Forum Expert mikerickson's Avatar
    Join Date
    03-30-2007
    Location
    Davis CA
    MS-Off Ver
    Excel 2011
    Posts
    6,229

    Re: I Need Numbers to Be Duplicated

    Quote Originally Posted by shg View Post
    Misread that, thank you.
    Me too, the first time through

  10. #10
    Registered User
    Join Date
    02-14-2013
    Location
    Cleveland, Ohio
    MS-Off Ver
    Excel 2010
    Posts
    11

    Re: I Need Numbers to Be Duplicated

    Wow. These are amazing responses. I now see that I wasn't completely clear in my original question.

    possible combinations of an array that could total 1000
    I should have said that the total could be up to 1000 and not go over 1000 with a selection of 8 numbers.
    That's why I gave this example.
    pick 114, 8 times to get a total of 912

  11. #11
    Registered User
    Join Date
    02-14-2013
    Location
    Cleveland, Ohio
    MS-Off Ver
    Excel 2010
    Posts
    11

    Re: I Need Numbers to Be Duplicated

    When I tried running the code myself I get this error.
    Run-Time error 13
    Type mismatch
    It then highlights this
    Combin(8) = Terms(i8)

  12. #12
    Forum Expert mikerickson's Avatar
    Join Date
    03-30-2007
    Location
    Davis CA
    MS-Off Ver
    Excel 2011
    Posts
    6,229

    Re: I Need Numbers to Be Duplicated

    When that error occurs what are the values of i8 and Terms(i8). That sounds like there might be a text value in the column.

  13. #13
    Registered User
    Join Date
    02-14-2013
    Location
    Cleveland, Ohio
    MS-Off Ver
    Excel 2010
    Posts
    11

    Re: I Need Numbers to Be Duplicated

    I had cell I8 blank and it gave the error. I put 1000 in cell I8 and it gave the error.

  14. #14
    Forum Expert mikerickson's Avatar
    Join Date
    03-30-2007
    Location
    Davis CA
    MS-Off Ver
    Excel 2011
    Posts
    6,229

    Re: I Need Numbers to Be Duplicated

    Cell I8 is different than the VBA variable i8

    When that error happens, press Debug, open the immediate window,type ?i8,terms(i8) and press return.
    What does it say?

    (I suspect that there may be a non-numeric value in the list in column A)

  15. #15
    Registered User
    Join Date
    02-14-2013
    Location
    Cleveland, Ohio
    MS-Off Ver
    Excel 2010
    Posts
    11

    Re: I Need Numbers to Be Duplicated

    When I type in ?i8,terms(i8) it says
    Print i8, Terms(i8)

  16. #16
    Registered User
    Join Date
    02-14-2013
    Location
    Cleveland, Ohio
    MS-Off Ver
    Excel 2010
    Posts
    11

    Re: I Need Numbers to Be Duplicated

    It has something to do with the workbook that I was using. When I placed the macro into a new workbook the code worked.

    I noticed when running the macro that when it gave out the possible combinations, there were combinations that didn't use 8 numbers.

    Is there a way so that the code always uses a total of 8 numbers?

    Could the code be changed to find within a range of say 975 - 1000 ?

  17. #17
    Registered User
    Join Date
    02-14-2013
    Location
    Cleveland, Ohio
    MS-Off Ver
    Excel 2010
    Posts
    11

    Re: I Need Numbers to Be Duplicated

    Duplicate post.
    Last edited by beardmoen; 08-08-2016 at 03:41 PM.

  18. #18
    Registered User
    Join Date
    02-14-2013
    Location
    Cleveland, Ohio
    MS-Off Ver
    Excel 2010
    Posts
    11

    Re: I Need Numbers to Be Duplicated

    I just am unsure of how to change the code myself if you wouldn't mind helping.

  19. #19
    Registered User
    Join Date
    02-14-2013
    Location
    Cleveland, Ohio
    MS-Off Ver
    Excel 2010
    Posts
    11

    Re: I Need Numbers to Be Duplicated

    Again, thanks for the help. Alas, I tried tweaking the code but came up short. Could never get it to work.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. [SOLVED] Extract duplicated numbers only from two ranges
    By YasserKhalil in forum Excel Programming / VBA / Macros
    Replies: 15
    Last Post: 12-25-2015, 03:41 PM
  2. Finding non-duplicated numbers in a list
    By Ian99099 in forum Excel General
    Replies: 1
    Last Post: 02-23-2015, 01:52 PM
  3. Highlight the duplicated numbers
    By Puerto in forum Excel Formulas & Functions
    Replies: 4
    Last Post: 04-19-2014, 06:21 AM
  4. Searching for duplicated numbers
    By ridnickhick in forum Excel General
    Replies: 5
    Last Post: 07-26-2012, 06:39 PM
  5. Using Vlookup for duplicated numbers
    By fodejimi in forum Excel General
    Replies: 5
    Last Post: 05-12-2011, 09:10 AM
  6. Find Missed and Duplicated Numbers
    By maperalia in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 04-10-2006, 08:45 PM
  7. [SOLVED] Excel Adding duplicated numbers together
    By JJ Joobler in forum Excel General
    Replies: 1
    Last Post: 01-06-2005, 10:06 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1