Auto copy new data entered based on non-duplicative data in a different sheet

SewStage

Board Regular
Joined
Mar 16, 2021
Messages
70
Office Version
  1. 365
Platform
  1. Windows
Hi. I'm looking for a way to automatically add the Date and Destination on the Mileage Log worksheet from the Date of Purchase and Company cells on Material Purchases worksheet every time I add a new entry; but 1) only if Online column does not have an X, and 2) only add unique combinations of Date of Purchase and Company (so I don't have a ton of duplicates). Ultimately looking for a way to not manually enter all my mileage if I can link it back to the dates of the physical purchases. Thanks much.
2021 Inventory Management.xlsx
AB
1DateDestination
21/19/2021Target
31/15/2021Home Depot
41/19/2022Michaels
51/23/2021Affinity
61/23/2021Costco
71/23/2021Joann Fabrics
81/26/2021Hobby Lobby
91/26/2021Joann Fabrics
101/28/2021Affinity
111/28/2021Joann Fabrics
121/30/2022Affinity
Mileage Log

2021 Inventory Management.xlsx
ABCDEFGHI
1CategoryDescriptionDetails Cost UnitsPer Unit CostCompanyDate of PurchaseOnline
24KitsQuilt kitHoliday Hearthland Advent Calendar$ 46.011$ 46.01MSQC1/16/2021X
25Shippingshipping$ 5.00 MSQC1/16/2021X
26NotionsNotionsLongarm centering tape$ 21.24 Amazon1/17/2021X
27NotionsNotionsgnome embellishments$ 29.45 Michaels1/19/2021
28SocksSocksFuzzy socks$ 12.006$ 2.00Target1/19/2021
29FabricFabricAndover by the Sea (for Ribbons quilt)$ 81.608$ 10.20Jordan Fabrics1/20/2021X
30FabricLayer cakeAndover by the Sea (for Ribbons quilt)$ 29.951$ 29.95Jordan Fabrics1/20/2021X
31SocksSocksGnome socks$ 8.976$ 1.50Joann Fabrics1/22/2021X
32FabricCork18x27$ 78.001.5$ 52.00Affinity1/23/2021
33FabricFabric$ 44.753.5$ 12.79Affinity1/23/2021
34FabricFQ$ 6.502$ 3.25Affinity1/23/2021
35FabricFabricLoyalty card$ (50.00) Affinity1/23/2021
36FabricFabricDiscount + tax$ (15.43) Affinity1/23/2021
37NotionsRicerice$ 9.99400$ 0.02Costco1/23/2021
38NotionsNotions$ 18.67 Joann Fabrics1/23/2021
39FabricFabricCustom placemats$ 18.502$ 9.25Joann Fabrics1/23/2021
40NotionsNotionsDiscount + tax$ (5.24) Joann Fabrics1/23/2021
41FabricJelly rollModa Solana$ 36.951$ 36.95Jordan Fabrics1/23/2021X
42NotionsNotionsCross My Heart pattern$ 9.95 Jordan Fabrics1/23/2021X
43Shippingshipping$ 4.95 Jordan Fabrics1/23/2021X
44FabricLayer cakeChocolicious$ 26.951$ 26.95Jordan Fabrics1/24/2021X
45Shippingshipping$ 4.95 Jordan Fabrics1/24/2021X
46NotionsNotionsgnome embellishments$ 12.99 Hobby Lobby1/26/2021
47NotionsNotionsgnome embellishments$ 17.97 Joann Fabrics1/26/2021
48FabricFabric$ 20.672.25$ 9.19Joann Fabrics1/26/2021
Material purchases
Cell Formulas
RangeFormula
D29D29=111.55-29.95
D32D32=26*3
D33D33=6.5*6+5.75
D36D36=-19.81+4.38
E37E37=16*25
D38D38=10.19+3.49+4.99
D39D39=SUM(3.5+3.5+6+5.5)
D40D40=-7.43+2.19
D47D47=5.99+5.99+5.99
D48D48=6.99+1.75+3.5+4+4.43
F24:F48F24=IF(E24>0,D24/E24,"")
 

Excel Facts

