I have a column with a different integer value in each of 21 rows. How can I loop through the values until I get 12 of the values to sum to a specific amount?
![]() |
There are isolated problems with current patches, but they are well-known and documented on this site. |
SIGN IN | Not a member? | REGISTER | PLUS MEMBERSHIP |
-
Arrays
Home » Forums » AskWoody support » Productivity software by function » MS Excel and spreadsheet help » Arrays
- This topic has 4 replies, 3 voices, and was last updated 24 years, 4 months ago.
AuthorTopiccfe5001
GuestJanuary 18, 2001 at 7:04 pm #1767825Viewing 0 reply threadsAuthorReplies-
H. Legare Coleman
AskWoody PlusJanuary 18, 2001 at 9:37 pm #1776280We need a little more information:
1- Does it have to be 12 consecutive values, or should it be any combination of the 21 cells?
2- Can a particular cell be used more that once?
3- What result do you want and how do you want it. Surly you don’t want the sum since you know that going in. Do you want a list of the indexes of the cells used? Do you want a list of the values used? In an array?
I assume you are talking about doing this in VBA.
-
cfe5001
Guest -
WSgrugeon
AskWoody LoungerJanuary 19, 2001 at 8:43 am #1776345I am not going to write the VBA for you but I can give a few pointers.
I would suggest you set the first 12 cells in a column to the right of the list to equal the corresponding list number. Put a total of this column at the bottom and test if it = the target.
Then you need to work out a process for stepping through the list moving the spaces up into the list. Clearly the first step will be to make the 12th cell blank and the 13th cell = its corresponding figure. Then you make the 11 cell blank and make the 12 cell = its corresponding figure.
You would probably have a series of nested loops to do this but if you were very clever and wanted to minimise the size of the code you would use a recursive sub.
Back in the old days we used to do things like this for fun – see how much you can do with how little programming!
-
H. Legare Coleman
AskWoody PlusJanuary 19, 2001 at 6:56 pm #1776376Ok, the code below will find the combination of 12 cells in the range A1:A21 that adds up to the sum passed as a parameter to the subroutine. First, a couple of warnings. The procedure is written to expect to find an exact match on the sum and therefore is only guaranted to work on integer values. If the cells can contain real numbers, the procedure will have to be modified to deal with the rounging errors. Second, be patient. This procedure can run for a long time. To find the sum where the values were in the last 12 cells took over 5 minutes on the 400mz system I tested it on.
Public Sub Find12(dReqTot As Double) Dim I1 As Integer, I2 As Integer, I3 As Integer, I4 As Integer, I5 As Integer, I6 As Integer Dim I7 As Integer, I8 As Integer, I9 As Integer, I10 As Integer, I11 As Integer, I12 As Integer Dim dSum As Double, dList(1 To 12) As Double Dim strWk As String For I1 = 1 To 10 For I2 = I1 + 1 To 11 For I3 = I2 + 1 To 12 For I4 = I3 + 1 To 13 For I5 = I4 + 1 To 14 For I6 = I5 + 1 To 15 For I7 = I6 + 1 To 16 For I8 = I7 + 1 To 17 For I9 = I8 + 1 To 18 For I10 = I9 + 1 To 19 For I11 = I10 + 1 To 20 For I12 = I11 + 1 To 21 dSum = Cells(I1, 1) + Cells(I2, 1) + Cells(I3, 1) dSum = dSum + Cells(I4, 1) + Cells(I5, 1) + Cells(I6, 1) dSum = dSum + Cells(I7, 1) + Cells(I8, 1) + Cells(I9, 1) dSum = dSum + Cells(I10, 1) + Cells(I11, 1) + Cells(I12, 1) If dSum = dReqTot Then Exit For Next I12 If dSum = dReqTot Then Exit For Next I11 If dSum = dReqTot Then Exit For Next I10 If dSum = dReqTot Then Exit For Next I9 If dSum = dReqTot Then Exit For Next I8 If dSum = dReqTot Then Exit For Next I7 If dSum = dReqTot Then Exit For Next I6 If dSum = dReqTot Then Exit For Next I5 If dSum = dReqTot Then Exit For Next I4 If dSum = dReqTot Then Exit For Next I3 If dSum = dReqTot Then Exit For Next I2 If dSum = dReqTot Then Exit For Next I1 If dSum = dReqTot Then strWk = Cells(I1, 1) & "+" & Cells(I2, 1) & "+" & Cells(I3, 1) & "+" strWk = strWk & Cells(I4, 1) & "+" & Cells(I5, 1) & "+" & Cells(I6, 1) & "+" strWk = strWk & Cells(I7, 1) & "+" & Cells(I8, 1) & "+" & Cells(I9, 1) & "+" strWk = strWk & Cells(I10, 1) & "+" & Cells(I11, 1) & "+" & Cells(I12, 1) & "=" & dReqTot MsgBox strWk Else MsgBox "No combination adds up to " & dReqTot End If End Sub
-
-
Viewing 0 reply threads -

