Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 26
Default Add value to array based on if...then output in VBA

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   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 3,885
Default Add value to array based on if...then output in VBA

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   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 27,285
Default Add value to array based on if...then output in VBA

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   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 783
Default Add value to array based on if...then output in VBA

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   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 733
Default Add value to array based on if...then output in VBA

"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
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
Create a List Output from a Horizontal Array Input Rob Excel Worksheet Functions 4 September 17th 09 05:40 PM
Validation based on output of a different cell. cdb Excel Discussion (Misc queries) 4 March 23rd 07 07:37 PM
Output client name based on date... Ted McCastlain Excel Worksheet Functions 0 September 5th 06 09:15 PM
customise array formula output TUNGANA KURMA RAJU Excel Discussion (Misc queries) 0 October 25th 05 05:15 AM
Help with 1 x 2 array output jomni[_3_] Excel Programming 3 April 2nd 04 03:49 PM


All times are GMT +1. The time now is 10:54 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"