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:

Excel Facts

Excel Wisdom
Using a mouse in Excel is the work equivalent of wearing a lanyard when you first get to college
On the Feeder sheet you say
I have two columns of data
Is "Value 1" in column A and "Location Time Cost Duration" in column B?
On the database sheet you say
all data is in columns
Is "Value 1" in column A and "Location" in column B? Please clarify what data is in which columns on each sheet.
 
Last edited:
Upvote 0
Try this out..... assuming the following

Your feeder sheet has data like so

LocationTimeCostDuration
A
B
C
D
E

<tbody>
</tbody>

Your database has data like so

AAAABBBBCCCCDDDDEEEE
House123School456Work789Tent101112Stuff131415

<colgroup><col width="64" span="20" style="width:48pt"> </colgroup><tbody>
</tbody>

and all data begins at A1

Code:
Sub Stuff()
Dim fr, dr As Range
Dim lCol, lRow, counter As Integer
Dim fd, db As Worksheet


Set fd = Sheets("Feeder")
Set db = Sheets("Database")
lRow = fd.Cells(Rows.Count, 1).End(xlUp).Row
lCol = db.Cells(1, Columns.Count).End(xlToLeft).Column


For Each fr In Range("A2:A" & lRow)
  counter = 1
    For Each dr In db.Range(db.Cells(1, 1), db.Cells(1, lCol))
        If dr.Value = fr.Value Then
            fr.Offset(, counter).Value = dr.Offset(1, 0).Value
            counter = counter + 1
        End If
    Next dr
Next fr
End Sub

To achieve results like this in your feeder sheet

LocationTimeCostDuration
AHouse123
BSchool456
CWork789
DTent101112
EStuff131415

<colgroup><col width="64" span="5" style="width:48pt"> </colgroup><tbody>
</tbody>
 
Upvote 0
Try this out..... assuming the following

Your feeder sheet has data like so

LocationTimeCostDuration
A
B
C
D
E

<tbody>
</tbody>

Your database has data like so

AAAABBBBCCCCDDDDEEEE
House123School456Work789Tent101112Stuff131415

<tbody>
</tbody>
Actually, my Feeder is like this:

BCDE
1Value1123
2Value2456
3Value3789
4Value4101112
5Value5131415


<tbody>
</tbody>
And in my Database, it is like this:

ABCD
1NameCategoryPrice
2Value1Location1
3Value1Time2
4Value1Cost3
5Value2Location4

<tbody>
</tbody>

So I can run pivot table. I want the row numbers to appear in column A of my Feeder sheet.
 
Upvote 0
Actually, my Feeder is like this:

BCDE
1Value1123
2Value2456
3Value3789
4Value4101112
5Value5131415

<tbody>
</tbody>
And in my Database, it is like this:

ABCD
1NameCategoryPrice
2Value1Location1
3Value1Time2
4Value1Cost3
5Value2Location4

<tbody>
</tbody>

So I can run pivot table. I want the row numbers to appear in column A of my Feeder sheet.

Ok so in that case your cell A1 in Feeder would have the value 2 and cell A2 would have 5 and everything in between those blank? and so on?
 
Upvote 0
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
 
Upvote 0
Cross-Posting
While we do not prohibit Cross-Posting on this site, we do ask that you please mention you are doing so and provide links in each of the threads pointing to the other thread (see rule 13 here along with the explanation: Forum Rules).
This way, other members can see what has already been done in regards to a question, and do not waste time working on a question that may already be answered.
 
Upvote 0

Forum statistics

Threads
1,214,833
Messages
6,121,868
Members
449,053
Latest member
Mesh

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