Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

If no matching row for a searched value found

I have some problems defining that check. It’s only a part of whole code with Do Untilloop.

If any row in my sheet matches searched value define FindRow as "1".

If matching row is found then FindRow = that row number.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel


With ThisWorkbook.Sheets("TKSLIST").Range("A:A")
    FindRow = 1
    strTKSname = ws.Cells(i + 1, 11).Text
    
    If .Find(What:=strTKSname, LookAt:=xlPart, LookIn:=xlValues, MatchCase:=False).Row Is Nothing Then
    FindRow = 1
    Else
    FindRow = .Find(What:=strTKSname, LookAt:=xlPart, LookIn:=xlValues, MatchCase:=False).Row
    End If
End With

I’m missing there something. Everytime when my searched value is not found I’m getting errors.

>Solution :

  1. Declare your variables properly

  2. Get the range wher something was found as FoundAt and compare that against Nothing

  3. Remove else part because FindRow = 1 was already set as initial value.

With ThisWorkbook.Sheets("TKSLIST").Range("A:A")
    Dim FindRow As Long
    FindRow = 1

    Dim strTKSname As String
    strTKSname = ws.Cells(i + 1, 11).Text
    
    Dim FoundAt As Range
    Set FoundAt = .Find(What:=strTKSname, LookAt:=xlPart, LookIn:=xlValues, MatchCase:=False)

    If Not FoundAt Is Nothing Then
        FindRow = FoundAt.Row
    End If 'no else needed because initialized as `FindRow = 1`
End With
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading