Join a Date and Time into a Single Cell using VBA Macros in Excel

Add to Favorites

In order to combine a cell that has a date with a cell that has a time, using a Macro and VBA in Excel, you must sum the data.

For instance, if you have one cell formatted as a date and another cell formatted as a time and you want to combine them into a single cell with the date and time together, you cannot simply concatenate the data.

If the date is in cell A1 and the time in cell B1 and you want to combine them in cell C1, you must do something like this:

Sub CombineDateTime()

Range("C1").Value = Range("A1").Value + Range("B1").Value

End Sub

Notice the plus sign in between the Range for A1 and B1.

It is easy to think that you should simply concatenate these two values and then put that into a cell but this will not work. Remember that dates and times are stored as numbers in Excel.  This is why you need to sum or add these date and time values in order to get the correct result.

Also, in case Excel does not automatically format the destination cell as you would like, remember to ensure that the correct date/time formatting is being used, whichever you want, as this can dramatically change the visible output of a cell containing a date and time value.

I hope this was helpful!  Remember to share this tutorial and follow us on Google Plus! Smile

 


Downloadable Files: Excel File

Similar Content on TeachExcel
Excel Data Validation - Limit What a User Can Enter into a Cell
Tutorial: Data Validation is a tool in Excel that you can use to limit what a user can enter into a...
How to Use Multiple Functions and Formulas in a Single Cell in Excel
Tutorial: Lets learn how to put multiple functions and formulas in a single cell in Excel in order t...
Show a Single Comment in Excel - Means the Comment is Always Visible, No Hovering Necessary
Macro: This macro in Excel allows you to display or show a particular comment in Excel. This will...
Increment a Date By X Days for Each Row in Excel
Tutorial: How to quickly increment a date by a certain number of days for each entry. Type the first...
Add a Drop Down Menu to a Cell in Excel - Data Validation Macro
Macro: Add a drop down menu or list to a cell in Excel with this free Excel macro. This is a...
Create a Column Chart with a Macro in Excel
Macro: This macro adds a column chart to Excel. This is an easy to use macro that allows you to q...