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!
 

Excel Facts

How can you turn a range sideways?
Copy the range. Select a blank cell. Right-click, Paste Special, then choose Transpose.
How familiar are you with macros? have you searched for finding copying duplicate date?
 
Upvote 0
I am pretty decent with macros. I have actually done comparing of 2 sets of values on macros. But I have never done comparing 2 sets of values and then extracting data using macros before.
 
Upvote 0
OK, this code should get you going: It is supposed to be in the workbook containing the new data. It will ask you to point out the database workbook to be refreshed (which does not have to be open).

<font face=Courier New><SPAN style="color:#00007F">Option</SPAN> <SPAN style="color:#00007F">Explicit</SPAN><br><br><SPAN style="color:#007F00">'---------------------------------------------</SPAN><br><SPAN style="color:#00007F">Sub</SPAN> UpdateDB()<br><SPAN style="color:#007F00">'</SPAN><br><SPAN style="color:#007F00">' Macro to update the Database book with the _<br>  new values in the current workbook _<br>  The macro will request the name of the _<br>  DB workbook, and open it if not already _<br>  open.</SPAN><br><SPAN style="color:#007F00">'---------------------------------------------</SPAN><br>    <SPAN style="color:#00007F">Dim</SPAN> wbLate <SPAN style="color:#00007F">As</SPAN> Workbook, wbDB <SPAN style="color:#00007F">As</SPAN> Workbook<br>    <SPAN style="color:#00007F">Dim</SPAN> rNew <SPAN style="color:#00007F">As</SPAN> Range, rOutp <SPAN style="color:#00007F">As</SPAN> Range<br>    <SPAN style="color:#00007F">Dim</SPAN> sDBName <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">String</SPAN>, sPath <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">String</SPAN><br>    <SPAN style="color:#00007F">Dim</SPAN> bFl <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Boolean</SPAN><br>    <br>    <SPAN style="color:#007F00">'set screenflikker off</SPAN><br>    Application.ScreenUpdating = <SPAN style="color:#00007F">False</SPAN><br>    <br>    <SPAN style="color:#00007F">Set</SPAN> wbLate = ThisWorkbook<br>    <br>    <SPAN style="color:#007F00">' Get the name of database WB</SPAN><br>    <SPAN style="color:#00007F">With</SPAN> Application.FileDialog(msoFileDialogOpen)<br>        .Title = "Select Database File to be updated"<br>        .Filters.Add "Excel Files", "*.xls*"<br>        .InitialFileName = wbLate.Path<br>        .Show<br>        <SPAN style="color:#00007F">If</SPAN> .SelectedItems.Count = 0 <SPAN style="color:#00007F">Then</SPAN> <SPAN style="color:#00007F">GoTo</SPAN> CleanUp<br>        sPath = .SelectedItems(1)<br>    <SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">With</SPAN><br>    <SPAN style="color:#007F00">' get the file name from path</SPAN><br>    sDBName = GetFName(sPath)<br>    <SPAN style="color:#00007F">On</SPAN> <SPAN style="color:#00007F">Error</SPAN> <SPAN style="color:#00007F">Resume</SPAN> <SPAN style="color:#00007F">Next</SPAN><br>    <SPAN style="color:#007F00">' check if file already open</SPAN><br>    <SPAN style="color:#00007F">Set</SPAN> wbDB = Workbooks(sDBName)<br>    <SPAN style="color:#00007F">On</SPAN> <SPAN style="color:#00007F">Error</SPAN> <SPAN style="color:#00007F">GoTo</SPAN> 0<br>    <SPAN style="color:#00007F">If</SPAN> wbDB <SPAN style="color:#00007F">Is</SPAN> <SPAN style="color:#00007F">Nothing</SPAN> <SPAN style="color:#00007F">Then</SPAN><br>        <SPAN style="color:#007F00">' DB was not open yet</SPAN><br>        bFl = <SPAN style="color:#00007F">True</SPAN><br>        <SPAN style="color:#00007F">Set</SPAN> wbDB = Workbooks.Open(sPath)<br>    <SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">If</SPAN><br>    <br>    <SPAN style="color:#00007F">Set</SPAN> rNew = wbLate.ActiveSheet.Range("A1")<br>    <SPAN style="color:#007F00">' as we are transferring lots of data speed up _<br>      process, by not doing recalcs etc.</SPAN><br>    Application.EnableEvents = <SPAN style="color:#00007F">False</SPAN><br>    <SPAN style="color:#00007F">Do</SPAN> <SPAN style="color:#00007F">While</SPAN> rNew.Value <> vbNullString<br>        <SPAN style="color:#007F00">'go through each row until empty row</SPAN><br>        <SPAN style="color:#00007F">Set</SPAN> rOutp = wbDB.ActiveSheet.Columns("A").Find(rNew.Value, _<br>                        searchdirection:=xlNext)<br>        <SPAN style="color:#00007F">If</SPAN> rOutp <SPAN style="color:#00007F">Is</SPAN> <SPAN style="color:#00007F">Nothing</SPAN> <SPAN style="color:#00007F">Then</SPAN>        <SPAN style="color:#007F00">' not found, so add</SPAN><br>            <SPAN style="color:#007F00">' find bottom row</SPAN><br>            <SPAN style="color:#00007F">Set</SPAN> rOutp = wbDB.ActiveSheet.Columns("A").Find(what:="*", _<br>                        after:=Cells(1, 1), _<br>                        searchdirection:=xlPrevious).Offset(1, 0)<br>        <SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">If</SPAN><br>        <SPAN style="color:#007F00">' set values to row of new data</SPAN><br>        rOutp.Resize(1, rNew.CurrentRegion.Columns.Count).Value = _<br>                rNew.Resize(1, rNew.CurrentRegion.Columns.Count).Value<br>        <br>        <SPAN style="color:#00007F">Set</SPAN> rNew = rNew.Offset(1, 0) <SPAN style="color:#007F00">' one row down</SPAN><br>    <SPAN style="color:#00007F">Loop</SPAN><br>    Application.EnableEvents = <SPAN style="color:#00007F">True</SPAN><br>    <br>    <SPAN style="color:#007F00">'close DB file if it was not open</SPAN><br>    <SPAN style="color:#00007F">If</SPAN> bFl <SPAN style="color:#00007F">Then</SPAN><br>        wbDB.Close savechanges:=<SPAN style="color:#00007F">True</SPAN><br>    <SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">If</SPAN><br>    MsgBox "Successfully updated"<br>CleanUp:<br>    <SPAN style="color:#00007F">Set</SPAN> wbLate = <SPAN style="color:#00007F">Nothing</SPAN><br>    <SPAN style="color:#00007F">Set</SPAN> wbDB = <SPAN style="color:#00007F">Nothing</SPAN><br>    <SPAN style="color:#00007F">Set</SPAN> rNew = <SPAN style="color:#00007F">Nothing</SPAN><br>    <SPAN style="color:#00007F">Set</SPAN> rOutp = <SPAN style="color:#00007F">Nothing</SPAN><br>    Application.ScreenUpdating = <SPAN style="color:#00007F">True</SPAN><br>    <br><SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">Sub</SPAN><br><br><SPAN style="color:#007F00">'--------------------------------------------</SPAN><br><SPAN style="color:#00007F">Function</SPAN> GetFName(sPath <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">String</SPAN>) <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">String</SPAN><br><SPAN style="color:#007F00">'</SPAN><br><SPAN style="color:#007F00">' Function that splits the filename from _<br>  the path passed as parameter. Function _<br>  returns the file name.</SPAN><br><SPAN style="color:#007F00">'--------------------------------------------</SPAN><br>    GetFName = Right(sPath, Len(sPath) - InStrRev(sPath, "\"))<br><SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">Function</SPAN><br></FONT>

