Adding to Dynamic Array based on IF Function

M1k3yboi92

New Member
Joined
May 21, 2013
Messages
7
Hi Everyone,

I have been searching through many Forums and Websites trying to answer this question. Some seem to show some of what i am trying to do but not all of it and i keep getting Syntax errors.

Basically i want to create an Array that is dynamic and the Values that are put into the Array are dependant on a set IF function Criteria.

I have 3 columns. for Ease we will say A, B & C.

If C has the Value "N" and A has Value Less than 2 then i want to put the Value of B into an Array.

The If Function is settled in a For Each Loop


_________________________________________

Dim Arr() as Variant

For Each Rng In Range("C3:C" & lastrow)
rw = Rng.Row
If Rng.Value = "Y" Then
Y = Y + 1
If Range("A" & rw).Value <= "2" Then
PCY = PCY + 1
End If
ElseIf Rng.Value = "N" Then
N = N + 1
If Range("A" & rw).Value <= "2" Then
PCN = PCN + 1
ReDim Preserve Arr(0 To UBound(Arr) + 1) As Variant
Arr(UBound(Arr)) = Range("B" & rw).Value
End If
End If
Next
_______________________________________________________________________

Most of the Forums / websites are showing examples of when people have already inputted Values for the array or are using a range on the Sheet. The only Range i have is that it will be in Column B but i do not know if there will be any values to put in the array or the other extreme i do not know how many will be put in the array.

Table looks similar to this,
ABC
2
1009Y
321N
232N
2901Y
32456N

<tbody>
</tbody>

Once the array have been built. I then need to paste the value in the array into another set of Cells. like below:

21
32
2456

<tbody>
</tbody>


any help would be really appreciated here!

Cheers,
Mike
 

Excel Facts

What is =ROMAN(40) in Excel?
The Roman numeral for 40 is XL. Bill "MrExcel" Jelen's 40th book was called MrExcel XL.
once youve filled the array you can fill range C with something like

Code:
Range("C2").Resize(UBound(arr) + 1).Value = Application.Transpose(arr)
 
Upvote 0
what if you change it to

PCN = PCN + 1
ReDim Preserve Arr(1 To PCN)
Arr(PCN) = Range("B" & rw).Value
 
Upvote 0
your array is empty so you cant use ubound(arr) when redim
use ReDim Preserve Arr(1 To PCN)

if u use 0 to PCN your first item in array will be empty
 
Upvote 0

Forum statistics

Threads
1,214,657
Messages
6,120,764
Members
448,991
Latest member
Hanakoro

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top