Within an IF statement, populate an array with values

cfdh_edmundo

Board Regular
Joined
Nov 9, 2005
Messages
133
Hi,

I've got a very basic if statement in VBA that color codes cells and writes text in an adjacent cell when conditions are met. The code is as follows and works exactly as I need:


Code:
If Cells(rowCount, 15).Value = Cells(rowCount - 1, 15).Value And Cells(rowCount, 16).Value <> Cells(rowCount - 1, 16).Value Then
    Cells(rowCount, 17).Value = "Flagging Point"
    Cells(rowCount, 15).Interior.ColorIndex = 36
    Cells(rowCount, 16).Interior.ColorIndex = 36
End If

What I would like to do is, in addition to the above, put Cells(rowCount, 15).Value into a one dimensional array if the condition is met. I havent yet declared the array.

Any help would be great.

Many thanks,

Chris
 

Excel Facts

Bring active cell back into view
Start at A1 and select to A9999 while writing a formula, you can't see A1 anymore. Press Ctrl+Backspace to bring active cell into view.
Is this part of a loop with "rowcount" being the counter?
 
Upvote 0
Yes, exactly that, my if statement is within a for loop which loops on rowCount

Rich (BB code):
NumRows = Range("A1", Range("A1").End(xlDown)).Rows.Count

For rowCount = 14 To NumRows - 1

'(then here is the IF statement and some other IF statements)

Next rowCount
 
Upvote 0
I think I may have figured out a workaround:


I declared flagString As String and then added this bit into the IF statement

Rich (BB code):
If Cells(rowCount, 15).Value = Cells(rowCount - 1, 15).Value And Cells(rowCount, 16).Value <> Cells(rowCount - 1, 16).Value Then
    Cells(rowCount, 17).Value = "Flagging Point"
    Range(Cells(rowCount - 1, 15), Cells(rowCount, 16)).Interior.ColorIndex = 36
    flagString = flagString & Cells(rowCount, 15).Value & "~"
 
End If

Then after the for loop ends, I add in this code:

Rich (BB code):
flagArray = Split(flagString, "~")
MsgBox Join(flagArray, vbCrLf)

this seems to work!
 
Upvote 0
Ok, how about
Code:
NumRows = Range("A1", Range("A1").End(xlDown)).Rows.Count
ReDim Ary(1 To NumRows) As Variant
For RowCount = 14 To NumRows - 1

   If Cells(RowCount, 15).Value = Cells(RowCount - 1, 15).Value And Cells(RowCount, 16).Value <> Cells(RowCount - 1, 16).Value Then
       Cells(RowCount, 17).Value = "Flagging Point"
       Cells(RowCount, 15).Interior.ColorIndex = 36
       Cells(RowCount, 16).Interior.ColorIndex = 36
       i = i + 1
       Ary(i) = Cells(RowCount, 15).Value
   End If
Next RowCount
 
Upvote 0

Forum statistics

Threads
1,224,828
Messages
6,181,204
Members
453,022
Latest member
RobertV1609

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