PDA

View Full Version : How to access the items in a row or column(i.e a cell) in Excel as Objects( Items ) ?



VinoBob
01-05-2010, 07:26 AM
Hello!


I would not like to declare a variable to iterate all cells in a row or a column so i wnat to use the For-each loop for each cell in a row like i can acces the word in a document for instance with the following code:

For Each Item In ActiveDocument.Words
so like that. How could i do this?

Thanks in advance!

Bob

MaximS
01-05-2010, 10:01 AM
I don't know what you exactly need but you can do similar thing in Excel by using following code:


For Each Cell In ActiveWorksheet.Range("A1:A100")

'do your stuff

Next Cell

VinoBob
01-05-2010, 11:12 AM
I don't know what you exactly need but you can do similar thing in Excel by using following code:


For Each Cell In ActiveWorksheet.Range("A1:A100")

'do your stuff

Next Cell



Nah..:( I tried the following and it doesnt work.


For Each Cell In ActiveWorksheet.Range("A1:A4")
CBX_Names.AddItem (Cell)
Next

The editor says that the variable( Cell ) isnt defined.:dunno

lucas
01-05-2010, 11:50 AM
Dim cell as range:

Private Sub UserForm_Initialize()
Dim cell As Range
For Each cell In Sheets("Sheet2").Range("A1:A4")
CBX_Names.AddItem cell
Next
End Sub

see attached example.

VinoBob
01-05-2010, 01:27 PM
Thanks guys!

I think i know now what was the problem, I had an Option explicit statement in the global declarations section, what i didnt noticed. Now it works without explicitly declaring a Cell object.

lucas
01-05-2010, 01:30 PM
Always leave option explicit in at the top of each module so you will be notified of just such omissions as failure to delcare variables.