VBA Store Values In Array Based On Criteria Then Pass Out

Lewzerrrr

Active Member
Joined
Jan 18, 2017
Messages
256
Hi,

In a part of my code I need to use a For Each Cell In Array("","","").. so I thought maybe I should dynamically do this using an array variable but i'm stuck..

What I'd like it to be is like something along the lines of below. The Sheet8.Cells is also defined as [UserRange] but I couldn't get this to work. Any help?

Code:
Dim MyArr() as Variant

For I = 1 to 28

 If Sheet8.Cells(I + 2, 4) = [MyName] then MyArr(I) = Sheet8.Cells(I + 2, 4)

Next I

For Each Cell In Array(MyArr)

CODE HERE

Next Cell
 

Excel Facts

Enter current date or time
Ctrl+: enters current time. Ctrl+; enters current date. Use Ctrl+: Ctrl+; Enter for current date & time.
You could use a collection instead:

Code:
Dim MyArr As New Collection


For I = 1 To 28
    If Sheet8.Cells(I + 2, 4) = [MyName] Then MyArr.Add Sheet8.Cells(I + 2, 4), Sheet8.Cells(I + 2, 4).Address
Next I


For Each Cell In MyArr
    ' CODE HERE
Next MyCell

WBD
 
Upvote 0
How about
Code:
Dim MyArr() As Range

For i = 1 To 28
   If Sheet8.Cells(i + 2, 4) = [myname] Then
      ReDim Preserve MyArr(1 To i)
      Set MyArr(i) = Sheet8.Cells(i + 2, 4)
   End If
Next i

For Each Cell In MyArr

'code Here

Next Cell
 
Upvote 0

Forum statistics

Threads
1,226,097
Messages
6,188,888
Members
453,509
Latest member
mdperry8190

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