简体   繁体   中英

Excel-VBA Find Function does not consider first row of column

I have been searching all around the web for an easy way to find the row and column numbers of certain strings or values. To my surprise, I have not found what I was hoping for, so I decided to code my own function. As I am quite a beginner, I am encountering some problems.

So in this example, my aim is to code a function which I can always use in any VBA sub in order to find row or column number. For now, I have coded the following function to find a column:

Function Find(SearchRange As String, Word As String) As Long
Dim found As Range

Set found = ActiveSheet.Columns(SearchRange).Find(what:=Word, LookIn:=xlValues, lookat:=xlWhole)

If Not found Is Nothing Then
Find = found.Row
End If
End Function

And then I coded the following sub to use the function:

Sub FindTest()
Cells(1, 2) = Find("A", "C")
End Sub

So if I have the following table (column A):

A
B
C
D
E

The result I should be getting is "3". This does work and I am happy up this point. However, if I input another "C" in the table, eg

C
B
C
D
E

The result I am getting is still "3" rather than "1". If I change the table to

C
B
A
D
E

The result I am getting is "1".

So it looks like the first row is not considered once the value I am looking for comes up more than once in the range that I am searching in.

I have searched the web about this issue and unfortunately I was not able to find a definite answer. However, some people suggested to make the search start in the first cell by having it start at the last cell (ie the search will jump directly to the start again after "passing" the last cell). The suggested code was to add

After:=.Cells(.Cells.Count)

in to the "Find" definition, however if I update to

Set found = ActiveSheet.Columns(SearchRange).Find(what:=Word, After:=.Cells(.Cells.Count), LookIn:=xlValues, lookat:=xlWhole)

I get "Compile Error: Invalid or unqualified reference".

I am really getting desperate at this stage. Is there anyone who can help me on this one?

Thanks a lot!

Try this:

Sub findstr()
    Dim rng As Range
    Set rng = ActiveSheet.UsedRange.Find("C", , xlValues, xlWhole)
    MsgBox rng.Row          '--->this will give you row
    MsgBox rng.Column       '--->this will give you column
End Sub

EDIT: Made changes in your Find function where you were trying to use After:=.Cells(.Cells.Count)

Function Find(SearchRange As String, Word As String) As Long
    Dim found As Range

    With Sheets("Sheet1").Range(SearchRange & ":" & SearchRange)
        Set found = ActiveSheet.Columns(SearchRange).Find(what:=Word, After:=.Cells(.Cells.Count), LookIn:=xlValues, lookat:=xlWhole)
    End With

    If Not found Is Nothing Then
        Find = found.Row
    End If

End Function

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM