VBA inquiry to return row number if match exists

rn119

New Member
Joined
Feb 27, 2013
Messages
49
I need the exact row number dynamic values (based on user input) might potentially exist in a macro. For example, if cell J2, K2, L2 values match table defined values in columns A, C and F (i.e., J2 has abc, K2 has 123, and L2 has xyz ...and it is only considered a match if those values all work in tandem in any row for columns A/C/F ). I can do this with an Index/IF function but I need the ability to throw a validation message to the user advising of the row number the user defined values exist in. Is there a way? Also if no match exists, then I need a validation message citing that Nothing exists.
 
Last edited:

Excel Facts

Create a chart in one keystroke
Select the data and press Alt+F1 to insert a default chart. You can change the default chart to any chart type
.... is only considered a match if those values all work in tandem in any row for columns A/C/F )
Can you elaborate as to what that means ?

Where on the sheet is the table located ?

Is there any possibility that more than only one row will match, if so, then what ?
 
Upvote 0
So let's say Sheet 1 has the table and looks like this from Columns A1 to G6 in this example...

Item IDItem NameContract IDContract NameContract DateLoc IDLoc Name
10980036Stationery837RN TEST ABC4/2/2014503101LOCATION ABC
10980037Furniture837RN TEST ABC7/1/2013503102LOCATION 123
10980038Supplies1334RN TEST 1237/1/2017503103LOCATION XYZ
10180052Software1334RN TEST 1239/1/2016503104LOCATION 456
10180044Miscellaneous1336RN TEST XYZ7/1/2015503105LOCATION 789

<tbody>
</tbody>

User defined values will fall in Sheet 1 in the following format (Columns A1 to C6 in this example).

Contract IDItem IDLoc ID
133910980036503101
133910980037503199
145510980038503105
150010180052503777
133610180044503105

<tbody>
</tbody>

So in this example the user defined values (the last row here) match the table's last row based on Contract ID, Item ID and Location ID variables. So once the macro is invoked, it will tell the user of potential duplicates or if no duplicates throw a message that no duplicates were found.

In regards to your last inquiry, yes it is possible multiple rows can be a match. So then in that case, is it possible to have ALL row numbers called out in the validation message?
 
Upvote 0
Assuming the J,K,L columns are that last table,
see if this is of any use, it presents an input box so you can just click the contract ID in column J that you want.
Code:
Sub MrE_rn119()
    
    Dim contractID As Range
    Dim rng As Range, fndID As Range
    Dim firstAddress As String, msg As String

    Set contractID = Application.InputBox("Select the Contract ID to look for" & vbLf & _
                     "by clicking a cell in column J ", "Find matching rows", Type:=8)
    
    If contractID.Count <> 1 Then Exit Sub
    If contractID.Column <> 10 Then Exit Sub
    
    With Sheets("Sheet1")
        Set rng = .Range("C2", .Range("C" & Rows.Count).End(xlUp))
    End With
    
    With rng
        Set fndID = .Find(What:=contractID, LookIn:=xlValues, _
                         LookAt:=xlWhole, SearchOrder:=xlByRows, _
                         SearchDirection:=xlNext, MatchCase:=False)
                        
        If Not fndID Is Nothing Then
            firstAddress = fndID.Address
            Do
                If fndID.Offset(, -2).Value = contractID.Offset(, 1) And _
                   fndID.Offset(, 3).Value = contractID.Offset(, 2).Value Then
                    msg = msg & vbLf & fndID.Row
                End If
                Set fndID = .FindNext(fndID)
            Loop While Not fndID Is Nothing And fndID.Address <> firstAddress
        End If
    End With
    
    If Len(msg) = 0 Then
        MsgBox "No matching rows found for Contract ID " & contractID
    Else
        MsgBox "Contract ID match found on row(s) " & msg
    End If
   
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,393
Messages
6,119,261
Members
448,880
Latest member
aveternik

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