Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 12
Default Adding cells in data range at random to determine a given number

Is there any macro or script that can be run in Excel that allows you to add
random cells to determine a fixed number? For example, a simple data range
in a column could be as follows:

1,3,5,7,9

and I want to know which cells equal '8' - the script will then add cells in
a sequenced order e.g. 1+3, 1+5, 1+7 ... 3+1, 3+5 .... 1+3+5, 1+3+7 etc.
until it finds the right answer, or a selection of answers (e.g. in my
example above, it could be 1+7 & 5+3).

I'm aware that this will be very processor intensive and will probably take
a long time to run, but is this possible and has a macro / script already
been written that I can purchase / use?

Thanks in advance!
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 9,101
Default Adding cells in data range at random to determine a given number

This code works. these two line change to get diffferent results
Data = Array(1, 3, 6, 8)
FindTotal = 12

I didn't know hwre you data originated so Isimple put the values in an array.

code start below here

Public Data
Public RowCount
Public TempArray(100)
Public FindTotal
Public DataLen
Sub gettcombinations()
Dim Level As Variant
Dim CountDigits As Integer

Data = Array(1, 3, 6, 8)
FindTotal = 12

DataLen = UBound(Data) + 1
RowCount = 1

For CountDigits = 1 To (DataLen + 1)
Level = 1
Call RecursiveAdd(Level, CountDigits)
Next CountDigits
End Sub

Sub RecursiveAdd(Level, CountDigits)
Dim count As Integer
For count = 1 To DataLen
'check to see if number already in array
'temp array contains the index of tthe array
'not the actual number
Found = False
For ArrayIndex = 1 To (Level - 1)
If TempArray(ArrayIndex) = count Then
Found = True
Exit For
End If
Next ArrayIndex
If Found = False Then
TempArray(Level) = count
If Level = CountDigits Then
Total = 0
For i = 1 To Level
Total = Total + Data(TempArray(i) - 1)
Next i

If Total = FindTotal Then
For j = 1 To Level
Cells(RowCount, j) = Data(TempArray(j) - 1)
Next j

RowCount = RowCount + 1
End If
Else
Call RecursiveAdd(Level + 1, CountDigits)
End If
End If
Next count
End Sub


"Valiant" wrote:

Is there any macro or script that can be run in Excel that allows you to add
random cells to determine a fixed number? For example, a simple data range
in a column could be as follows:

1,3,5,7,9

and I want to know which cells equal '8' - the script will then add cells in
a sequenced order e.g. 1+3, 1+5, 1+7 ... 3+1, 3+5 .... 1+3+5, 1+3+7 etc.
until it finds the right answer, or a selection of answers (e.g. in my
example above, it could be 1+7 & 5+3).

I'm aware that this will be very processor intensive and will probably take
a long time to run, but is this possible and has a macro / script already
been written that I can purchase / use?

Thanks in advance!

Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
How do I determine which data in a range add up to a fixed number? Mray Excel Worksheet Functions 5 May 27th 10 09:04 PM
Adding random #s in a range to equal a specific # Jeff Excel Worksheet Functions 4 April 28th 08 02:46 PM
Finding Number Within Range Then Copying Data Below Number to Cells [email protected] Excel Programming 5 October 16th 06 06:32 PM
Adding input box number to range of cells values Jessica Excel Programming 4 March 22nd 06 06:02 PM
Adding two columns to determine number of days to pay Katherine[_4_] Excel Programming 3 August 18th 04 08:18 PM


All times are GMT +1. The time now is 12:33 AM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"