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

Excel motto
Not everything I do at work revolves around Excel. Only the fun parts.

Fat Cat

Active Member
Joined
Nov 5, 2004
Messages
336
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
 

BOGIA

Active Member
Joined
May 23, 2005
Messages
266
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
 

Fat Cat

Active Member
Joined
Nov 5, 2004
Messages
336
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
 

BOGIA

Active Member
Joined
May 23, 2005
Messages
266
Thanks alots Fat Cat. It works. You are the champ...
Thanks again
 
Master Excel Bundle

Excel contains over 450 functions, with more added every year. That’s a huge number, so where should you start? Right here with this bundle.

Forum statistics

Threads
1,163,632
Messages
5,832,772
Members
430,166
Latest member
Nige Leeming

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
Top