Reputation: 7105
I have 2 worksheets in a workbook,namely "MasterSheet" and "Operations". The data in the mastersheet is constantly changing whereas the data in operations is largely static.
I would like to be able to copy all records in column A in the master sheet if it does not exist in column A of the "Operations" worksheet. If it exists, it is simply ignored. I also need it to appear in the same order as it appears in the master sheet as well as copy columns B,C and D from the master sheet to the "Operations" worksheet.
If a record is deleted from the mastersheet, no data changes in the "Operations" worksheet.
How do i achieve this using VBA?
The version of Excel installed is 2010 and 2013.
** EDIT **
I tried with the following code and it doesn't work i.e. no records are copied. I also changed the worksheet names to 1 and 2.
Sub x()
Dim r1 As Excel.Range
Dim r2 As Excel.Range
Dim r3 As Excel.Range
Dim c As Excel.Range
Set r1 = Sheets(1).Range("A1").Resize(Range("A" & Rows.Count).End(xlUp).Row)
Set r2 = Sheets(2).Range("A1").Resize(Range("A" & Rows.Count).End(xlUp).Row)
For Each c In r2
Set r3 = r1.Find(What:=c.Value, MatchCase:=False, Lookat:=xlWhole)
If r3 Is Nothing Then
Sheets(2).Range("A" & Rows.Count).End(xlUp).Offset(1).Value = c.Value
End If
Next
End Sub
Upvotes: 0
Views: 2448
Reputation: 11
Have you looked into the Range.AdvancedFilter Method? It may work for you.
into http://msdn.microsoft.com/en-us/library/office/ff841242(v=office.15).aspx
In your requirement you did not specify the order sequence of the merged document just that the source (master data) values need to be in source order. This will affect how you handle the combined sheet. Adding a column to represent sort order with unique id's (ordered sequence of numbers) to both worksheets may be helpful.
Assumption/Clarification
Pseudo code
name range for master database
you can do this manually or automate it
count number of rows in master
Dim masterSheet As Worksheet
Dim masterRows As Long As Long
Set masterSheet = ThisWorkbook.Sheets("master")
masterRows = master.UsedRange.Rows.Count
name range for operations database
copy range for master to new tab
Worksheets("master").Range("MasterDataRange").Copy _
destination:=Worksheets("newMasterTabName").Range("A1")
copy range for operations to new tab underneath the master data (masterRows + 1)
Worksheets("operations").Range("OperationsDataRange").Copy _
destination:=Worksheets("newMasterTabName").Range("master.UsedRange.Rows.Count")
name new consolidated range (from the destination tab - s/b eqal to number rows master + number rows operations)
invoke AdvancedFilter on consolidated table
Range("consolidatedNewMasterRangeName").AdvancedFilter _
Action:=xlFilterInPlace, _
CriteriaRange:=Range("Criteria") _
Unique:=True
Once again, if you add a sort order to your Criteria so you have A,B,C,D,sortId you can use sortId after merge to reorder the list at intended.
My advise is to get it working using the gui first. Then replicate in VBA.
Upvotes: 1