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

Will the fill handle fill 1, 2, 3?
Yes! Type 1 in a cell. Hold down Ctrl while you drag the fill handle.
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
UNITHUB MILES2500HOURS150
S531628086994____10247____
S531628174496____11746____
S531628283207____10461____
S531628376009____9647____
S531628477772____10689____
S531628590824____11287____
S531628677509____10058____
S531628779138____10836____

<colgroup><col><col><col><col><col></colgroup><tbody>
</tbody>


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,214,389
Messages
6,119,232
Members
448,879
Latest member
VanGirl

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