1

I just want to check whether there is anything wrong with my code. My code is trying to look for this value 1234 in column A and once it found it, it will select the value besides it if the condition was met. If there isn't 1234, then it will do nothing. Unfortunately, it does nothing even if 1234 was in column A. When I remove the Else statement, it was able to run as per normal. May I know what is wrong with it?

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
        Else
        'Do nothing if 1234 is N.A.
        Exit Do
        End If
        otherrow = otherrow + 1
   Loop

End Sub
Community
  • 1
  • 1
user1204868
  • 606
  • 6
  • 15
  • 31
  • 1
    Why loop? You can either use `.AutoFilter` [EXAMPLE](http://stackoverflow.com/questions/11631363/how-to-copy-a-line-in-excel-using-a-specific-word-and-pasting-to-another-excel-s) or `.Find/.FindNext` [EXAMPLE](http://www.siddharthrout.com/2011/07/14/find-and-findnext-in-excel-vba/) – Siddharth Rout Sep 27 '13 at 08:53
  • @SiddharthRout I need to locate 1234 then do a selection beside it or a couple of columns after it, I thought probably this is the way to do it. I am not so sure on how to do autofilter or .find – user1204868 Sep 27 '13 at 09:00
  • `I am not so sure on how to do autofilter or .find` I have served you the links on a plate in my previous comment. You need to go through them in detail and understand them. If you are a beginner then you cannot do that in approx 7 mins :) – Siddharth Rout Sep 27 '13 at 09:30
  • @SiddharthRout sorry, I left out the 'EXAMPLE ' totally.. will look at them now – user1204868 Sep 27 '13 at 09:32

2 Answers2

2

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 )

Community
  • 1
  • 1
  • is it possible that I don't make use of the search to locate the cell with 1234? It looks complicated when I try to locate the cell besides the value of 1234. From my code, I make use of the do loop to try to do both at the same time – user1204868 Sep 27 '13 at 09:28
  • @user1204868 sorry but I have a difficult time understanding you. Can you make more effort to explain what you mean? –  Sep 27 '13 at 09:31
  • for example, I need to look for 1234 in Col A. Once I found it, I will have to do a selection beside 1234, it can be Col B for illustration. In other words, if 1234 is at A12, then I would like to select B12 and so on.. I hope I have cleared up the doubts – user1204868 Sep 27 '13 at 09:35
  • Thank you so much for your effort! :) I am not able to use the revised still as my file do have empty columns in between. Luckily for your statement on Loop Until IsEmpty(vCell), it gave me an idea and I have found a way to make it work! – user1204868 Sep 27 '13 at 10:18
0

Special thanks to @mehow on giving me an idea to make my code work

Sub testr1()
Dim vCell As Range
Dim otherrow As Long

otherrow = 1

    Do While otherrow <= Rows.Count

        Set vCell = Sheets("Sheet1").Cells(otherrow, 1)

         If vCell = 1234 Then
            If Range("B5") = "B" Then
                Cells(otherrow, 2).Select
            End If
        Exit Do
        End If
        otherrow = otherrow + 1

        Loop

End Sub
user1204868
  • 606
  • 6
  • 15
  • 31