VBA table row relative reference

John BX

New Member
Joined
Dec 22, 2014
Messages
36
My code below works ok, as long as the table is at row 1 on the sheet. I would like it to work with the table placed anywhere not just row 1.
the problem seems to be with rngFound.row which returns the row number on the sheet, not the table.

Can somebody help?
Thanks

CompanyNameGradeRate1Rate2
Company_APayGrade011020
Company_APayGrade021525
Company_ZPayGrade012025
Company_ZPayGrade022530

<tbody>
</tbody>

Code:
Sub getRate()


    Dim rngFound As Range
    Dim strFirst As String
    Dim strCompany As String
    Dim strGrade As String


    strCompany = "Company_Z"
    strGrade = "PayGrade02"


    Set rngFound = Range("tblRates[CompanyName]").Find(strCompany, , xlValues, xlWhole)
    If Not rngFound Is Nothing Then
        strFirst = rngFound.Address
        Do
            Debug.Print Range("tblRates[Grade]")(rngFound.Row - 1).Value
            Debug.Print rngFound.Row - 1
            If Range("tblRates[Grade]")(rngFound.Row - 1) = strGrade Then
            
                'Found
                MsgBox "Found a match at row: " & rngFound.Row & Chr(10) & _
                       "Rate 1: " & Range("tblRates[Rate1]")(rngFound.Row - 1) & Chr(10) & _
                       "Rate 2: " & Range("tblRates[Rate2]")(rngFound.Row - 1)
            End If
            Set rngFound = Range("tblRates[CompanyName]").Find(strCompany, rngFound, xlValues, xlWhole)
        Loop While rngFound.Address <> strFirst
    Else
        MsgBox "Rate not found"
    End If


    Set rngFound = Nothing


End Sub
 
Last edited:

Some videos you may like

Excel Facts

How to create a cell-sized chart?
Tiny charts, called Sparklines, were added to Excel 2010. Look for Sparklines on the Insert tab.

RoryA

MrExcel MVP, Moderator
Joined
May 2, 2008
Messages
36,067
Office Version
  1. 365
  2. 2019
  3. 2016
  4. 2010
Platform
  1. Windows
  2. MacOS
Subtract Range("tblRates").Row rather than 1.
 

John BX

New Member
Joined
Dec 22, 2014
Messages
36
Thanks for replying.
I replaced all the 1s with Range("tblRates").row but it now returns incorrect values for Rate1 and Rate2. Is there something else I need to change?
 

RoryA

MrExcel MVP, Moderator
Joined
May 2, 2008
Messages
36,067
Office Version
  1. 365
  2. 2019
  3. 2016
  4. 2010
Platform
  1. Windows
  2. MacOS
Sorry - forgot about the header row being skipped in a simple reference. You need:

Code:
Range("tblRates[#All]").Row
 

Watch MrExcel Video

Forum statistics

Threads
1,122,518
Messages
5,596,621
Members
414,082
Latest member
sasmita

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
Top