PDA

View Full Version : Find corresponding value in database, the copy whole row to update it.



Leo7
10-11-2018, 06:43 AM
Hi there!

I am trying to set up a macro that allows for an easier updating of our database.
Our workbooks have two sheets: one is the DATABASE, which mainly has the identifier for every record in column A, and then metadata about the record that eventually needs to be queried or updated in the following columns (B:Q). In the other sheet I made a QUERY table: it has the exact same structure so as to look up records in the database. You type the records you are interested in in Column A and then everything in B:Q is populated with the corresponding data by VLOOKUP formulas that search the DATABASE.
The idea is that in order to update the database, you could get only the records you want to edit in the QUERY sheet, edit them in place, and then have a macro copy back the whole row to it's corresponding row in the database.

I've searched the web extensively and I have found a simple script that I have been able to adapt mostly successfully. The only problem is that the original script was meant to copy back only a single value next to the identifier - it used Offset (0,1) -, and I want to copy the whole row. I understand this should be fairly simple, but since I am only a VBA tourist, I can't get it to work. I understand everything's fine up until the last instruction (which would be handling the "Paste" part). I think I've managed to get the first part of the instruction right (where to copy to, that would be B:Q in the DATABASE sheet for every corresponding row), but I don't know how to properly express where to copy from (the part after the equal sign, which I mean to indicate every row in B:Q from the QUERY sheet).

Here's the code. Any help will be greatly appreciated!


Sub Button2_Click()
Dim lastDatabase_Rw As Long, lastQuery_Rw As Long
Dim c As Range, rangeDB As Range
'Determine Last Row in rangeDB and rangeQUERY Columns
lastDatabase_Rw = Worksheets("DATABASE").Range("A" & Rows.Count).End(xlUp).Row
lastQuery_Rw = Worksheets("QUERY").Range("A" & Rows.Count).End(xlUp).Row
'Loop through rangeDB Range searching for values in rangeQUERY Range
'Copy Row for each match
For Each rangeDB In Worksheets("DATABASE").Range("A2:A" & lastDatabase_Rw)
With Worksheets("QUERY").Range("A2:A" & lastQuery_Rw)
Set c = .Find(rangeDB, lookat:=xlWhole)
If Not c Is Nothing Then
Worksheets("DATABASE").Range("B" & rangeDB.Row & ":Q" & rangeDB.Row).Value = Worksheets("QUERY").Range(c.Offset(0, 1).Column & c.Offset(0, 1).Row & ":" & c.Offset(0, 16).Column & c.Offset(0, 16).Row).Value
End If
End With
Next
End Sub

All the best,
Leo.

mancubus
10-12-2018, 01:02 AM
welcome to the forum.


by taking into account item 3 in my signature, try changing

Worksheets("DATABASE").Range("B" & rangeDB.Row & ":Q" & rangeDB.Row).Value = Worksheets("QUERY").Range(c.Offset(0, 1).Column & c.Offset(0, 1).Row & ":" & c.Offset(0, 16).Column & c.Offset(0, 16).Row).Value

to

rangeDB.Offset(0, 1).Resize(0, 16).Value = c.Offset(0, 1).Resize(0, 16).Value

or omit 0's

rangeDB.Offset(, 1).Resize(, 16).Value = c.Offset(, 1).Resize(, 16).Value