Cycle through a list in VBA

A

azidrane

Hey, I need to just do a For...Next loop to cycle through a predefined
list (or range really).

Sort of..

For every cell in this list
Check the text in list against a variable
perform action
Next in list


that sort of thing. I just don't konw what to call the list, cell,
anything..

Thanks in advance.
 
N

Nick Hodge

azidrane

Something like

Sub Iterate()
Dim myCell as Range
For Each myCell in Range("A1:A100")
'do stuff here
Next MyCell
End Sub

You can use Selection instead of the range dsignation whih will iterate the
cells under the selection

--
HTH
Nick Hodge
Microsoft MVP - Excel
Southampton, England
www.nickhodge.co.uk
[email protected]
 
D

Dave Peterson

Something like:

Option Explicit
Sub TestMe01()

Dim myRng As Range
Dim myCell As Range
Dim myString As String

myString = "WhatDoYouWantToCompare"

Set myRng = Worksheets("sheet1").Range("a1:A1000")

For Each myCell In myRng.Cells
With myCell
If LCase(.Value) = LCase(mystr) Then
'do something
Else
'do something else or not
End If
End With
Next myCell

End Sub




If you're new to macros, you may want to read David McRitchie's intro at:
http://www.mvps.org/dmcritchie/excel/getstarted.htm
 
Top