Adding time format in the sentence

aryanaveen

Board Regular
Joined
Jan 5, 2015
Messages
104
Hi Guys

please help me with below code

Basically I am trying to combine values from multiple cells to one sentence.
Range B16 has date, Range B17 is time in "hh:mm" format

when I use below code, time is getting converted in to numbers

Range("B7").Value = "I come on " & Range("B16").Value & " " & Range("B17").Value & " with " & Range("D1").Value

Result what I am getting is "I come on Friday, July 31, 2020 0.6375 with Naveen

Expected result - "I come on Friday, July 31, 2020 02:30 PM with Naveen
 

Excel Facts

When did Power Query debut in Excel?
Although it was an add-in in Excel 2010 & Excel 2013, Power Query became a part of Excel in 2016, in Data, Get & Transform Data.
Try

VBA Code:
Range("B7").Value = "I come on " & Range("B16").Value & " " & Format(Range("B17").Value, "hh:mm AM/PM") & " with " & Range("D1").Value
 
Upvote 0
Yes, I'll come back with that
This would do it ...
VBA Code:
Sub Test1()
  With Range("B7")
    .Value = "I come on " & Range("B16").Value & " " & Format(Range("B17").Value, "hh:mm AM/PM") & " with " & Range("D1").Value
    .Characters(11, InStr(1, .Value, ":") - 5).Font.Bold = True
  End With
End Sub


.. but this more general code might be more useful if you have to adapt.

VBA Code:
Sub Test2()
  Dim sPrefix As String, sSuffix As String
  
  sPrefix = "I come on "
  sSuffix = " with " & Range("D1").Value
  
  With Range("B7")
    .Value = sPrefix & Range("B16").Value & " " & Format(Range("B17").Value, "hh:mm AM/PM") & sSuffix
    .Characters(Len(sPrefix) + 1, InStr(1, .Value, sSuffix) - Len(sPrefix) - 1).Font.Bold = True
  End With
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,914
Messages
6,122,211
Members
449,074
Latest member
cancansova

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