Home |
Search |
Today's Posts |
|
#1
![]()
Posted to microsoft.public.excel.programming
|
|||
|
|||
![]()
How do I add a value to an array (in VBA) based upon the output of an if...then statement? I have two arrays -- if a certain value falls within a range in array 1, I want to add the corresponding value in array 2 to a third array, which I will use to hold values for later calculation.
Thanks in advance for your help. |
#2
![]()
Posted to microsoft.public.excel.programming
|
|||
|
|||
![]()
Hi Scott
not quite sure what you're trying to do. Could you give a more specific example (or the code you already have)? -- Regards Frank Kabel Frankfurt, Germany Scott P wrote: How do I add a value to an array (in VBA) based upon the output of an if...then statement? I have two arrays -- if a certain value falls within a range in array 1, I want to add the corresponding value in array 2 to a third array, which I will use to hold values for later calculation. Thanks in advance for your help. |
#3
![]()
Posted to microsoft.public.excel.programming
|
|||
|
|||
![]()
As an example:
store values from ar2 in ar3 if ar1 is 1 and < 10 Sub BBB() Dim ar1(1 To 10), ar2(1 To 10), ar3() ReDim ar3(1 To 1) j = 0 For i = 1 To 10 ar1(i) = Int(Rnd() * 25 - 8) ar2(i) = i * i If ar1(i) 1 And ar1(i) < 10 Then j = j + 1 ReDim Preserve ar3(1 To j) ar3(j) = ar2(i) End If Next For i = 1 To j Debug.Print ar3(i) Next End Sub -- Regards, Tom Ogilvy "Scott P" wrote in message ... How do I add a value to an array (in VBA) based upon the output of an if...then statement? I have two arrays -- if a certain value falls within a range in array 1, I want to add the corresponding value in array 2 to a third array, which I will use to hold values for later calculation. Thanks in advance for your help. |
#4
![]()
Posted to microsoft.public.excel.programming
|
|||
|
|||
![]()
If speed of execution is an issue, one might want to remove the ReDim
Preserve from the loop: Sub BBB() Dim ar1(1 To 10), ar2(1 To 10), ar3() 'ReDim ar3(1 To 1) ReDim ar3(1 To 10) j = 0 For i = 1 To 10 ar1(i) = Int(Rnd() * 25 - 8) ar2(i) = i * i If ar1(i) 1 And ar1(i) < 10 Then j = j + 1 'ReDim Preserve ar3(1 To j) ar3(j) = ar2(i) End If Next ReDim Preserve ar3(1 To j) For i = 1 To j Debug.Print ar3(i) Next End Sub Tom Ogilvy wrote: As an example: store values from ar2 in ar3 if ar1 is 1 and < 10 Sub BBB() Dim ar1(1 To 10), ar2(1 To 10), ar3() ReDim ar3(1 To 1) j = 0 For i = 1 To 10 ar1(i) = Int(Rnd() * 25 - 8) ar2(i) = i * i If ar1(i) 1 And ar1(i) < 10 Then j = j + 1 ReDim Preserve ar3(1 To j) ar3(j) = ar2(i) End If Next For i = 1 To j Debug.Print ar3(i) Next End Sub |
#5
![]()
Posted to microsoft.public.excel.programming
|
|||
|
|||
![]()
"Alan Beban" wrote...
If speed of execution is an issue, one might want to remove the ReDim Preserve from the loop: .... If execution speed were critical, one might dispense with the ReDim Preserve call entirely and use Dim ar3(1 To 10) |
Reply |
Thread Tools | Search this Thread |
Display Modes | |
|
|
![]() |
||||
Thread | Forum | |||
Create a List Output from a Horizontal Array Input | Excel Worksheet Functions | |||
Validation based on output of a different cell. | Excel Discussion (Misc queries) | |||
Output client name based on date... | Excel Worksheet Functions | |||
customise array formula output | Excel Discussion (Misc queries) | |||
Help with 1 x 2 array output | Excel Programming |