How to Split Date and Time Using VBA in Excel – 2 Methods

Quick View:

Sub Split_Date_and_Time()

Date_with_Time = CDate(InputBox("Enter the Date with Time: "))

Separated_Date = Int(Date_with_Time)
Separated_Time = Date_with_Time - Separated_Date
Display_Date = Format(Separated_Date, "dd/mm/yyyy")
Display_Time = Format(Separated_Time, "hh:mm:ss")
Output = "Date: " + Display_Date + vbNewLine + vbNewLine + "Time: " + Display_Time
MsgBox Output

End Sub

VBA Code to Split Date and Time in Excel Using VBA


Split Date and Time Using Excel VBA 

Step 1: Input Data

  • Enter date with time.

You can specify it inside the code, or use an InputBox.

An InputBox was used here.

Date_with_Time = CDate(InputBox("Enter the Date with Time: "))

Step 2: Separating the Date

The date is the integer, and time is the fractional part of the number.

  • Separate the date using the Int function of VBA:
Separated_Date = Int(Date_with_Time)

Step 3: Separating the Time

  • Subtract the date from the initial input: date with time.
Separated_Time = Date_with_Time - Separated_Date

Step 4 (Optional): Displaying the Outputs

  • Use a Message Box to display date and time (the Format function and the vbNewLine object were used).
Display_Date = Format(Separated_Date, "dd/mm/yyyy")
Display_Time = Format(Separated_Time, "hh:mm:ss")
Output = "Date: " + Display_Date + vbNewLine + vbNewLine + "Time: " + Display_Time
MsgBox Output

 

VBA Code:

Sub Split_Date_and_Time()

Date_with_Time = CDate(InputBox("Enter the Date with Time: "))

Separated_Date = Int(Date_with_Time)

Separated_Time = Date_with_Time - Separated_Date

Display_Date = Format(Separated_Date, "dd/mm/yyyy")
Display_Time = Format(Separated_Time, "hh:mm:ss")
Output = "Date: " + Display_Date + vbNewLine + vbNewLine + "Time: " + Display_Time
MsgBox Output

End Sub

VBA Code to Split Date and Time in Excel Using VBA

Output:

  • Run the code.
  • Enter the date with time in the InputBox. Here, 3/9/2021 5:17:09 AM

Entering Input to Split Date and Time Using Excel VBA

  • Click OK.

You’ll see the split date and time in two lines in a Message Box.


Example 1 – Developing a Macro to Split Date and Time Using Excel VBA

The sample dataset contains the candidates’ names and their interview schedule.

Data Set to Split Date and Time Using Excel VBA

To split the Interview Schedule into date and time:

  • The VBA code will be:
Sub Split_Date_and_Time_Range()

Set Input_Range = Range("C3:C12")
Set Output_Range = Range("D3:E12")

For i = 1 To Input_Range.Rows.Count
    Date_with_Time = Input_Range.Cells(i, 1)
    Separated_Date = Int(Date_with_Time)
    Separated_Time = Date_with_Time - Separated_Date
    Display_Date = Format(Separated_Date, "mm/dd/yyyy")
    Display_Time = Format(Separated_Time, "hh:mm:ss")
    Output_Range.Cells(i, 1) = Display_Date
    Output_Range.Cells(i, 2) = Display_Time
Next i

End Sub

VBA Code to Split Date and Time Using Excel VBA

 Output:

  • Run the code.

It will return Date and Time.


Example 2 – Creating a User-Defined Function to Split Date and Time Using Excel VBA

  • The VBA code will be:
Function SplitDateandTime(Date_with_Time)

Dim Output As Variant
ReDim Output(0, 1)

Separated_Date = Int(Date_with_Time)
Separated_Time = Date_with_Time - Separated_Date

Display_Date = Format(Separated_Date, "mm/dd/yyyy")
Display_Time = Format(Separated_Time, "hh:mm:ss")

Output(0, 0) = Display_Date
Output(0, 1) = Display_Time

SplitDateandTime = Output

End Function

Output:

The code creates the SplitDateandTime function.

  • Select two cells in your worksheet and enter the formula:
=SplitDateandTime(C3)
  • Press ENTER or CTRL + SHIFT + ENTER (for older Excel versions).

This is the output.

Entering Function to Split Date and Time Using Excel VBA

  • Drag down the Fill Handle to see the result in the rest of the cells.


Download Practice Workbook

Download the practice workbook.


<< Go Back to Date and Time | Split | Learn Excel

Get FREE Advanced Excel Exercises with Solutions!
Rifat Hassan
Rifat Hassan

Rifat Hassan, BSc, Electrical and Electronic Engineering, Bangladesh University of Engineering and Technology, has worked with the ExcelDemy project for almost 2 years. Within these 2 years, he has written over 250 articles. He has also conducted a few Boot Camp sessions on effective coding, especially Visual Basic for Applications (VBA). Currently, he is working as a Software Developer to develop and deploy additional add-ins to enhance the customers with a more sophisticated experience with Microsoft Office Suits,... Read Full Bio

2 Comments
  1. hi good day

    suppose my excel data is like 01/01/23 14.20pm means how could i make it 01/01/2023 in one column and 14.20pm in another column. kindly help me.

    one more doubt if i am using text to column also it will come 01/01/23 after that how i can make it 01/01/2023 ,

    • Reply Lutfor Rahman Shimanto
      Lutfor Rahman Shimanto Dec 6, 2023 at 3:50 PM

      Hello GURU

      Thanks for sharing your queries. Your Excel Data is like “01/01/23 14.20 PM” (24-Hour Clock Time does not need AM/PM extensions). You want to store “01/01/23” and “14.20 PM” in another column.

      I am presenting another sub-procedure that will fulfil your requirements by modifying the sub-procedure mentioned in the article.

      Excel VBA Sub-procedure:

      
      Sub AdvancedSplitDateAndTime()
      
          Dim ws As Worksheet
          Dim lastRow As Long
          Dim i As Long
          Dim dateWithTime As Date
          Dim separatedDate As Date
          Dim separatedTime As Date
      
          Set ws = ThisWorkbook.Sheets("Sheet1")
          
          ws.Range("C:C").NumberFormat = "[$-en-US]h:mm AM/PM;@"
          
          lastRow = ws.Cells(ws.Rows.Count, "A").End(xlUp).Row
      
          For i = 1 To lastRow
      
              dateWithTime = ws.Cells(i, 1).Value
      
              separatedDate = DateSerial(Year(dateWithTime), Month(dateWithTime), Day(dateWithTime))
              separatedTime = TimeValue(Format(dateWithTime, "h:mm AM/PM"))
      
              ws.Cells(i, 3).Value = separatedTime
      
              ws.Cells(i, 2).Value = separatedDate
      
          Next i
          
      End Sub
      

      OUTPUT OVERVIEW:

      You have one more doubt when using Text to Column features. Typically, the Text to Column features will display dates in the desired format you mention. You can apply a custom format like “m/d/yyyy” after that if it does not.

      Hopefully, the suggestions and the presented code will help you. Good luck!

      Regards
      Lutfor Rahman Shimanto

Leave a reply

Advanced Excel Exercises with Solutions PDF

 

 

ExcelDemy
Logo