Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 37
Default 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?
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 63
Default 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?

  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 7,247
Default 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?



  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 27,285
Default 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?


Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
loop in range Aksel Børve Excel Programming 3 March 9th 05 02:30 PM
Loop in a Range aksel børve Excel Programming 2 March 8th 05 06:40 PM
Loop through Range and Cut and Paste Kevin L Excel Programming 2 February 16th 05 06:19 PM
Loop through a range Fred[_21_] Excel Programming 6 October 22nd 04 10:45 PM
Increasing a range in a loop ric Excel Programming 4 April 26th 04 07:59 PM


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

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"