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?
![]() |
Patch reliability is unclear. Unless you have an immediate, pressing need to install a specific patch, don't do it. |
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, 5 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
-
Excessive security alerts
by
WSSebastian42
3 hours, 22 minutes ago -
* CrystalDiskMark may shorten SSD/USB Memory life
by
Alex5723
4 hours, 11 minutes ago -
Ben’s excellent adventure with Linux
by
Ben Myers
8 minutes ago -
Seconds are back in Windows 10!
by
Susan Bradley
5 hours, 22 minutes ago -
WebBrowserPassView — Take inventory of your stored passwords
by
Deanna McElveen
1 hour, 14 minutes ago -
OS news from WWDC 2025
by
Will Fastie
5 hours, 25 minutes ago -
Need help with graphics…
by
WSBatBytes
4 hours, 5 minutes ago -
AMD : Out of Bounds (OOB) read vulnerability in TPM 2.0 CVE-2025-2884
by
Alex5723
19 hours, 38 minutes ago -
Totally remove or disable BitLocker
by
CWBillow
18 hours, 32 minutes ago -
Windows 10 gets 6 years of ESU?
by
n0ads
6 hours, 27 minutes ago -
Apple, Google stores still offer China-based VPNs, report says
by
Nibbled To Death By Ducks
1 day, 6 hours ago -
Search Forums only bring up my posts?
by
Deo
1 day, 6 hours ago -
Windows Spotlight broken on Enterprise and Pro for Workstations?
by
steeviebops
1 day, 18 hours ago -
Denmark wants to dump Microsoft for Linux + LibreOffice
by
Alex5723
1 day, 10 hours ago -
How to get Microsoft Defender to honor Group Policy Setting
by
Ralph
1 day, 18 hours ago -
Apple : Paragon’s iOS Mercenary Spyware Finds Journalists Target
by
Alex5723
2 days, 4 hours ago -
Music : The Rose Room – It’s Been A Long, Long Time album
by
Alex5723
2 days, 5 hours ago -
Disengage Bitlocker
by
CWBillow
1 day, 19 hours ago -
Mac Mini M2 Service Program for No Power Issue
by
Alex5723
2 days, 7 hours ago -
New Win 11 Pro Geekom Setup questions
by
Deo
1 day, 6 hours ago -
Windows 11 Insider Preview build 26200.5651 released to DEV
by
joep517
2 days, 15 hours ago -
Windows 11 Insider Preview build 26120.4441 (24H2) released to BETA
by
joep517
2 days, 15 hours ago -
iOS 26,, MacOS 26 : Create your own AI chatbot
by
Alex5723
2 days, 19 hours ago -
New PC transfer program recommendations?
by
DaveBoston
1 day ago -
Windows 11 Insider Preview Build 22631.5545 (23H2) released to Release Preview
by
joep517
2 days, 23 hours ago -
Windows 10 Build 19045.6029 (22H2) to Release Preview Channel
by
joep517
2 days, 23 hours ago -
Best tools for upgrading a Windows 10 to an 11
by
Susan Bradley
2 days, 11 hours ago -
The end of Windows 10 is approaching, consider Linux and LibreOffice
by
Alex5723
1 day, 15 hours ago -
Extended Windows Built-in Disk Cleanup Utility
by
bbearren
2 days ago -
Win 11 24H2 June 2025 Update breaks WIFI
by
dportenlanger
3 days, 18 hours ago
Recent blog posts
- Ben’s excellent adventure with Linux
- Seconds are back in Windows 10!
- WebBrowserPassView — Take inventory of your stored passwords
- OS news from WWDC 2025
- Best tools for upgrading a Windows 10 to an 11
- Master patch listing for June 10, 2025
- 24H2 may not be offered June updates
- June 2025 updates are out
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.