Translate Formula to VBA

Livin404

Well-known Member
Joined
Jan 7, 2019
Messages
743
Office Version
  1. 365
  2. 2019
Platform
  1. Windows
Greetings, I have times in Column D and would like to add 20 minutes to that post them to Column E. Not every cell in Column D contains a time so I'm also trying to avoid value errors. I know the code
Excel Formula:
=D7+20/1440
works fine, but I need something in VBA to match only times in Column D. Column D will remain the same, Column E will have that time with 20 minutes added to it. Thank you,
 

Excel Facts

Format cells as time
Select range and press Ctrl+Shift+2 to format cells as time. (Shift 2 is the @ sign).
Do you want this to happen automatically when you enter a Time in column D.
Or do you want the script to run when you press a button?

Show me a example of how you put time in column D
Do you do it with a script or manually.
 
Upvote 0
Do you want this to happen automatically when you enter a Time in column D.
Or do you want the script to run when you press a button?

Show me a example of how you put time in column D
Do you do it with a script or manually.
Thank you for the prompt response. It will definitely happen with a button. It will happen right before the following VBA, and the VBA below it will need to include the new time in Column D.

Time.JPG



VBA Code:
Sub twenty_four_hour_Clock()
   With Range("D1", Range("D" & Rows.Count).End(xlUp))
      .NumberFormat = "@"
      .Value = Evaluate(Replace("if(@="""","""",if(isnumber(@),text(mod(@,1),""hhmm""),@))", "@", .Address))
   End With
End Sub
 
Upvote 0
Do you want this to happen automatically when you enter a Time in column D.
Or do you want the script to run when you press a button?

Show me a example of how you put time in column D
Do you do it with a script or manually.
Then after the VBA shown above it will look like this.



Block to Dep.JPG
 
Upvote 0
You can use this for D1
VBA Code:
Sub twenty_four_hour_Clock()
 Dim x As Long
   With Range("D1", Range("D" & Rows.Count).End(xlUp))
      .NumberFormat = "@"
      .Value = Evaluate(Replace("if(@="""","""",if(isnumber(@),text(mod(@,1),""hhmm""),@))", "@", .Address))
   End With
     If IsNumeric(Range("D1").Value * 1) And Application.WorksheetFunction.IsText(Range("D1").Value) Then
       x = Right(Range("D1"), 2) * 1
        If x > 39 Then
          Range("E1").Value = Range("D1").Value * 1 + 60
         Else
          Range("E1").Value = Range("D1").Value * 1 + 20
        End If
      End If
   
End Sub
And if for range I think you can expand it.
 
Upvote 0

Forum statistics

Threads
1,214,780
Messages
6,121,522
Members
449,037
Latest member
tmmotairi

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