If you rather have the macro in the DB workbook because the new data workbooks come from somewhere else, then let me know. Easy rewrite.
 
Upvote 0
I can but not for the next week as I don't have access to a PC. But you can do it yourself.

In the code above change
Set wbLate = ThisWorkbook
To
Set wbDB = ThisWorkbook

And change the two occurrences of
Set wbDB = ...
To
Set wbLate = ...

That should do the trick
 
Upvote 0
I can but not for the next week as I don't have access to a PC. But you can do it yourself.

In the code above change
Set wbLate = ThisWorkbook
To
Set wbDB = ThisWorkbook

And change the two occurrences of
Set wbDB = ...
To
Set wbLate = ...

That should do the trick

Thank's a lot.

I'll give it a try.
 
Upvote 0
I haven't tested it but code should be:

<font face=Calibri><SPAN style="color:#00007F">Option</SPAN> <SPAN style="color:#00007F">Explicit</SPAN><br><br><SPAN style="color:#007F00">'---------------------------------------------</SPAN><br><SPAN style="color:#00007F">Sub</SPAN> UpdateDB()<br><SPAN style="color:#007F00">'</SPAN><br><SPAN style="color:#007F00">' Macro to update the Database book with the _<br>  new values in a workbook to be selected. _<br>  The macro will request the name of the _<br>  workbook, and open it if not already _<br>  open.</SPAN><br><SPAN style="color:#007F00">'---------------------------------------------</SPAN><br>    <SPAN style="color:#00007F">Dim</SPAN> wbLate <SPAN style="color:#00007F">As</SPAN> Workbook, wbDB <SPAN style="color:#00007F">As</SPAN> Workbook<br>    <SPAN style="color:#00007F">Dim</SPAN> rNew <SPAN style="color:#00007F">As</SPAN> Range, rOutp <SPAN style="color:#00007F">As</SPAN> Range<br>    <SPAN style="color:#00007F">Dim</SPAN> sDBName <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">String</SPAN>, sPath <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">String</SPAN><br>    <SPAN style="color:#00007F">Dim</SPAN> bFl <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Boolean</SPAN><br>    <br>    <SPAN style="color:#007F00">'set screenflikker off</SPAN><br>    Application.ScreenUpdating = <SPAN style="color:#00007F">False</SPAN><br>    <br>    <SPAN style="color:#00007F">Set</SPAN> wbDB = ThisWorkbook<br>    <br>    <SPAN style="color:#007F00">' Get the name of data WB</SPAN><br>    <SPAN style="color:#00007F">With</SPAN> Application.FileDialog(msoFileDialogOpen)<br>        .Title = "Select File to update this Databse with"<br>        .Filters.Add "Excel Files", "*.xls*"<br>        .InitialFileName = wbDB.Path<br>        .Show<br>        <SPAN style="color:#00007F">If</SPAN> .SelectedItems.Count = 0 <SPAN style="color:#00007F">Then</SPAN> <SPAN style="color:#00007F">GoTo</SPAN> CleanUp<br>        sPath = .SelectedItems(1)<br>    <SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">With</SPAN><br>    <SPAN style="color:#007F00">' get the file name from path</SPAN><br>    sDBName = GetFName(sPath)<br>    <SPAN style="color:#00007F">On</SPAN> <SPAN style="color:#00007F">Error</SPAN> <SPAN style="color:#00007F">Resume</SPAN> <SPAN style="color:#00007F">Next</SPAN><br>    <SPAN style="color:#007F00">' check if file already open</SPAN><br>    <SPAN style="color:#00007F">Set</SPAN> wbLate = Workbooks(sDBName)<br>    <SPAN style="color:#00007F">On</SPAN> <SPAN style="color:#00007F">Error</SPAN> <SPAN style="color:#00007F">GoTo</SPAN> 0<br>    <SPAN style="color:#00007F">If</SPAN> wbLate <SPAN style="color:#00007F">Is</SPAN> <SPAN style="color:#00007F">Nothing</SPAN> <SPAN style="color:#00007F">Then</SPAN><br>        <SPAN style="color:#007F00">' WB was not open yet</SPAN><br>        bFl = <SPAN style="color:#00007F">True</SPAN><br>        <SPAN style="color:#00007F">Set</SPAN> wbLate = Workbooks.Open(sPath)<br>    <SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">If</SPAN><br>    <br>    <SPAN style="color:#00007F">Set</SPAN> rNew = wbLate.ActiveSheet.Range("A1")<br>    <SPAN style="color:#007F00">' as we are transferring lots of data speed up _<br>      process, by not doing recalcs etc.</SPAN><br>    Application.EnableEvents = <SPAN style="color:#00007F">False</SPAN><br>    <SPAN style="color:#00007F">Do</SPAN> <SPAN style="color:#00007F">While</SPAN> rNew.Value <> vbNullString<br>        <SPAN style="color:#007F00">'go through each row until empty row</SPAN><br>        <SPAN style="color:#00007F">Set</SPAN> rOutp = wbDB.ActiveSheet.Columns("A").Find(rNew.Value, _<br>                        searchdirection:=xlNext)<br>        <SPAN style="color:#00007F">If</SPAN> rOutp <SPAN style="color:#00007F">Is</SPAN> <SPAN style="color:#00007F">Nothing</SPAN> <SPAN style="color:#00007F">Then</SPAN>        <SPAN style="color:#007F00">' not found, so add</SPAN><br>            <SPAN style="color:#007F00">' find bottom row</SPAN><br>            <SPAN style="color:#00007F">Set</SPAN> rOutp = wbDB.ActiveSheet.Columns("A").Find(what:="*", _<br>                        after:=Cells(1, 1), _<br>                        searchdirection:=xlPrevious).Offset(1, 0)<br>        <SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">If</SPAN><br>        <SPAN style="color:#007F00">' set values to row of new data</SPAN><br>        rOutp.Resize(1, rNew.CurrentRegion.Columns.Count).Value = _<br>                rNew.Resize(1, rNew.CurrentRegion.Columns.Count).Value<br>        <br>        <SPAN style="color:#00007F">Set</SPAN> rNew = rNew.Offset(1, 0) <SPAN style="color:#007F00">' one row down</SPAN><br>    <SPAN style="color:#00007F">Loop</SPAN><br>    Application.EnableEvents = <SPAN style="color:#00007F">True</SPAN><br>    <br>    <SPAN style="color:#007F00">'close DB file if it was not open</SPAN><br>    <SPAN style="color:#00007F">If</SPAN> bFl <SPAN style="color:#00007F">Then</SPAN><br>        wbDB.Close savechanges:=<SPAN style="color:#00007F">True</SPAN><br>    <SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">If</SPAN><br>    MsgBox "Successfully updated"<br>CleanUp:<br>    <SPAN style="color:#00007F">Set</SPAN> wbLate = <SPAN style="color:#00007F">Nothing</SPAN><br>    <SPAN style="color:#00007F">Set</SPAN> wbDB = <SPAN style="color:#00007F">Nothing</SPAN><br>    <SPAN style="color:#00007F">Set</SPAN> rNew = <SPAN style="color:#00007F">Nothing</SPAN><br>    <SPAN style="color:#00007F">Set</SPAN> rOutp = <SPAN style="color:#00007F">Nothing</SPAN><br>    Application.ScreenUpdating = <SPAN style="color:#00007F">True</SPAN><br>    <br><SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">Sub</SPAN><br><br><SPAN style="color:#007F00">'--------------------------------------------</SPAN><br><SPAN style="color:#00007F">Function</SPAN> GetFName(sPath <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">String</SPAN>) <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">String</SPAN><br><SPAN style="color:#007F00">'</SPAN><br><SPAN style="color:#007F00">' Function that splits the filename from _<br>  the path passed as parameter. Function _<br>  returns the file name.</SPAN><br><SPAN style="color:#007F00">'--------------------------------------------</SPAN><br>    GetFName = Right(sPath, Len(sPath) - InStrRev(sPath, "\"))<br><SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">Function</SPAN><br><br></FONT>
 
Upvote 0
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



Hello Again,

Thanks for your help.

I think in this part something needs to be changed, because at this point the code adds missing values at the end of my table, but it overwrites also values for founded value, which is not desired.

Can you please take a look once again?
 
Upvote 0

Forum statistics

Threads
1,214,987
Messages
6,122,618
Members
449,092
Latest member
amyap

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