VBA Code to compare Dates, then run macro to match colums

Airstar

New Member
Joined
Oct 4, 2019
Messages
6
Hi, Hope I can get some help with adding code to loop through the dates in column A of Sheet1 & compare them to the dates in Column A, Sheet2. If the date on Sheet2 is greater than Sheet1 run the MatchColumns macro.

Sheet1 accumulates jobs with recommendations through out the year. Sheet2 accumulates completed recommendations. Any row on Sheet1 without a match needs to be followed up on. Duplications are appearing as highlighted on row 15. Since the job on row 15 took place on 1/24/19, the recommendation could not have been completed prior to that date, 12/19/2018

[FONT=Verdana,Arial,Tahoma,Calibri,Geneva,sans-serif]
Excel 2010
ABCGHIJKOPQR
1Sheet1 DataSheet2 Data
2Sch DteJob NMLast NMDescriptionNO MATCHSch DteJob NMLast NMDescription
312/1/201813738 - Gil SubGillmanReplace Thermostat $456, Compressor & Contactor (will email Quote)NO MATCH
412/3/201813740 - Dary, HaishDaryEstimate to replace equipment. Quote emailedNO MATCH
512/3/201813745 - McCray, VicMcCrayDS-Replace Condenser Fan Motor $700 ($595) (SCH)RECONCILED12/4/201813746 - McCray, VicMcCrayPerform Recommend work on- Condenser Fan Motor
612/8/201813772 - Dell, ErikDelClear roof Cap $193NO MATCH
712/14/201813782 - Ellen, RobEllenReplace Gas Valve $668 ($567) (SCH)RECONCILED12/17/201813797 - Ellen, RobEllenPerform Recommend work on- Gas Valve
812/14/201813765 - Palo, MarcPaloReplace Blower motor capacitor $208, Hard star kit $327, Pull & clean blower assembly $275, or Replace Blower motor $665. Upgrade equipmentNO MATCH
912/17/201813799 - Kane, CageKaneReplace unit with York package unit. $5,900NO MATCH
1012/18/201813790 - Moss, RenMossEstimate to replace equipment or ductwork. Will email quote when we get pricing from electrician. Quote emailed on 12/19/2019 for estimate.RECONCILED12/19/201813774 - Moss, Ren 2925MossPerformed recommend work on Ductwork
1112/18/201813796 - Kell, SuzKellEstimate to replace equipment. Quoted-Standard York system $7,778 less 10% discount $(777) for a net price of $7001. York condenser, furnace, evaporator coil, and thermostat. 10 Yr Part, 10 Yr compressor, 1 Yr labor warranty. (Not covered under warranty- dispatch fees, Refrigerant lines, refrigerant, T-stat wire, drain lines, ductwork and insulation.)NO MATCH
121/16/201913846 - Benny, DonnaBennyRewire air handler and thermostat $182NO MATCH
131/18/201913840 - Ender, BettEnderleReplace Reversing Valve Solenoid (Warranty) $214 -Order PartNO MATCH
141/23/201913880 - Laba, DaveLabaReplace Hot Surface Ignitor $227 ($193) (have to order part and return)RECONCILED1/24/201913940 - Laba, DaveLabaPerformed recommend work on-Ignitor
151/23/201913877 - Moss, Ren 2925MossEstimate to replace ductwork. Will sanitize ductwork, seal connections, replace registers, clean furnace and inspect system $1195. Customer will call us when the house is ready for us to do the job.RECONCILED12/19/201813774 - Moss, Ren 2925MossPerformed recommend work on Ductwork
161/24/201913882 - More, MichellMoreReplace inducer draft motor $566 ($481) Customer will not replace sellingNO MATCH
Sheet1


I've added the following code, but I', getting a run time error

[<code>Sub CompareDates() </code><code> </code><code>If Sheet1.Range("A1") > Sheet2.Range("A1") Then
Run "MatchColumns"
End If
End Sub]

Any help would greatly be appreciated by this really novice VBA/Excel user!

Thanks in advance:)
</code>[/FONT]<strike></strike>
 

Excel Facts

Can a formula spear through sheets?
Use =SUM(January:December!E7) to sum E7 on all of the sheets from January through December
See if this works for you

Code:
Sub t()
Dim i As Long
    For i = 3 To Sheet1.Cells(Rows.Count, 1).End(xlUp).Row 'If data starts on row other than 3, change accordingly.
        If Sheet1.Cells(i, 1).Value < Sheet2.Cells(i, 1).Value Then
            MatchColumns
        End If
    Next
End Sub
 
