Excel Table Functions: a Find returns the sheet row rather than the table row

rtemen

New Member
Joined
Sep 23, 2013
Messages
36
I have a small program that is trying to find a cell containing a certain string and return the Row in the table where it is found.

Here is my program:

Code:
Private Sub test()
    'Routine to move data to table from Molding Table
    'Check to not overwrite current records but add new ones.
    
    Dim summObj As ListObject
    Dim I, X As Integer
    Dim foundrow As Integer
    
    ' Get the table reference
    Set summObj = Worksheets("Summary").ListObjects("SummaryTable")
    With summObj
    
    foundrow = .ListColumns("ID").Range.Find("1,3").Row
    foundrow = .ListColumns("ID").DataBodyRange.Find("1,3").Row
    End With


End Sub

Two questions:
1. Both of my commands to find the row return the spreadsheet row and not the table row.
2. If the string is not in the table row, it raises the RunTime Error '91'. What is wrong?

Rich
 

Excel Facts

Lock one reference in a formula
Need 1 part of a formula to always point to the same range? use $ signs: $V$2:$Z$99 will always point to V2:Z99, even after copying
You are going to have to subtract the header row of the table from the foundrow.

When you search for a non-existent string it will return an error, you need to mitigate that.

Dim foundrow as variant
foundrow = .ListColumns("ID").Range.Find("1,3").Row
if Iserror(foundrow) = true then foundrow = 0
 
Upvote 0
As Jeffrey pointed out, you have to do a subtraction to get the row number within the table and you also need to provide an error trap should you accidentally search for an ID that does not exist. This is how I would write such a macro...
Code:
[table="width: 500"]
[tr]
	[td]Private Sub test()

  Dim summObj As ListObject
  Dim I As Long, X As Long
  Dim foundrow As Long

  Set summObj = Worksheets("Summary").ListObjects("SummaryTable")
  On Error Resume Next
  foundrow = summObj.ListColumns("ID").Range.Find("4,5").Row - summObj.DataBodyRange.Row + 1
  On Error GoTo 0

End Sub[/td]
[/tr]
[/table]

Note1: If you search for text that is not in the table, foundrow will be assigned a value of 0.

Note2: I changed your Integer declarations to Long data types. There is nothing to be gained by using Integer and you avoid potential problems when you use Long instead of Integer.
 
Last edited:
Upvote 0
Could you not use the Match function to give the ListRow directly ?
Code:
    On Error Resume Next
    foundrow = Application.WorksheetFunction.Match("1,3", .ListColumns("ID").DataBodyRange, 0)
        If foundrow = 0 Then
            MsgBox " Nothing found"
        Else
            MsgBox "foundrow = .ListRows(" & foundrow & ")"
        End If
    On Error GoTo 0
 
Upvote 0
Hi there.
I do not know how or where to ask this question, so I will try to ask it here. (Or, please tell me where to ask this question)
When working with a ListObject, both the Microsoft info as well as the interactive syntax checker shows that there is a Find for the ListObject.
So, while you can select cells in a table using the TABLE rows and columns, update cells using the TABLE rows and columns, etc, the Find gives the spreadsheet row and column rather than the TABLE's????
What kind of logic is this?
So, first, am I missing some sort of high level assignment or setting or something that will make the FIND answer in the Table's lingo?
If the FIND that is attached to the various ListObject objects does not work, then why is it there???
Just frustrated with the hours of Googling to find help and nothing helps or describes why it doesn't work and how you are supposed to handle it.
Thanks,
Rich
 
Upvote 0
Apparrently .Find always gives the sheet address
I found this type of work around, maybe it's adaptable
Code:
    Set oLo = Sheet1.ListObjects(1)
    
    If Not Intersect(oLo.DataBodyRange, ActiveCell) Is Nothing Then
        MsgBox "oLo column    " & Range(oLo.DataBodyRange(1), ActiveCell).Columns.Count & vbLf & _
               "oLo row           " & Range(oLo.DataBodyRange(1), ActiveCell).Rows.Count
    End If
 
Upvote 0

Forum statistics

Threads
1,223,889
Messages
6,175,226
Members
452,620
Latest member
dsubash

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top