MACRO TO FIND 3 NUMBERS IN A LIST THAT MAKE A TOTAL (SOLVED)

BrianB

Well-known Member
Joined
Feb 17, 2003
Messages
8,127
** PLEASE DO NOT REPLY TO THIS MESSAGE**
This is to demonstrate the principle for solving a common problem.
It is necessary to have a separate subroutine to test different size sets of numbers. So if a total is not matched by adding 2 numbers the macro goes on to try 3,4,5 etc.

As the size of set increases so does the run time. This obviously depends on how many numbers in the list. With around 100 numbers and checking sets of numbers bigger than 4 you probably need an overnight run. Some time can be save by ordering the numbers. eg. if searching for a negative number have all negative numbers at the top of the list.
Code:
'=== copy from here to the end =======================
'- MACRO TO FIND A 3 NUMBER SET IN A LIST
'- TO MAKE A GIVEN TOTAL
'- Check total in cell A1 - other numbers A2 down
'- add loops to adapt to different size sets
'- results put to column B
'- Brian Baulsom January 2003
'=====================================================
Option Base 1
Dim NumberSheet As Worksheet
Dim NumberList() As Variant     ' array of values
Dim N1 As Long
Dim N2 As Long
Dim N3 As Long
Dim LastRow As Long
Dim MyRow As Long
Dim CheckNumber As Double
Dim CheckSum As Double
Dim SetFound As Boolean

'========================================================
'- MAIN ROUTINE
'========================================================
Sub FIND_NUMBERS()
    Application.Calculation = xlCalculationManual
    Set NumberSheet = ActiveSheet
    '----------------------------------------------------
    '- checksum in cell A1
    CheckNumber = NumberSheet.Range("A1").Value
    '----------------------------------------------------
    '- number array
    LastRow = NumberSheet.Range("A65536").End(xlUp).Row
    ReDim NumberList(LastRow)
    For r = 1 To LastRow
        NumberList(r) = NumberSheet.Cells(r + 1, 1).Value
    Next
    '-----------------------------------------------------
    '- loops - try 2 numbers (not shown), then 3 ... etc.
    'SET2 (suboutine not here)
    'If SetFound = True Then GoTo GetOut
    '-----------------------------------
    SET3        ' run subroutine
    If SetFound = True Then GoTo GetOut
    '-----------------------------------------------------
    MsgBox ("Total not found.")
GetOut:
    Application.StatusBar = False
    Application.Calculation = xlCalculationAutomatic
End Sub
'------------------------------------------------------------

'============================================================
'- SUBROUTINE TO FIND 3 NUMBERS THAT MAKE UP THE TOTAL
'============================================================
Private Sub SET3()
    SetFound = False
    For N1 = 1 To LastRow
        For N2 = N1 + 1 To LastRow
            For N3 = N2 + 1 To LastRow
                '---------------------------------------------------
                '- check total
                Application.StatusBar = _
                    " 3 Numbers " & N1 & ":" & N2 & ":" & N3
                CheckSum = _
                    NumberList(N1) + NumberList(N2) + NumberList(N3)
                '---------------------------------------------------
                '- change the next line according to the closeness of match required
                If Abs(CheckNumber - CheckSum) < 1 Then '
                    NumberSheet.Range("B1").Value = NumberList(N1)
                    NumberSheet.Range("B2").Value = NumberList(N2)
                    NumberSheet.Range("B3").Value = NumberList(N3)
                    MsgBox ("Please see results in column B")
                    SetFound = True
                    Exit Sub
                End If
                '---------------------------------------------------
            Next N3
        Next N2
    Next N1
End Sub
'-------------------------------------------------------------------
'== copy down to here ==============================================
 

Excel Facts

How to show all formulas in Excel?
Press Ctrl+` to show all formulas. Press it again to toggle back to numbers. The grave accent is often under the tilde on US keyboards.

Forum statistics

Threads
1,224,918
Messages
6,181,735
Members
453,064
Latest member
robatthe2A

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