Macro to compare previous worksheet only to new worksheet.

Levi2020

New Member
Joined
Sep 2, 2019
Messages
4
I have a workbook that is used to keep track of hub mileage (in column B) and hours ran (in column C) of trailers. A new sheet is added weekly to record that weeks data.
I am trying to come up with a macro that will compare only the previous weeks data and throw up an alert if the mileage gap is greater than
2500 and hours gap is greater than 150. Can anyone help with this?
 

Excel Facts

Save Often
If you start asking yourself if now is a good time to save your Excel workbook, the answer is Yes
Hello.

Care to post some sample data, with description of tab names and how new ones are created, and how you want the alert to work.
 
Upvote 0
Tab names are just the date that the current week is completed. It's just a basic sheet. New tabs were being created manually until I added this macro.
Sub CopyRename()
Dim sName As String
Dim wks As Worksheet
Worksheets("MASTER3").Copy after:=Sheets(Worksheets.Count)
Set wks = ActiveSheet
Do While sName <> wks.Name
sName = Application.InputBox _
(Prompt:="Enter new worksheet name")
On Error Resume Next
wks.Name = sName
On Error GoTo 0
Loop
Set wks = Nothing
End Sub

A
[TABLE="width: 455"]
<colgroup><col><col><col><col><col></colgroup><tbody>[TR]
[TD]UNIT[/TD]
[TD]HUB MILES[/TD]
[TD="align: right"]2500[/TD]
[TD]HOURS[/TD]
[TD="align: right"]150[/TD]
[/TR]
[TR]
[TD]S5316280[/TD]
[TD]86994[/TD]
[TD]____[/TD]
[TD]10247[/TD]
[TD]____[/TD]
[/TR]
[TR]
[TD]S5316281[/TD]
[TD]74496[/TD]
[TD]____[/TD]
[TD]11746[/TD]
[TD]____[/TD]
[/TR]
[TR]
[TD]S5316282[/TD]
[TD]83207[/TD]
[TD]____[/TD]
[TD]10461[/TD]
[TD]____[/TD]
[/TR]
[TR]
[TD]S5316283[/TD]
[TD]76009[/TD]
[TD]____[/TD]
[TD]9647[/TD]
[TD]____[/TD]
[/TR]
[TR]
[TD]S5316284[/TD]
[TD]77772[/TD]
[TD]____[/TD]
[TD]10689[/TD]
[TD]____[/TD]
[/TR]
[TR]
[TD]S5316285[/TD]
[TD]90824[/TD]
[TD]____[/TD]
[TD]11287[/TD]
[TD]____[/TD]
[/TR]
[TR]
[TD]S5316286[/TD]
[TD]77509[/TD]
[TD]____[/TD]
[TD]10058[/TD]
[TD]____[/TD]
[/TR]
[TR]
[TD]S5316287[/TD]
[TD]79138[/TD]
[TD]____[/TD]
[TD]10836[/TD]
[TD]____[/TD]
[/TR]
</tbody>[/TABLE]


Right now this formula is being used =IF('8.21.2019'!B2+'8.28.2019'!$C$1<='8.28.2019'!B2,"RECHECK","____"). This however has to be changed on each new sheet. What I would like is be able to run a macro after all new data is entered to do this check automatically and maybe highlight the cells of those that need rechecking. The issue I am having is I can't find anything that references comparing the previous page to current page only. These pages will change weekly as you add new.
 
Upvote 0
Hello,

Have included your code and added the bottom few lines.

Code:
Sub CopyRename()
Dim sName As String
Dim wks As Worksheet
Worksheets("MASTER3").Copy after:=Sheets(Worksheets.Count)
Set wks = ActiveSheet
Do While sName <> wks.Name
sName = Application.InputBox _
(Prompt:="Enter new worksheet name")
On Error Resume Next
wks.Name = sName
On Error GoTo 0
Loop
Set wks = Nothing
'code to insert formula
    MY_LAST = Sheets(Worksheets.Count - 1).Name
    MY_NEW = Sheets(Worksheets.Count).Name
    Range("C2").Formula = "=IF('" & MY_LAST & "'!B2+'" & MY_NEW & "'!$C$1<='" & MY_NEW & "'!B2," & """RECHECK""" & "," & """-----""" & ")"
    Range("C2").Copy Range("C3:C" & Range("B" & Rows.Count).End(xlUp).Row)
End Sub
 
Upvote 0
I appreciate your help. This has gotten me farther than I could have gotten myself. The only issue I am having now is the formula is pasting to the master sheet instead of the newly created sheet. Any ideas?

Sub CopyRename()

Dim sName As String
Dim wks As Worksheet

Worksheets("MASTER3").Copy after:=Sheets(Worksheets.Count)
Set wks = ActiveSheet
Do While sName <> wks.Name
sName = Application.InputBox _
(Prompt:="Enter new worksheet name")

On Error Resume Next
wks.Name = sName

On Error GoTo 0
Loop
Set wks = Nothing


MY_LAST = Sheets(Worksheets.Count - 1).Name
MY_NEW = Sheets(Worksheets.Count).Name
Range("C2:C56,C58:C78").Formula = "=IF('" & MY_LAST & "'!B2+'" & MY_NEW & "'!$C$1<='" & MY_NEW & "'!B2," & """RECHECK""" & "," & """-----""" & ")"
Range("E2:E56,E58:E78").Formula = "=IF('" & MY_LAST & "'!D2+'" & MY_NEW & "'!$E$1<='" & MY_NEW & "'!D2," & """RECHECK""" & "," & """-----""" & ")"
Range("C2:C56,C58:C78").Copy Range("C3:C" & Range("B" & Rows.Count).End(xlUp).Row)
Range("E2:E56,E58:E78").Copy Range("E3:E" & Range("D" & Rows.Count).End(xlUp).Row)






End Sub
 
Upvote 0

Forum statistics

Threads
1,224,820
Messages
6,181,155
Members
453,021
Latest member
Justyna P

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