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:

Excel Facts

Back into an answer in Excel
Use Data, What-If Analysis, Goal Seek to find the correct input cell value to reach a desired result
Subtract Range("tblRates").Row rather than 1.
 
Upvote 0
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?
 
Upvote 0
Sorry - forgot about the header row being skipped in a simple reference. You need:

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

Forum statistics

Threads
1,214,832
Messages
6,121,849
Members
449,051
Latest member
excelquestion515

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