How scan specific column in each row?

G

Guest

Someone gave me the below code but it scans every cell.
It was useful but now I need to look in specific columns, how do I do that?
I'm sure this must be a pretty basic question.

Dim rCell As Range
Dim rRng As Range
With Worksheets("zKeyWordCount")
Set rRng = .Range("c1", .Cells(.Rows.Count, "C").End(xlUp))
End With
For Each rCell In rRng.Cells
....
Next
 
G

Guest

If you drop .Cells from your If statement:

Dim rCell As Range
Dim rRng As Range
With Worksheets("zKeyWordCount")
Set rRng = .Range("c1", .Cells(.Rows.Count, "C").End(xlUp))
End With
For Each rCell In rRng 'Drop .Cells
....
Next

Then on my computer it only checks the cells in column "C"
 
D

Dave Peterson

Dim rCell As Range
dim rCell2 as range
Dim rRng As Range
With Worksheets("zKeyWordCount")
Set rRng = .Range("c1", .Cells(.Rows.Count, "C").End(xlUp))
End With
For Each rCell In rRng.Cells
'to look at all the cells in that row...
for each rcell2 in rcell.entirerow.cells
'...
next rcell2
next rcell

======
But you could use other things to look at a smaller set of cells

Dim rCell As Range
Dim rRng As Range
With Worksheets("zKeyWordCount")
Set rRng = .Range("c1", .Cells(.Rows.Count, "C").End(xlUp))
End With
For Each rCell In rRng.Cells
if rcell.offset(0,-2).value = "hi from column A" then
'do something
end if
if rcell.offset(0,3).value = "hi from column F" then
'do something else
end if
next rcell

Since you're looping through the cells in column C, you can use rcell.offset()
to go in either direction.
 

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

Top