Get the particular list of values in another column

Feroz90

Board Regular
Joined
Apr 25, 2019
Messages
52
Hi All,

I am trying to find a particular value in the list with SUM function.

Example: 100, 120, 150, 180, 120, 100, 140

And the result I need should be 450, whereas if you add the 150, 180 & 120 from the list, we will get that particular value (450).

I looked on internet and I found the below code to do that.

Function GetCombination(CoinsRange As Range, SumCellId As Double) As String
Dim xStr As String
Dim xSum As Double
Dim xCell As Range
xSum = SumCellId
For Each xCell In CoinsRange
If Not (xSum / xCell < 1) Then
xStr = xStr & Int(xSum / xCell) & " of " & xCell & " "
xSum = xSum - (Int(xSum / xCell)) * xCell
End If
Next
GetCombination = xStr
End Function

Like i will be getting it as 1 of 150, 1 of 180, 1 of 120, but the problem is if the list has 150, 150, 150, 150, 100, 100, 120, I am getting the result as "3 of 150".

What I need is let the list in Range ("A") column, and the result I am waiting for will be in Range("B") column, All I want is, I need only the numbers which are the exact results should be listed in Range("C") column without this "1 of" or "2 of".

Can anyone give it a try.

Thanks in advance
 

Excel Facts

Create a Pivot Table on a Map
If your data has zip codes, postal codes, or city names, select the data and use Insert, 3D Map. (Found to right of chart icons).
instead of
Rich (BB code):
xStr = xStr & Int(xSum / xCell) & " of " & xCell & " "


try

Rich (BB code):
Rich (BB code):
xStr = xStr & Int(xSum / xCell)


I think that will do it. The & " of " & xCell & " " is the 1 of, or 2 of in your code.
 
Upvote 0
have you used msgbox to get the values of xStr and xCell as the code loops through? That would be my next bit of troubleshooting.
 
Upvote 0
No i don't

insert some msgbox's to show you exactly what those values are as the code runs. This should help identify how to modify the code better.

Rich (BB code):
Function GetCombination(CoinsRange As Range, SumCellId As Double) As String
Dim xStr As String
Dim xSum As Double
Dim xCell As Range
xSum = SumCellId
msgbox "xSum = " xSum
For Each xCell In CoinsRange
If Not (xSum / xCell < 1) Then
xStr = xStr & Int(xSum / xCell) & " of " & xCell & " "
msgbox "xStr & Int(xSum / xCell) = " xStr
xSum = xSum - (Int(xSum / xCell)) * xCell
msgbox "xSum - (Int(xSum / xCell)) = " xSum
End If
Next
GetCombination = xStr
End Function


Something like that.
 
Last edited:
Upvote 0

Forum statistics

Threads
1,214,592
Messages
6,120,433
Members
448,961
Latest member
nzskater

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