PC Review


Reply
Thread Tools Rate Thread

Adding cells in data range at random to determine a given number

 
 
=?Utf-8?B?VmFsaWFudA==?=
Guest
Posts: n/a
 
      31st Jul 2007
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 With Quote
 
 
 
 
=?Utf-8?B?Sm9lbA==?=
Guest
Posts: n/a
 
      31st Jul 2007
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 With Quote
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

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


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 Microsoft Excel Worksheet Functions 5 27th May 2010 09:04 PM
number of times consective data occurs in a range of cells =?Utf-8?B?VGhhbmtzIGEgbG90IFJhbHBoLiBpdCB3b3JrZWQu Microsoft Excel Worksheet Functions 10 21st Jun 2007 05:21 AM
Finding Number Within Range Then Copying Data Below Number to Cells acctemp@millenniumbank.com Microsoft Excel Programming 5 16th Oct 2006 06:32 PM
Adding input box number to range of cells values =?Utf-8?B?SmVzc2ljYQ==?= Microsoft Excel Programming 4 22nd Mar 2006 06:02 PM
Adding a Random Number when appending data Lindsay Filz Microsoft Access Queries 0 5th Aug 2004 02:30 PM


Features
 

Advertising
 

Newsgroups
 


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