Last edited:
Upvote 0
JLGWhiz, thanks so much for your reply. Still getting the same result. Your code runs the match macro, but it's not evaluating the dates prior to the run. I've double checked to be sure Col A on both sheets is formatted as a date; Col I on Sheet1 is also formatted as date. I've also tried formatting as a number, but I'm still getting the same result. What do you think could be the problem?

Thanks again for your invaluable assistance!
 
Upvote 0
I set up two sheets with dates in column A beginning at row 3 on both. Some cells were left blank on sheet 2 as in the OP example. I used a fake macro named 'MatchColumns" with both codes being in the standard code module1. The code ran as expected with no errors. Where dates in sheet 1 were less than dates in sheet 2, the fake macro ran, blanks were bypassed as well as any dates equal to or less than the corresponding date on sheet 1. What is the error you are getting and which line of code is highlighted when you click the 'Debug' button? Your error might be in the called macro.
 
Last edited:
Upvote 0
JLGWhiz, I am still getting the same result as shown in the example above. I'm not getting an error in the macro, but some of the results are incorrect. On Sheet1 row 15 there is a match dated 12/19/2018 (Sheet2 row 6) which is not greater than 1/23/19 (Sheet1 row 15). The match marco should not have run in this instance. The job dated 12/19/2018 correctly matched to Sheet1 row10 dated 12/18/2018.

How can I get the date comparison macro to run against each match?

Here is a copy of the data from Sheet2

Excel 2010
ABCGH
1Sch DateJob NameLast NameItem DescriptionRECONCILED
212/3/201813692 - Dom, TamDomPerform Recommend work on capacitorsRECONCILED
312/4/201813746 - McCray, VicMcCrayPerform Recommend work on- Condenser Fan MotorRECONCILED
412/5/201813744 - Warden, VicWardenPerformed recommend work on- Circuit BoardRECONCILED
512/5/201813605 - Yung, RobYungPerform Recommend work on RAFGRECONCILED
612/19/201813774 - Moss, Ren 2925MossPerformed recommend work on DuctworkRECONCILED
712/14/201813747 - Thomas, L.ThomasPerform Recommend work on- Arma FlexRECONCILED
812/17/201813797 - Ellen, RobEllenPerform Recommend work on- Gas ValveRECONCILED
912/18/201813783 - Dragoon, LLCDragoonPerformed Recommended work on- Blower, Belt, Capacitor, Drain Line, CompressorRECONCILED
1012/18/201813800 - Brywn, GloBrownPerformed Recommended work on Master-Gas valve/burnersRECONCILED
111/11/201913830 - Biech, RobBiechPerform Recommend work - Pull & cleaned Evap Coil. Changed FiltersRECONCILED
121/24/201913940 - Laba, DaveLabaPerformed recommend work on-IgnitorRECONCILED
131/28/201913837 - Dimm, ClineDimmPerform Recommend Work on Blower Wheel Assembly $275, Replaced Blower Motor Capacitor $177, Put Armor-flex $55RECONCILED
141/29/201913884 - Ella, RonEllaPerform Recommend work on- Clean Blower AssemblyRECONCILED
15
Sheet2


And here is the code I am using to match columns

[Sub MatchColumns()

Dim i, total, fRow As Integer
Dim found As Range

total = Worksheets(1).Range("A" & Rows.Count).End(xlUp).Row

For i = 1 To total
answer1 = Worksheets(1).Range("C" & i).Value
Set found = Worksheets(2).Columns("C:C").Find(what:=answer1) 'finds a match

If found Is Nothing Then
Worksheets(1).Range("H" & i).Value = "NO MATCH"
Else
fRow = Sheets(2).Columns("C:C").Find(what:=answer1).Row
Worksheets(1).Range("I" & i).Value = Worksheets(2).Range("A" & fRow).Value
Worksheets(1).Range("J" & i).Value = Worksheets(2).Range("B" & fRow).Value
Worksheets(1).Range("K" & i).Value = Worksheets(2).Range("C" & fRow).Value
Worksheets(1).Range("L" & i).Value = Worksheets(2).Range("D" & fRow).Value
Worksheets(1).Range("M" & i).Value = Worksheets(2).Range("E" & fRow).Value
Worksheets(1).Range("N" & i).Value = Worksheets(2).Range("F" & fRow).Value
Worksheets(1).Range("O" & i).Value = Worksheets(2).Range("G" & fRow).Value
Worksheets(1).Range("H" & i).Value = Worksheets(2).Range("H" & fRow).Value

End If
Next i

End Sub]

Thanks so much for your help!
 
Upvote 0

Forum statistics

Threads
1,223,227
Messages
6,170,853
Members
452,361
Latest member
d3ad3y3

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