For each next

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I have a macro that cycles through each cell in a range, eg

for each CELL in RANGE

This seems to work OK until I include something in the code that changes the
value of CELL, and then the macro just ends at that point.

I have tried declaring CELL as a range but no success.

Any clues as to why this might happen?

Regards, Kaval
 
Hi Kaval,

Sub TestIt2()
Dim rCell As Range
Dim rng As Range

Set rng = Range("A1:A10")

For Each rCell In rng
With rCell
If IsNumeric(.Value) Then
.Value = .Value * 2
End If
End With
Next rCell

End Sub
 
Thanks, I since discovered I had a worksheet change event that was running
when I changed the cells and this was ending the code execution.

Thanks.
 
Thanks, I since discovered I had a worksheet change event that was running

Would this idea help?

Sub Demo()
Dim Cell As Range

'// Don't be interrupted by "Worksheet_Change"
Application.EnableEvents = False
For Each Cell In [A1:C4]
Cell = Rnd
Next
Application.EnableEvents = True
End Sub
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top