VBA Code to Find Row Number and Copy it to Adjacent Cell

theteerex

Board Regular
Joined
Mar 2, 2018
Messages
102
With two sheets, a Feeder sheet and a Database, I have two columns of data I would like matched.
On the Feeder, all data is in rows:
VALUE 1 -Location Time Cost Duration
VALUE 2 -Location Time Cost Duration
VALUE 3
-Location Time Cost Duration
VALUE 4
-Location Time Cost Duration

On Database, all data is in columns:
VALUE 1 Location
VALUE 1 Time
VALUE 1 Cost
VALUE 1 Duration
VALUE 2 Location
VALUE 2 Time
VALUE 2 Cost
VALUE 2 Duration
VALUE 3 Location
VALUE 3 Time
VALUE 3 Cost
VALUE 3 Duration

So I can run pivot table.

I would like my code to find the first instance of each value(a text string) in the Database and copy the row number in the adjacent cell on the Feeder sheet.
So my data would end up looking like this on the Feeder sheet:
2 VALUE 1 -Location Time Cost Duration
6 VALUE 2 -Location Time Cost Duration
10 VALUE 3
-Location Time Cost Duration
14 VALUE 4
-Location Time Cost Duration

This way, a user can always see that their records have been correctly created in the Database without having to run a search. It also serves as an error checker.
I would like this to run automatically.
Any suggestions?

Thanks in advance!
 
Last edited:
Try:
Code:
Sub FindRow()
    Application.ScreenUpdating = False
    Dim LastRow As Long
    LastRow = Sheets("Feeder").Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
    Dim rng As Range
    Dim foundRng As Range
    For Each rng In Sheets("Feeder").Range("B1:B" & LastRow)
        Set foundRng = Sheets("Database").Range("A:A").Find(rng, LookIn:=xlValues, lookat:=xlWhole)
        If Not foundRng Is Nothing Then
            rng.Offset(0, -1) = foundRng.Row
        End If
    Next rng
    Application.ScreenUpdating = True
End Sub
This code worked perfectly and drama free!
Thank you so much!
 
Upvote 0

Excel Facts

How can you turn a range sideways?
Copy the range. Select a blank cell. Right-click, Paste Special, then choose Transpose.

Forum statistics

Threads
1,223,911
Messages
6,175,323
Members
452,635
Latest member
laura12345

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