ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   For Each Loop w/ a range (https://www.excelbanter.com/excel-programming/327094-each-loop-w-range.html)

xlcharlie

For Each Loop w/ a range
 
The basics of my code a

For each cell in Range("A1, B3, C5")
If cell.value=x Then
'change to this value
Else
'change to another value
End If
Next Cell

But this only seems to work on the first cell. What I am trying to do is
iterate through small group of cells (not necessarily in an array) to see
whether the date in each is a good business day. If not, the code replaces
the date with a string variable. Can you not use a For Each Loop for this?

Andrea Jones

For Each Loop w/ a range
 
Try this:

Sub foreachrange()
Dim Mycells, Myrefs
Mycells = Array("A2", "A4", "A6", "A8", "A10", "A12")
For Each Myrefs In Mycells
If Sheet1.Range(Myrefs).Value 3 Then Sheet1.Range(Myrefs).Value = 20
Next
End Sub

Obviously replace the 3 and =20 with whatever you want.

Andrea Jones

"xlcharlie" wrote:

The basics of my code a

For each cell in Range("A1, B3, C5")
If cell.value=x Then
'change to this value
Else
'change to another value
End If
Next Cell

But this only seems to work on the first cell. What I am trying to do is
iterate through small group of cells (not necessarily in an array) to see
whether the date in each is a good business day. If not, the code replaces
the date with a string variable. Can you not use a For Each Loop for this?


Chip Pearson

For Each Loop w/ a range
 
Your For Each loop works fine for me. Have you tried stepping
through the code (F8 in the VBE)?


--
Cordially,
Chip Pearson
Microsoft MVP - Excel
Pearson Software Consulting, LLC
www.cpearson.com


"xlcharlie" wrote in
message
...
The basics of my code a

For each cell in Range("A1, B3, C5")
If cell.value=x Then
'change to this value
Else
'change to another value
End If
Next Cell

But this only seems to work on the first cell. What I am
trying to do is
iterate through small group of cells (not necessarily in an
array) to see
whether the date in each is a good business day. If not, the
code replaces
the date with a string variable. Can you not use a For Each
Loop for this?




Tom Ogilvy

For Each Loop w/ a range
 
Sub AB()
For Each cell In Range("A1, B3, C5")
Debug.Print cell.Address
Next cell
End Sub

produces:
$A$1
$B$3
$C$5

so your problem isn't in looping through the cells and there is no sense to
going to something more convoluted to achieve what is already being
achieved.

--
Regards,
Tom Ogilvy


"xlcharlie" wrote in message
...
The basics of my code a

For each cell in Range("A1, B3, C5")
If cell.value=x Then
'change to this value
Else
'change to another value
End If
Next Cell

But this only seems to work on the first cell. What I am trying to do is
iterate through small group of cells (not necessarily in an array) to see
whether the date in each is a good business day. If not, the code

replaces
the date with a string variable. Can you not use a For Each Loop for

this?




All times are GMT +1. The time now is 12:01 AM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
ExcelBanter.com