VBA-code "Time" and format tt:mm

blaksnm

Well-known Member
Joined
Dec 15, 2009
Messages
554
Office Version
  1. 365
Platform
  1. Windows
Hi guys
I have this routine;

Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
'Modified 2/26/2020 10:33:36 PM EST
If Target.Column = 11 Or 12 Then
Cancel = True
Target.Value = Time
End If
End Sub

The routine "works" for other columns than the expected 11 og 12 - why?
(innstead of target.column I would use some kind of "target.range" (ex for range("M4:N10") - how is this done?
I should have ajusted the result of the value to only be TT:MM - not TT:MM:SS - how do I do that?
Thanks for help :)
 

Excel Facts

Fastest way to copy a worksheet?
Hold down the Ctrl key while dragging tab for Sheet1 to the right. Excel will make a copy of the worksheet.
How about
VBA Code:
Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
   If Not Intersect(Target, Range("M4:N10")) Is Nothing Then
      Cancel = True
      Target.Value = Time
      Target.NumberFormat = "hh:mm"
   End If
End Sub
 
Upvote 0
:)
Thanks for tips, but the value is still tt:mm:ss - the rest is working all right :)
 
Upvote 0
Time is measured in hh:mm:ss so I don't understand. The code I suggested should format it to just show Hours & minutes.
You may need to adjust the number format for your locale.
 
Upvote 0
Well
I have at "test" in a formula that compares to time-values.
When theese two values are not alike the formula responds a "message"
I dont want the values to be evalueted on more than tt:mm (Seconds are not wanted)
 
Upvote 0
In that case use
VBA Code:
Target.Value = Int(Time * 1440) / 1440
 
Upvote 0
You're welcome & thanks for the feedback
 
Upvote 0

Forum statistics

Threads
1,215,465
Messages
6,124,982
Members
449,201
Latest member
Lunzwe73

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