Macro for checking if a value exists in a different workbook and adding it if it is missing

Sushajith

New Member
Joined
Aug 19, 2013
Messages
2
Hi there. I would like to have a macro designed. I have 2 workbooks, one having latest information and the other having the information in database already. both workbooks have 26 columns each. I would like to check if a value that exists in Column A of the "latest-information" workbook also exists in the "already-in-database" workbook. If so, I would like to update all 26 columns of that row in the "already-in-database" workbook with information from the "latest-information" workbook. If the value does not exist, I would like to add all 26 fields in that row into a new sheet on the "already-in-database" workbook. Please help me with this query!
 
Yes the code above searches for existing values to update them and if not found then add new data at the bottom. if you want only to add all new data to the bottom and not overwrite then the replace this piece of code
Code:
   Do While rNew.Value <> vbNullString
        'go through each row until empty row
        Set rOutp = wbDB.ActiveSheet.Columns("A").Find(rNew.Value, _
                        searchdirection:=xlNext)
        If rOutp Is Nothing Then        ' not found, so add
            ' find bottom row
            Set rOutp = wbDB.ActiveSheet.Columns("A").Find(what:="*", _
                        after:=Cells(1, 1), _
                        searchdirection:=xlPrevious).Offset(1, 0)
        End If
        ' set values to row of new data
        rOutp.Resize(1, rNew.CurrentRegion.Columns.Count).Value = _
                rNew.Resize(1, rNew.CurrentRegion.Columns.Count).Value
        
        Set rNew = rNew.Offset(1, 0) ' one row down
    Loop

with
Code:
   Do While rNew.Value <> vbNullString
        ' find bottom row
        Set rOutp = wbDB.ActiveSheet.Columns("A").Find(what:="*", _
                        after:=Cells(1, 1), _
                        searchdirection:=xlPrevious).Offset(1, 0)
        ' set values to row of new data
        rOutp.Resize(1, rNew.CurrentRegion.Columns.Count).Value = _
                rNew.Resize(1, rNew.CurrentRegion.Columns.Count).Value
        
        Set rNew = rNew.Offset(1, 0) ' one row down
    Loop
 
Upvote 0

Excel Facts

Copy formula down without changing references
If you have =SUM(F2:F49) in F50; type Alt+' in F51 to copy =SUM(F2:F49) to F51, leaving the formula in edit mode. Change SUM to COUNT.
Yes the code above searches for existing values to update them and if not found then add new data at the bottom. if you want only to add all new data to the bottom and not overwrite then the replace this piece of code

And if I need to add only missing entries, without updating existing one?

Thanks sijpie.
 
Upvote 0
Code:
   Do While rNew.Value <> vbNullString
        'go through each row until empty row
        Set rOutp = wbDB.ActiveSheet.Columns("A").Find(rNew.Value, _
                        searchdirection:=xlNext)
        If rOutp Is Nothing Then        ' not found, so add
            ' find bottom row
            Set rOutp = wbDB.ActiveSheet.Columns("A").Find(what:="*", _
                        after:=Cells(1, 1), _
                        searchdirection:=xlPrevious).Offset(1, 0)
            ' set values to row of new data
            rOutp.Resize(1, rNew.CurrentRegion.Columns.Count).Value = _
                rNew.Resize(1, rNew.CurrentRegion.Columns.Count).Value
        End If


        
        Set rNew = rNew.Offset(1, 0) ' one row down
    Loop
 
Upvote 0

Forum statistics

Threads
1,216,105
Messages
6,128,859
Members
449,472
Latest member
ebc9

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