Hi everyone, I've attached sample tables and code below to help better visualize my problem. Each week, I run a program to update my database ("Table A") with data from "Table B". Part of it prompts the user to enter the week's number, filters "Table B" according to the input and then copies over the visible rows to Table A.
Lets say that in Week 5, after already running the macro, i see that one new Week 4 entry was retroactively added to ("Table B"). I would like to add this new row of data below the last Week 4 entry in Table A but before Week 5 to make it consistent. The intended result is shown in "Ideal Result" sheet.
In other words, i'm trying to figure out how to:
Lets say that in Week 5, after already running the macro, i see that one new Week 4 entry was retroactively added to ("Table B"). I would like to add this new row of data below the last Week 4 entry in Table A but before Week 5 to make it consistent. The intended result is shown in "Ideal Result" sheet.
In other words, i'm trying to figure out how to:
- Compare the visible rows in Table A and the entire database in Table B using the values in Column D (there is no primary key in the rows of data),
- Find the non-duplicates in Table A,
- And then copy & pasting them below the specified week's entry in Table B
Sample.xlsm | |||||||
---|---|---|---|---|---|---|---|
A | B | C | D | E | |||
1 | Account No | Account Name | Amount | Week | Client Name | ||
2 | 1001 | Company A | 2453 | Dec20 Week4 | Tom | ||
3 | 1002 | Company B | 158 | Dec20 Week4 | Bob | ||
4 | 1003 | Company C | 340 | Dec20 Week4 | Harry | ||
5 | 1005 | Company D | 141 | Dec20 Week5 | James | ||
6 | 1006 | Company F | 1550 | Dec20 Week5 | Robert | ||
Table A |
Sample.xlsm | |||||||
---|---|---|---|---|---|---|---|
A | B | C | D | E | |||
1 | Account No | Account Name | Amount | Week | Client Name | ||
2 | 1001 | Company A | 2453 | Dec20 Week4 | Tom | ||
3 | 1002 | Company B | 158 | Dec20 Week4 | Bob | ||
4 | 1003 | Company C | 340 | Dec20 Week4 | Harry | ||
5 | 1004 | Company D | 158 | Dec20 Week4 | John | ||
Table B |
Sample.xlsm | |||||||
---|---|---|---|---|---|---|---|
A | B | C | D | E | |||
1 | Account No | Account Name | Amount | Week | Client Name | ||
2 | 1001 | Company A | 2453 | Dec20 Week4 | Tom | ||
3 | 1002 | Company B | 158 | Dec20 Week4 | Bob | ||
4 | 1003 | Company C | 340 | Dec20 Week4 | Harry | ||
5 | 1004 | Company D | 158 | Dec20 Week4 | John | ||
6 | 1005 | Company E | 141 | Dec20 Week5 | James | ||
7 | 1006 | Company F | 1550 | Dec20 Week5 | Robert | ||
Ideal Result |
VBA Code:
Public Sub CopyData()
Set TableA = ThisWorkbook.Worksheets(1)
Set TableB = ThisWorkbook.Worksheets(2)
LastRowOfTableB = TableB.Range("B" & Rows.Count).End(xlUp).Row
'Filters Table A according to user input
Do
myValue = InputBox("Which week's data do you wish to copy over?" & vbCrLf & "Enter 1, 2, 3, 4 or 5 only")
If myValue = "" Then Exit Sub
Loop Until myValue > 0 And myValue < 6
If myValue = 4 Then
TableA.Range("A4").CurrentRegion.AutoFilter Field:=4, Criteria1:= _
"* Week4"
ElseIf myValue = 5 Then
TableA.Range("A4").CurrentRegion.AutoFilter Field:=4, Criteria1:= _
"* Week5"
End If
'Copies the Filtered Data Over
TableA.Range("A2:X200").SpecialCells(xlCellTypeVisible).Copy _
TableB.Range("A" & LastRowOfTableB)
End Sub