Link to home
Start Free TrialLog in
Avatar of Andreamary
Andreamary

asked on

Update Excel macro with variable criteria

I have a macro that I run on a daily report. Within the macro code, there is a date that needs to be revised every two months for the macro to be effective. I would like to revise a macro if possible so that instead of having to edit the date in the VBA code every publication cycle, that a reference table is included within the VBA code.

The current macro does the following:
  1. Extracts the publication date from the text string in B4 and copies it into $C$5
  1. If date in $B$5 is greater than the hard-coded cut-off date of 19-July-17 at 23:00 hrs AND if $K11 does not equal "89" or "99", then replace contents of $D11 with text string = "LATE". If conditions are not met, then no change to $D11 (original text string retained, or if $D11 was blank, then it remains blank).

Current macro code:
Sub ProMS_Transfer_Late_WOs()
Dim lr As Long
Dim rng As Range, cell As Range
Dim compDate As Date
lr = Cells(Rows.Count, "K").End(xlUp).Row
Set rng = Range("K11:K" & lr)
compDate = DateValue("19 July 2017") + TimeValue("23:00:00")
Columns("B:B").ColumnWidth = 5
Columns("D:D").ColumnWidth = 32.43
Columns("F:F").ColumnWidth = 6
Columns("G:G").ColumnWidth = 42.14
Range("C5").Select
ActiveCell.FormulaR1C1 = "=TEXT(MID(R4C2,169,11),""dd-mmm-yyyy"")"
If Range("c5").Value > compDate Then
    For Each cell In rng
        Cells(cell.Row, "D").Select
        If cell <> 89 And cell <> 99 Then
            Cells(cell.Row, "D") = "LATE"
        End If
    Next cell
End If
End Sub

Open in new window


Desired revisions to current macro:
  1. Determine the Cut-off Date for the Pub Date listed in $C$5 by finding the $C$5 pub date in the Pub Date list (see below)
    1. The Cut-off Date associated with each Pub Date is listed in the column to the right of the Pub Date called Cut-off Date, in the same row
  2. If report 'Execution' date (B5) is > than the determined Cut-off Date at 23:00 hrs AND if $K11 does not equal "89" or "99", then replace contents of $D11 with text string = "LATE". If conditions are not met, then no change to $D11 (original text string retained, or if $D11 was blank, then it remains blank).

Error handling:
If Publication Date in $C$5 is not found in the Pub Cycle list below, a message box pops up saying 'Pub Cycle date not found'.

Here is the reference table of Pub Dates and associated Cut-off Dates:
Pub Date      Cut-off Date
17-Aug-17      24-May-17
12-Oct-17            19-Jul-17
07-Dec-17      13-Sep-17
01-Feb-18      08-Nov-17
29-Mar-18      03-Jan-18
24-May-18      28-Feb-18
19-Jul-18            25-Apr-18
13-Sep-18      20-Jun-18
08-Nov-18      15-Aug-18
03-Jan-19            10-Oct-18
28-Feb-19      05-Dec-18
25-Apr-19            30-Jan-19
20-Jun-19            27-Mar-19
15-Aug-19      22-May-19
10-Oct-19            17-Jul-19
05-Dec-19      11-Sep-19

I have attached a sample of the report that the macro is run on.

I hope I've provided enough detail and clarity. Just let me know if you need anything more.

Thanks!
Andrea
EE_macro.xls
Avatar of Shums Faruk
Shums Faruk
Flag of India image

Hi Andrea,

Try below:
Sub ProMS_Transfer_Late_WOs()
Dim Ws As Worksheet
Dim LR As Long
Dim Rng As Range, c As Range
Dim CompDate As Date
Set Ws = Worksheets("PA01")
LR = Ws.Cells(Rows.Count, "K").End(xlUp).Row
Set Rng = Ws.Range("K11:K" & LR)
Application.ScreenUpdating = False
CompDate = Ws.Range("D5") + TimeValue("23:00:00")
Ws.Columns("B:B").ColumnWidth = 17.5
Ws.Columns("D:D").ColumnWidth = 10
Ws.Columns("F:F").ColumnWidth = 7
Ws.Columns("G:G").ColumnWidth = 42.14
Ws.Range("C5").FormulaR1C1 = "=TEXT(MID(R4C2,169,11),""dd-mmm-yyyy"")"
Ws.Range("D5").FormulaR1C1 = "=IFERROR(VLOOKUP(DATEVALUE(RC3),Table!C1:C2,2,0),""Pub Cycle date not found"")"
If Ws.Range("D5").Value = "Pub Cycle date not found" Then
    MsgBox "Pub Cycle date not found"
    Exit Sub
