Hello,
I'm having trouble coming up with a way to determine which specific variables from a group add up to a specific number.
The macro will begin on a screen which displays a particular 7-digit value. It picks up this amount as a variable, let's call it TE. It will then scroll to subsequent pages and pick up a value off each page (we'll call these E1, E2, E3, etc). It then needs to report back which of these numbers total up to exactly equal TE.
The number of pages varies on each file we process and there could be as many as 20 pages in a single file. Due to the nature of the source material, there will always be only one possible combination that adds up to TE, so the first combination this macro finds that equals TE will always be the correct solution. Any E values that are greater than TE can be ignored, as subtraction is never a factor; these amounts can only be added together.
Hypothetical example:
TE = 100
E1 = 50
E2 = 30
E3 = 21
E4 = 119
E5 = 60
E6 = 12
E7 = 15
E8 = 8
E9 = 105
E10 = 86
The solution is E1 + E2 + E6 + E8. How do I get the macro to determine that combination on its own and report this back to the user?
The only way I know of is to write a line of code to sum every possible combination of E variables and just find the one that equals 100. But if we have as many as 20 of the E variables, the sheer number of possible combinations is beyond the scope of this macro.
How would I make this into a reasonable amount of code, including the ability to report the correct combination of E variables back to the user in a msgbox at the end? The format would need to be "E1 + E2 + E6 + E8" and not the actual numerical values.
Thanks in advance for any help provided to my problem.
I'm having trouble coming up with a way to determine which specific variables from a group add up to a specific number.
The macro will begin on a screen which displays a particular 7-digit value. It picks up this amount as a variable, let's call it TE. It will then scroll to subsequent pages and pick up a value off each page (we'll call these E1, E2, E3, etc). It then needs to report back which of these numbers total up to exactly equal TE.
The number of pages varies on each file we process and there could be as many as 20 pages in a single file. Due to the nature of the source material, there will always be only one possible combination that adds up to TE, so the first combination this macro finds that equals TE will always be the correct solution. Any E values that are greater than TE can be ignored, as subtraction is never a factor; these amounts can only be added together.
Hypothetical example:
TE = 100
E1 = 50
E2 = 30
E3 = 21
E4 = 119
E5 = 60
E6 = 12
E7 = 15
E8 = 8
E9 = 105
E10 = 86
The solution is E1 + E2 + E6 + E8. How do I get the macro to determine that combination on its own and report this back to the user?
The only way I know of is to write a line of code to sum every possible combination of E variables and just find the one that equals 100. But if we have as many as 20 of the E variables, the sheer number of possible combinations is beyond the scope of this macro.
How would I make this into a reasonable amount of code, including the ability to report the correct combination of E variables back to the user in a msgbox at the end? The format would need to be "E1 + E2 + E6 + E8" and not the actual numerical values.
Thanks in advance for any help provided to my problem.