Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to get a 'Cell' object in excel VBA

Tags:

excel

vba

So i was using a for each loop to go through a bunch of rows. I ran into a problem where i was deleting rows and it was causing a row to get skipped so i have changed to a do while loop.

The problem i am having is trying to get a 'cell' object. Before when i had:

For Each C In Worksheets("Blah").Range("A2:A" & lastRow).Cells

I could do things like

C.Offset(1, 0)
C.Value = "Troy"

Etc. I tried using:

C = Worksheets("Blah").Cells(iRow, 2)

but that just gives C the value of the cell. How do i get the actual cell object?

Thanks

like image 959
Troy Cosentino Avatar asked Aug 13 '12 21:08

Troy Cosentino


1 Answers

To get an object reference and not the value itself you need to use 'Set'

Set C = Worksheets("Blah").Cells(iRow, 2)
like image 172
Валерий Зайцев Avatar answered Sep 21 '22 20:09

Валерий Зайцев