Please take a look at this answer. It explains how to select cells and their offsets.
Apply what you've learnt from the above answer inside the for each loop where the comment says match found do something
. You can remove that comment or just add your code below it in between the If
and End If
which indicate that there is a match.
Sub testr1()
Dim myRange As Range
' change this to any range you like to check
Set myRange = Range("A1:A100")
Dim searchTerm As String
' specify your search term
searchTerm = "1234"
Dim cell As Range
' cell will be each cell in your range
For Each cell In myRange
' checks whether the cell matches the searchTerm
If StrComp(cell, searchTerm, vbTextCompare) = 0 Then
' match found do something
End If
Next
End Sub
This code iterates through all cells in the myRange
variable. In the above example the range is set to A1
to A100
so the cell
variable inside the loop will be the A1
for the first iteration, A2
for second, and so on until A100
.
The StrComp()
function compares each cell's value to your searchTerm
which is 1234
in the example.
match found do something
is where I am hoping you will apply the logic from the answer I provided the link to above.
your original but revised code
Sub testr1()
Dim vCell As Range
Dim otherrow As Long
otherrow = 1
Do
Set vCell = Sheets("Sheet1").Cells(otherrow, 1)
If vCell = 1234 Then
If Range("B5") = "B" Then
Cells(otherrow, 2).Select
End If
End If
otherrow = otherrow + 1
Loop Until IsEmpty(vCell)
End Sub
Get rid of the else statement. The Exit Do
exits your loop if 1234
is not found. So in case if the first cell doesn't equal 1234
you exiting the loop, and it doesnt move to the second row.
Also, you need to add a Boolean statement do your loop to prevent an infinite loop. I have added Loop Until isEmpty(vCell)
to let the loop know that once there is an empty cell to terminate the loop. There are other, better approaches but if you do not want to modify your original code too much then this should be enough to avoid infinite looping.
What is your code actually doing?
I keep asking myself what you are trying to achieve with your code and I can't seem to give a good reason. Your revised code (the above one) will iterate through all the cells in column A until it finds an empty one. It will look for a match to your 1234
(which should really be used as a string not a stand-alone number - consider wrapping it in double quotes). Once a match is found it will check whether column B
on the corresponding row has a value of B
in it. If it does then it will select that cell.
Where the logic fails...
What is the point of iterating through the entire column since only the last 1234 and it's corresponding B
will be selected? It makes no sense to me as it is. Try to better explain what you are trying to achieve.
Solution
Based on the latest comment I have edited the code to meet criteria
Sub testr1()
Dim vCell As Range
Dim otherrow As Long
otherrow = 1
Do
Set vCell = Sheets("Sheet1").Cells(otherrow, 1)
If StrComp(vCell, "1234", vbTextCompare) = 0 Then
If StrComp(vCell.Offset(0, 1), "B", vbTextCompare) = 0 Then
vCell.Offset(0, 1).Select
' if all criterias are met then the cell will be highlighted in red
vCell.Offset(0,1).Interior.Color = RGB(255, 0, 0)
End If
End If
otherrow = otherrow + 1
Loop Until IsEmpty(vCell)
End Sub
You need to use .Offset(0,1)
property of the Range object (vCell
) to select the corresponding cell ( one to the right )