Spell Check in Excel
Press F7 to start spell check in Excel. Be careful, by default, Excel does not check Capitalized Werds (whoops)
Hi, this can be done with the worksheet change event, the thing is that only fires when you manually adjust a cells value, therefore in order to get the code to work the "Date Of Purchase" column has to be the last cell in which you enter data & it has to be done manually, not dragged or pasted, if you do that the following code should work for you.

Put the following code in a module;

VBA Code:
Sub AddMilelageLog()
Dim ID As String, s1 As String, s2 As String
Dim WS As Worksheet, WS1 As Worksheet
Dim MatchingID As Long
Dim i As Long, LastRow As Long, MaxRow As Long
Dim SearchedValue As String

    Application.ScreenUpdating = False
    Application.DisplayAlerts = False

    s1 = ActiveCell.Offset(-1, 0).Address
    s2 = ActiveCell.Offset(-1, -1).Address
    
    MaxRow = LastRowColumn("R")

    ID = ActiveCell.Offset(-1, 0).Value & "~" & ActiveCell.Offset(-1, -1)
    
    Set WS = Worksheets("Mileage Log")
    Set WS1 = Worksheets("Material purchases")
    
    For i = 1 To MaxRow
         If StrComp(WS.Range("A" & i).Value & "~" & WS.Range("B" & i).Value, ID, vbTextCompare) = 0 Then
              MatchingID = i
              Exit For
         End If
    Next i
    
    If i > MaxRow Then
        WS.Select
        WS.Cells(2, 1).Select
        LastRow = LastRowColumn("R") + 1
        WS.Cells(LastRow, 1).Value = WS1.Range(s1).Value
        WS.Cells(LastRow, 2).Value = WS1.Range(s2).Value
    End If
    
    WS1.Select
    
    Application.ScreenUpdating = True
    Application.DisplayAlerts = True
    
End Sub
Function LastRowColumn(RowColumn As String) As Long
Dim sht As Worksheet
'PURPOSE: Function To Return the Last Row Or Column Number In the Active Spreadsheet
'INPUT: "R" or "C" to determine which direction to search
Set sht = ActiveSheet

Select Case LCase(Left(RowColumn, 1)) 'If they put in 'row' or column instead of 'r' or 'c'.
  Case "c"
    LastRowColumn = sht.Cells.Find("*", LookIn:=xlFormulas, SearchOrder:=xlByColumns, _
    SearchDirection:=xlPrevious).Column
  Case "r"
    LastRowColumn = sht.Cells.Find("*", LookIn:=xlFormulas, SearchOrder:=xlByRows, _
    SearchDirection:=xlPrevious).Row
  Case Else
    LastRowColumn = 1
End Select

End Function

Put this code behind sheet "Material purchases" & the code will run each time you enter a date.

VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
    With Sheets("Material purchases")
        If Not Intersect(Target, Range("H:H")) Is Nothing Then
            If ActiveCell.Offset(-1, 1) = "X" Or ActiveCell.Offset(-1, 1) = "2" Or ActiveCell.Offset(-1, 1) = 2 Then
               Exit Sub
            Else
               Call AddMilelageLog
            End If
        End If
    End With
End Sub
 
Upvote 0
Thank you so much for taking the time to respond, JW00, but even though I DID spend 30+ years in IT I'm not a developer and have no knowledge of VBA, unfortunately. What I've done for now is just create a pivot table and manually delete dups, which is still a lot quicker than manually entering hundreds of entries.

Again, thanks much; truly appreciate it!
 
Upvote 0
No problem & thanks for your kind words, I noticed you are using 365 so try this formula in A2 in a new sheet, it may just save you some time.

=UNIQUE(FILTER('Material purchases'!G:H,('Material purchases'!I:I="")*('Material purchases'!G:G<>0)))
 
Upvote 0
Solution
I am so jealously in awe of all of you that know this stuff!!! That worked perfectly; THANK YOU!
 
Upvote 0
I'm glad it is working for you, all the best.
 
Upvote 0

Forum statistics

Threads
1,215,061
Messages
6,122,921
Members
449,094
Latest member
teemeren

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