Plus Membership
Donations from Plus members keep this site going. You can identify the people who support AskWoody by the Plus badge on their avatars.
AskWoody Plus members not only get access to all of the contents of this site -- including Susan Bradley's frequently updated Patch Watch listing -- they also receive weekly AskWoody Plus Newsletters (formerly Windows Secrets Newsletter) and AskWoody Plus Alerts, emails when there are important breaking developments.
Get Plus!
Welcome to our unique respite from the madness.
It's easy to post questions about Windows 11, Windows 10, Win8.1, Win7, Surface, Office, or browse through our Forums. Post anonymously or register for greater privileges. Keep it civil, please: Decorous Lounge rules strictly enforced. Questions? Contact Customer Support.
Search Newsletters
Search Forums
View the Forum
Search for Topics
Recent Topics
-
June 2025 Office non-Security Updates
by
PKCano
6 minutes ago -
Secure Boot Update Fails after KB5058405 Installed
by
SteveIT
7 minutes ago -
Firefox Red Panda Fun Stuff
by
Lars220
3 minutes ago -
How start headers and page numbers on page 3?
by
Davidhs
10 hours, 27 minutes ago -
Attack on LexisNexis Risk Solutions exposes data on 300k +
by
Nibbled To Death By Ducks
5 hours, 2 minutes ago -
Windows 11 Insider Preview build 26200.5622 released to DEV
by
joep517
19 hours, 8 minutes ago -
Windows 11 Insider Preview build 26120.4230 (24H2) released to BETA
by
joep517
19 hours, 9 minutes ago -
MS Excel 2019 Now Prompts to Back Up With OneDrive
by
lmacri
8 hours, 50 minutes ago -
Firefox 139
by
Charlie
1 hour, 26 minutes ago -
Who knows what?
by
Will Fastie
14 hours, 14 minutes ago -
My top ten underappreciated features in Office
by
Peter Deegan
19 hours, 53 minutes ago -
WAU Manager — It’s your computer, you are in charge!
by
Deanna McElveen
14 hours, 16 minutes ago -
Misbehaving devices
by
Susan Bradley
22 hours, 1 minute ago -
.NET 8.0 Desktop Runtime (v8.0.16) – Windows x86 Installer
by
WSmeyerbos
2 days, 1 hour ago -
Neowin poll : What do you plan to do on Windows 10 EOS
by
Alex5723
1 hour, 10 minutes ago -
May 31, 2025—KB5062170 (OS Builds 22621.5415 and 22631.5415 Out-of-band
by
Alex5723
2 days ago -
Discover the Best AI Tools for Everything
by
Alex5723
23 hours, 35 minutes ago -
Edge Seems To Be Gaining Weight
by
bbearren
1 day, 14 hours ago -
Rufus is available from the MSFT Store
by
PL1
1 day, 22 hours ago -
Microsoft : Ending USB-C® Port Confusion
by
Alex5723
3 days, 1 hour ago -
KB5061768 update for Intel vPro processor
by
drmark
1 day, 1 hour ago -
Outlook 365 classic has exhausted all shared resources
by
drmark
1 day ago -
My Simple Word 2010 Macro Is Not Working
by
mbennett555
2 days, 21 hours ago -
Office gets current release
by
Susan Bradley
2 days, 23 hours ago -
FBI: Still Using One of These Old Routers? It’s Vulnerable to Hackers
by
Alex5723
4 days, 14 hours ago -
Windows AI Local Only no NPU required!
by
RetiredGeek
3 days, 22 hours ago -
Stop the OneDrive defaults
by
CWBillow
4 days, 14 hours ago -
Windows 11 Insider Preview build 27868 released to Canary
by
joep517
5 days ago -
X Suspends Encrypted DMs
by
Alex5723
5 days, 2 hours ago -
WSJ : My Robot and Me AI generated movie
by
Alex5723
5 days, 3 hours ago
Recent blog posts
Key Links
Want to Advertise in the free newsletter? How about a gift subscription in honor of a birthday? Send an email to sb@askwoody.com to ask how.
Mastodon profile for DefConPatch
Mastodon profile for AskWoody
Home • About • FAQ • Posts & Privacy • Forums • My Account
Register • Free Newsletter • Plus Membership • Gift Certificates • MS-DEFCON Alerts
Copyright ©2004-2025 by AskWoody Tech LLC. All Rights Reserved.