Two time variables in a single cell

Luke777

Board Regular
Joined
Aug 10, 2020
Messages
246
Office Version
  1. 365
Platform
  1. Windows
Hi all,

I have two variables called StartTime and EndTime. Each can be displayed as a time in a given cell.

I would like to combine both variables to create a new variable called StartEndTime which can then be displayed in a given cell with the time format of "6:30-15:00"

So far my efforts have lead to the decimals of these numbers being displayed.. halfway there lol

Any ideas?

Thanks in advance!
 

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.
Assuming start time is in A2 and end time in b2:
Excel Formula:
=TEXT(A2,"hh:mm") & " - " & TEXT(B2,"hh.mm")
 
Upvote 0
try this:
VBA Code:
Sub test()
St = Cells(2, 1)
et = Cells(2, 2)
 sthr = Hour(St)
 stmn = Minute(St)
 ethr = Hour(et)
 etmn = Minute(et)
 MsgBox (sthr & ":" & stmn) & "-" & (ethr & ":" & etmn)
 
  
End Sub
 
Upvote 0
try this:
VBA Code:
Sub test()
St = Cells(2, 1)
et = Cells(2, 2)
 sthr = Hour(St)
 stmn = Minute(St)
 ethr = Hour(et)
 etmn = Minute(et)
 MsgBox (sthr & ":" & stmn) & "-" & (ethr & ":" & etmn)
 
 
End Sub
This seems to work 99% fine - the only issue I'm having is "06:30:00 and 15:00:00" (test time) are joining together as "6:30-15:0". Annoyingly lacking a zero lol
 
Upvote 0
May be
VBA Code:
Sub test()
    StartTime = Cells(2, 1)
    EndTime = Cells(2, 2)
    StartEndTime = Format(StartTime, "h:mm") & "-" & Format(EndTime, "h:mm")
    MsgBox StartEndTime
  
End Sub
"h:mm" Or "h:mm:ss"
 
Upvote 0
Solution

Forum statistics

Threads
1,215,684
Messages
6,126,199
Members
449,298
Latest member
Jest

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