VBA Time change

BOGIA

Active Member
Joined
May 23, 2005
Messages
266
Hi everyone,

I'm trying to write a short statement to change the time if it is greater than 6:00:00 but it doesn't work. The data I have always 6:00:10 or more.

I have tried this but no good:

If range("a1") > 6:00:00 then
range("a1") = 6:00:00
End if

Could anyone please help! Thanks

TD
 

Excel Facts

Is there a shortcut key for strikethrough?
Ctrl+S is used for Save. Ctrl+5 is used for Strikethrough. Why Ctrl+5? When you use hashmarks to count |||| is 4, strike through to mean 5.
you could try something like;

Code:
Sub ChangeTime()

Dim myTime As Date

myTime = Format(Cells(1, 1), "hh:mm:ss")
If myTime > "6:00:00" Then
    Cells(1, 1) = Format("6:00:00", "hh:mm:ss")
End If

End Sub
 
Upvote 0
VBA time change

Thanks very much Fat Cat. I know I'm pushing my luck here but could you please do me one more favour!

Instead of one cell, I need to change more cells.
eg: a1, a2:a4, a6, a7:a12

Thanks again Fat Cat
 
Upvote 0
Try;

Code:
Private Sub CommandButton1_Click()
Dim myTime As Date
Dim LastRow As Long
Dim FirstRow As Long
Dim whichCol As String

FirstRow = 1        'set to the row you want to start looking from
LastRow = 13        'set to the row you want to stop looking at
whichCol = "A"        'set to the Column you want to look down

For i = FirstRow To LastRow
    Select Case i
    Case 5, 13      'put in here the rows not to look at
    Case Else       'it will look thru all the other rows
    
        myTime = Range(whichCol & i).Value
        If myTime > "06:00:00" Then
            Range(whichCol & i).Value = Format("6:00:00", "hh:mm:ss")
        End If
    End Select
Next i
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,375
Messages
6,119,168
Members
448,870
Latest member
max_pedreira

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