Multiple invoice values add up to given sum

Oakwoodbespoke

New Member
Joined
Jun 27, 2023
Messages
27
Office Version
  1. 365
Platform
  1. Windows
Hi

I have a Macro that I found on the web to anaylyze a list of values and tell me which one adds up to a given amount.

While it works with whole number it doesn't work with currency/accountancy where the values are such as £345.26

use =TRANSPOSE(MakeupANUMBER(A2:A10,B2)) in D2 to calc results



I have added the current code below


Public Function MakeupANumber(xNumbers As Range, xCount As Long)
'updateby Extendoffice
Dim arrNumbers() As Long
Dim arrRes() As String
Dim ArrTemp() As Long
Dim xIndex As Long
Dim rg As Range

MakeupANumber = ""

If xNumbers.CountLarge = 0 Then Exit Function
ReDim arrNumbers(xNumbers.CountLarge - 1)

xIndex = 0
For Each rg In xNumbers
If IsNumeric(rg.Value) Then
arrNumbers(xIndex) = CLng(rg.Value)
xIndex = xIndex + 1
End If
Next rg
If xIndex = 0 Then Exit Function

ReDim Preserve arrNumbers(0 To xIndex - 1)
ReDim arrRes(0)

Call Combinations(arrNumbers, xCount, ArrTemp(), arrRes())
ReDim Preserve arrRes(0 To UBound(arrRes) - 1)
MakeupANumber = arrRes
End Function

Private Sub Combinations(Numbers() As Long, Count As Long, ArrTemp() As Long, ByRef arrRes() As String)

Dim currentSum As Long, i As Long, j As Long, k As Long, num As Long, indRes As Long
Dim remainingNumbers() As Long, newCombination() As Long

currentSum = 0
If (Not Not ArrTemp) <> 0 Then
For i = LBound(ArrTemp) To UBound(ArrTemp)
currentSum = currentSum + ArrTemp(i)
Next i
End If

If currentSum = Count Then
indRes = UBound(arrRes)
ReDim Preserve arrRes(0 To indRes + 1)

arrRes(indRes) = ArrTemp(0)
For i = LBound(ArrTemp) + 1 To UBound(ArrTemp)
arrRes(indRes) = arrRes(indRes) & "," & ArrTemp(i)
Next i
End If

If currentSum > Count Then Exit Sub
If (Not Not Numbers) = 0 Then Exit Sub

For i = 0 To UBound(Numbers)
Erase remainingNumbers()
num = Numbers(i)
For j = i + 1 To UBound(Numbers)
If (Not Not remainingNumbers) <> 0 Then
ReDim Preserve remainingNumbers(0 To UBound(remainingNumbers) + 1)
Else
ReDim Preserve remainingNumbers(0 To 0)
End If
remainingNumbers(UBound(remainingNumbers)) = Numbers(j)

Next j
Erase newCombination()

If (Not Not ArrTemp) <> 0 Then
For k = 0 To UBound(ArrTemp)
If (Not Not newCombination) <> 0 Then
ReDim Preserve newCombination(0 To UBound(newCombination) + 1)
Else
ReDim Preserve newCombination(0 To 0)
End If
newCombination(UBound(newCombination)) = ArrTemp(k)

Next k
End If

If (Not Not newCombination) <> 0 Then
ReDim Preserve newCombination(0 To UBound(newCombination) + 1)
Else
ReDim Preserve newCombination(0 To 0)
End If

newCombination(UBound(newCombination)) = num

Combinations remainingNumbers, Count, newCombination, arrRes
Next i

End Sub
 

Attachments

  • Screenshot 2025-04-01 125549.jpg
    Screenshot 2025-04-01 125549.jpg
    29.2 KB · Views: 6
Hi Oakwoodbespoke. The code at this link does what you want. HTH. Dave
 
Upvote 0
Solution

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