For each this, then that and that

W

wal50

If I drop the "X" part, the below will shade the cells I want. I just can't
get the syntax for adding - ActiveCell.Offset(0, 1) = "X".

I have tried several versions of this with only undesired results.

Any help would be appreciated. Thank in advance
wal50

Sub MarkCells()
Dim Cell As Range
Dim MarkThis As String
MarkThis = Application.InputBox("Enter Value to Mark", Type:=2)
Set rng = Application.InputBox("Enter Range to check -
A1:A20", Type:=8)
rng.Select
For Each Cell In Selection
If Cell.Value = MarkThis Then
Cell.Interior.ColorIndex = 4 And
ActiveCell.Offset(0, 1) = "X"
Next Cell
End Sub
 
D

Dave Peterson

Maybe just changing:

ActiveCell.Offset(0, 1) = "X"
to
Cell.Offset(0, 1).Value = "X"

Since you're not activating/selecting the cell when you loop through them.

(I like specifying the property, too.)
 
D

Dave Peterson

Maybe just changing:

ActiveCell.Offset(0, 1) = "X"
to
Cell.Offset(0, 1).Value = "X"

Since you're not activating/selecting the cell when you loop through them.

(I like specifying the property, too.)
 
B

Bernard Liengme

Try this - it worked for me

Sub MarkCells()
Dim Cell As Range
Dim MarkThis As String
MarkThis = Application.InputBox("Enter Value to Mark", Type:=2)
Set rng = Application.InputBox("Enter Range to check - A1: A20 ", Type:=8)
For Each Cell In rng
If Cell.Value = MarkThis Then
Cell.Interior.ColorIndex = 4
Cell.Offset(0, 1) = "X"
End If
Next Cell
End Sub

There is no need to do the Select; it did not change the active cell as you
went through the loop
best wishes
 
B

Bernard Liengme

Try this - it worked for me

Sub MarkCells()
Dim Cell As Range
Dim MarkThis As String
MarkThis = Application.InputBox("Enter Value to Mark", Type:=2)
Set rng = Application.InputBox("Enter Range to check - A1: A20 ", Type:=8)
For Each Cell In rng
If Cell.Value = MarkThis Then
Cell.Interior.ColorIndex = 4
Cell.Offset(0, 1) = "X"
End If
Next Cell
End Sub

There is no need to do the Select; it did not change the active cell as you
went through the loop
best wishes
 

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