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"?
 

Excel Facts

Quick Sum
Select a range of cells. The total appears in bottom right of Excel screen. Right-click total to add Max, Min, Count, Average.
Try
VBA Code:
Cells(c.Row, "m").FormulaR1C1 = "=IF(MOD(RC[-12],1)<=TIMEVALUE(""3:50:00 PM""),""RB"",""FM"")"
 
Last edited:
Upvote 0
Actually try
VBA Code:
Cells(c.Row, "m").FormulaR1C1 = "=IF(RC[-12]<=INT(TODAY())+TIMEVALUE(""3:50:00 PM""),""RB"",""FM"")"

As my previous post didn't take account of today's date

and if you want it strictly only to apply to today's date try

VBA Code:
Cells(c.Row, "m").FormulaR1C1 = "=IF(INT(RC[-12])=TODAY(),IF(RC[-12]<=INT(TODAY())+TIMEVALUE(""3:50:00 PM""),""RB"",""FM""),"""")"
 
Last edited:
Upvote 0
You're welcome

You should be able to just use
VBA Code:
Cells(c.Row, "m").FormulaR1C1 = "=IF(INT(RC[-12])=TODAY(),IF(RC[-12]<=TODAY()+TIMEVALUE(""3:50:00 PM""),""RB"",""FM""),"""")"
 
Upvote 0
Mark, you outdid yourself. I entered and tested this new solution and it works exactly as needed. I'm most grateful thank you very much
 
Upvote 0
You're welcome (although if using the formula on multiple lines I would recommend putting =TODAY() in a separate cell and referencing it in the formula rather than having it within the formula)
 
Upvote 0
Sorry Mark your last statement is a little over my head but regardless, your previous replies were immensely helpful and I can't thank you enough for all your time and effort and immediate response.
 
Upvote 0
It just means put
Excel Formula:
=TODAY()
in a separate cell (in the code below Z3) and reference the cell in the formula, it'll be more efficient than having it calculate twice in every formula

Rich (BB code):
Cells(c.Row, "m").FormulaR1C1 = _
        "=IF(INT(RC[-12])=R3C26,IF(RC[-12]<=R3C26+TIMEVALUE(""3:50:00 PM""),""RB"",""FM""),"""")"
 
Upvote 0
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)
 
Upvote 0

Forum statistics

Threads
1,224,819
Messages
6,181,153
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