VBA Code to Compare Two String Entries with Time

OilEconomist

Active Member
Joined
Dec 26, 2016
Messages
421
Office Version
  1. 2019
Platform
  1. Windows
Hello and thanks in advance for your assistance. What is the VBA code to compare the following two entries.

(1) I want to make sure if the dates are the same. If so, proceed to the next part.
(2) If either entry is >9:30 AM, the I would like to reset that respective entry to 9:30 AM. e.g. .Cells(1, 1) would become .Cells(1, 1) = "2023-02-27, Mon @ 05:30 PM EST"
(3) Indicate which cell has the larger value.

.Cells(1, 1) = "2023-02-27, Mon @ 05:30 PM EST"
.Cells(1, 2) = "2023-02-27, Mon @ 08:30 AM EST"

Once again thanks!
 

Excel Facts

Repeat Last Command
Pressing F4 adds dollar signs when editing a formula. When not editing, F4 repeats last command.
This confuse me:
(2) If either entry is >9:30 AM, the I would like to reset that respective entry to 9:30 AM. e.g. .Cells(1, 1) would become .Cells(1, 1) = "2023-02-27, Mon @ 05:30 PM EST"

Do you mean:
would become .Cells(1, 1) = "2023-02-27, Mon @ 09:30 AM EST"
??
Also, how to indicate the larger cell? (highling background or font) ?
 
Upvote 0
@bebo021999 thanks not only for your response, but such a quick one. Apologies for the confusion. You are correct it would become Cells(1, 1) = "2023-02-27, Mon @ 09:30 AM EST".

I actually want to know which cell is larger than 9:30 AM so once the change is done I would know. Maybe bold it or both, if greater than 9:30 AM. Whichever one is or was greater, maybe just color fill the one with the greater value.
 
Upvote 0
once the change is done I would know. Maybe bold it or both, if greater than 9:30 AM.
Is this what you mean?

23 02 28.xlsm
AB
12023-02-27, Mon @ 05:30 PM EST2023-02-27, Mon @ 09:30 AM EST
22023-02-27, Mon @ 08:30 AM EST2023-02-27, Mon @ 08:30 AM EST
After 9.30
Cell Formulas
RangeFormula
B1:B2B1=IF(TIMEVALUE(MID(A1,19,8))>TIME(9,30,0),LEFT(A1,18)&"09:30 AM EST",A1)
Cells with Conditional Formatting
CellConditionCell FormatStop If True
A1:B2Expression=TIMEVALUE(MID(A1,19,8))>TIME(9,30,0)textNO
 
Upvote 0
VBA code;
VBA Code:
Option Explicit
Sub findDate()
Dim rng As Range, cell As Range, dateA As Date, dateB As Date, timeB
Set rng = Range("A1:B1")
dateA = DateValue(Left(rng.Cells(1, 1), 10))
For Each cell In rng
    dateB = DateValue(Left(cell, 10))
    If dateB <> dateA Then Exit Sub
Next
For Each cell In rng
    timeB = Mid(cell, Len(cell) - 11, 8)
    If CDate(timeB) - 9 / 24 > 0 Then
        cell.Value = Replace(cell, timeB, " 09:00 AM")
        cell.Interior.Color = vbYellow
    End If
Next
End Sub
Before
before.JPG



after
after.JPG
 
Upvote 0
VBA code;
VBA Code:
Option Explicit
Sub findDate()
Dim rng As Range, cell As Range, dateA As Date, dateB As Date, timeB
Set rng = Range("A1:B1")
dateA = DateValue(Left(rng.Cells(1, 1), 10))
For Each cell In rng
    dateB = DateValue(Left(cell, 10))
    If dateB <> dateA Then Exit Sub
Next
For Each cell In rng
    timeB = Mid(cell, Len(cell) - 11, 8)
    If CDate(timeB) - 9 / 24 > 0 Then
        cell.Value = Replace(cell, timeB, " 09:00 AM")
        cell.Interior.Color = vbYellow
    End If
Next
End Sub
Before
View attachment 86387


afterView attachment 86388
@bebo021999 that works, but can you make a couple of changes to get to my exact needed answer. This way if somebody uses the final solution, they're not wondering why they're not getting the exact answer. I will mark as the final solution once done.

change
VBA Code:
 cell.Value = Replace(cell, timeB, " 09:00 AM")

to
VBA Code:
 cell.Value = Replace(cell, timeB, "09:30 AM")

basically remove the space before the 09 and change the 09:00 AM to 09:30 AM.
 
Upvote 0

Forum statistics

Threads
1,214,620
Messages
6,120,554
Members
448,970
Latest member
kennimack

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