ElseIf Ws.Range("C5").Value > CompDate Then
    For Each c In Rng
        If Ws.Cells(c.Row, "K") <> 89 And Ws.Cells(c.Row, "K") <> 99 Then
            Ws.Cells(c.Row, "D").Value = "LATE"
        End If
    Next c
End If
Application.ScreenUpdating = True
End Sub

Open in new window

Andrea_EE_macro.xlsm
SOLUTION
Avatar of Mike in IT
Mike in IT
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Andreamary
Andreamary

ASKER

Hi Shums and Mike,

Thanks for your quick responses...much appreciated!

Shums — I was testing your solution on some of my actual reports and found it wasn't working. In drilling down, I realize it's because my daily reports do not have the table in the second tab, so I get the 'Pub cycle not found' message box. I do store this macro in a Personal.xlsb file. Is it possible for me to add the table tab into that file instead, and have the macro code reference it in that location, since the Personal.xlsb file will always be open and available?

Mike — I ran your macro and got the following error:
Compile error: Object required, and 'compDate' on line 15 was highlighted
I wasn't sure what that meant...

Cheers,
Andrea
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Hi Shums,

Thanks for the second solution. It worked fine for the 12-Oct-2017 Pub Date, but when I tested it on the 07-Dec-2017 Pub Date (which has a Cut-off Date of 13-Sep-2017) with a report 'Execution' date in B5 of 07-Sep-2017, it interpreted the work orders as late. Just to confirm:

If 'Execution' date (B5) of report is < or = to Cut-off Date at 23:00, no work orders are converted to LATE as described above.
If 'Execution' date (B5) of report is > Cut-off Date at 23:00, work orders that do not have '89' or '99' in Col K are converted to LATE as described above.

I have attached the test DEC report in case that helps.

Thanks,
Andrea
Test-Report_DEC.xls
Hi Andrea,

I tried and it worked...try attached...
Test-Report_DEC.xlsm
Hi Shums,

Just to confirm what's not working at my end: the work orders in the test DEC report are being changed to LATE , and they should not be.

In the DEC report, B5 date (07-Sep-2017)  is < the DEC Cut-off date of 13-Sep-2017, so the macro should not change any work orders to LATE. When I run the macro, however, the work orders are being changed to LATE.

Hope that helps...

Andrea
Hi Shums,

I tried your latest version and it appears to be doing the same thing as the previous version: identifying the work orders as LATE when, in fact, they are not, since the cut-off date for 07-DEC-2017 is 13-Sep-2017, and the B5 date in the test report is 07-Sep-2017. The reason I am testing this scenario is to ensure the macro will work correctly when run anytime, and only changing work orders to LATE if the report execution date is > than the cut-off date.

Maybe we can return to your first solution provided in the file called "Andrea_EE_macro" , in which the code inserted the correct 'Cut-off Date' into D5. The only challenge with that solution was having the table in the report because my daily reports will not have the table in them. I do store this macro in a Personal.xlsb file. Is it possible for me to add the table tab into that file instead, and have the macro code reference it in that location, since the Personal.xlsb file will always be open and available?

I'm sorry this is taking up so much of your time. I hope I am being clear enough in my feedback...

Andrea
Hi Shums,

I just noticed that the following line of macro code referred to "C5", which is the Pub Date and not the report Execution date. So I tested revising the code to "B5", and it's working:

I changed this line:
If Ws.Range("C5").Value > CompDate Then

To read:
If Ws.Range("B5").Value > CompDate Then

So thanks very much for your patient work on this solution! I will close the question, and share the points to acknowledge both you and Mike's much appreciated contributions.

Cheers
Andrea
Thanks for your approach, Mike, and for taking it through to completion, Shums!
Glad it worked out and sorry for not being able to return to this question sooner to help more.