Consulting

Results 1 to 2 of 2

Thread: Find corresponding value in database, the copy whole row to update it.

  1. #1

    Find corresponding value in database, the copy whole row to update it.

    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.

  2. #2
    VBAX Guru mancubus's Avatar
    Joined
    Dec 2010
    Location
    Ride the wind to the sun
    Posts
    2,590
    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
    PLS DO NOT PM; OPEN A THREAD INSTEAD!!!

    1) Posting Code
    [CODE]PasteYourCodeHere[/CODE]
    (or paste your code, select it, click # button)

    2) Uploading File(s)
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) (multiple files can be selected while holding Ctrl key) / Upload Files / Done
    Replace company specific / sensitive / confidential data. Include so many rows and sheets etc in the uploaded workbook to enable the helpers visualize the data and table structure. Helpers do not need the entire workbook.

    3) Testing the Codes
    always back up your files before testing the codes.

    4) Marking the Thread as Solved
    from Thread Tools (on the top right corner, above the first message)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •