Cycle through a list in VBA

  • Thread starter Thread starter azidrane
  • Start date Start date
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.
 
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
(e-mail address removed)
 
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
 

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