VBA Code based on NOW (time)

poikl

Active Member
Joined
Jun 8, 2002
Messages
484
Platform
  1. Windows
Hi,
I have the following code in MACRO in ColM to enter initials based on the NOW entry in ColA. However it always enters FM no matter what time so it's not working!
In ColM I have entered "Cells(c.Row, "m").Formula = "=IF(RC[-12]<=TIMEVALUE(""3:50:00 PM""),""RB"",""FM"")"
In ColA I have:"Cells(c.Row, "A").Value = Format(Date, "m/d/yy") & " " & Format(Time, "hh:mm:ss")
Can you please help correct the code so that an entry after 3:50PM shows "FM" and anything earlier (of same date) shows "RB"?
 
If I can only ask if you would please be able to include in the above Code another possibility to enter in ColM RB (besides the one above) that if entry in ColA equals the one directly above or below it would ALSO enter RB in ColA.
So for example if the Time entered in Row 10 ColA either equals Row 9 or Row11 it would show RB in ColM (even if it's past 3:50 PM)
I'll look at it tomorrow
 
Upvote 0

Excel Facts

Repeat Last Command
Pressing F4 adds dollar signs when editing a formula. When not editing, F4 repeats last command.
Thank you so much in advance, and will be looking forward:
Just to clarify: If you can please include in the code that you've graciously written for me, "Cells(c.Row, "m").FormulaR1C1 = "=IF(INT(RC[-12])=TODAY(),IF(RC[-12]<=TODAY()+TIMEVALUE(""3:50:00 PM""),""RB"",""FM""),"""")"
an OR and AND statement that if entry in ColA equals the one directly above or below it would ALSO enter RB in ColA.
 
Upvote 0
Rich (BB code):
another possibility to enter in ColM RB (besides the one above) 
that if entry in ColA equals the one directly 
above or below it would ALSO enter RB in ColA.

the one directly above or below it would ALSO enter RB in ColA.

that if entry in ColA equals the one directly above or below it would ALSO enter RB in ColA.

if the Time entered in Row 10 ColA either equals Row 9 or Row11 it would show RB in ColM

The bolded statements above do not match.... please clarify

Please also note...
If it is ColA
Then it would need to be VBA not a formula because It would cause a circular reference.
You can't have a value and a formula in the same cell
It will affect the formula in ColM
 
Upvote 0
I’m very sorry Mark for the typo and thank you so much for noticing and replying so quickly. Yes I was asking for help to adjust an existing VBA MACRO.
But wherever it says “it would ALSO enter RB in ColA.” I actually meant ColM. You already had written for me that it should enter RB in ColM if it’s after 3:50 so I’m asking if you can include in that same VBA command an additional possibility for RB to show in ColM if the Time shown in ColA either equals the row above or the Row below. Again I’m sorry to have confused you with my mistake but it has been a very tiring night yesterday
 
Upvote 0
Try (assumed that you only want the last test to apply only if it is todays date)...
VBA Code:
Cells(c.Row, "m").FormulaR1C1 = _
        "=IF(AND(INT(RC[-12])=R3C26,OR(RC[-12]=R[-1]C[-12],RC[-12]=R[1]C[-12])),""RB"",IF(INT(RC[-12])=R3C26,IF(RC[-12]<=R3C26+TIMEVALUE(""3:50:00 PM""),""RB"",""FM""),""""))"


Book1
ABLMYZ
107/11/2024 22:17
208/11/2024 22:17RB
308/11/2024 22:1708/11/2024
Sheet5
Cell Formulas
RangeFormula
M2M2=IF(AND(INT(A2)=$Z$3,OR(A2=A1,A2=A3)),"RB",IF(INT(A2)=$Z$3,IF(A2<=$Z$3+TIMEVALUE("3:50:00 PM"),"RB","FM"),""))
Z3Z3=TODAY()


Book1
ABLMYZ
107/11/2024 22:17
208/11/2024 22:17FM
307/11/2024 22:1708/11/2024
Sheet5
Cell Formulas
RangeFormula
M2M2=IF(AND(INT(A2)=$Z$3,OR(A2=A1,A2=A3)),"RB",IF(INT(A2)=$Z$3,IF(A2<=$Z$3+TIMEVALUE("3:50:00 PM"),"RB","FM"),""))
Z3Z3=TODAY()


Book1
ABLMYZ
108/11/2024 22:17
208/11/2024 22:17RB
307/11/2024 22:1708/11/2024
Sheet5
Cell Formulas
RangeFormula
M2M2=IF(AND(INT(A2)=$Z$3,OR(A2=A1,A2=A3)),"RB",IF(INT(A2)=$Z$3,IF(A2<=$Z$3+TIMEVALUE("3:50:00 PM"),"RB","FM"),""))
Z3Z3=TODAY()
 
Upvote 0
Thank you again for all your help it must’ve been so time consuming.
I had to leave work early so I won’t be able to try it till I get back early next week.
Just may I ask it seems that you created a Formula for me to copy and paste down the row but I was hoping if it was possible to include the added criteria (of ColA date & time equals row on top or below) into your previously created VBA code in my existing MACRO?
 
Upvote 0
It is the same macro, the tables are the results
 
Upvote 0
Thank you. I can’t wait to try it out and will let you know how it goes
 
Upvote 0
Hi
I copied and pasted the VBA code you wrote but ColM is BLANK!
Was I supposed to also enter the Formulas you wrote on the Excel Sheet "
=IF(AND(INT(A2)=$Z$3,OR(A2=A1,A2=A3)),"RB",IF(INT(A2)=$Z$3,IF(A2<=$Z$3+TIMEVALUE("3:50:00 PM"),"RB","FM"),""))
Z3 Z3 =TODAY()"?
 
Upvote 0
Was I supposed to also enter the Formulas you wrote on the Excel Sheet "
No!

When I run

VBA Code:
Sub testFormula()
    Dim c As Range
    For Each c In Range("A2:A10")

        Cells(c.Row, "m").FormulaR1C1 = _
                                      "=IF(AND(INT(RC[-12])=R3C26,OR(RC[-12]=R[-1]C[-12],RC[-12]=R[1]C[-12])),""RB"",IF(INT(RC[-12])=R3C26,IF(RC[-12]<=R3C26+TIMEVALUE(""3:50:00 PM""),""RB"",""FM""),""""))"
    Next

End Sub



With the data as per the below
Book1 (version 1) new.xlsb
ABLMXYZ
210/11/2024 02:17:10
309/11/2024 22:17:1611/11/2024
411/11/2024 22:17:16
511/11/2024 22:17:16
611/11/2024 22:17:16
710/11/2024 22:17:16
811/11/2024 04:17:16
911/11/2024 12:17:16
1011/11/2024 16:17:16
Sheet5
Cell Formulas
RangeFormula
Z3Z3=TODAY()



I get

Book1 (version 1) new.xlsb
ABLMXYZ
210/11/2024 02:17:10 
309/11/2024 22:17:16 11/11/2024
411/11/2024 22:17:16RB
511/11/2024 22:17:16RB
611/11/2024 22:17:16RB
710/11/2024 22:17:16 
811/11/2024 04:17:16RB
911/11/2024 12:17:16RB
1011/11/2024 16:17:16FM
Sheet5
Cell Formulas
RangeFormula
Z3Z3=TODAY()
M2:M10M2=IF(AND(INT(A2)=$Z$3,OR(A2=A1,A2=A3)),"RB",IF(INT(A2)=$Z$3,IF(A2<=$Z$3+TIMEVALUE("3:50:00 PM"),"RB","FM"),""))


Have you put =TODAY() in Z3?
 
Upvote 0

Forum statistics

Threads
1,223,889
Messages
6,175,224
Members
452,620
Latest member
dsubash

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