bottom
Great ExcelTips!
         
Your e-mail address is safe!
Close Note

Tips.Net > ExcelTips Home > Data Entry > Entering Dates Without Separators

Entering Dates Without Separators

Summary: When doing data entry into a worksheet, you might want to enter dates without the need to type the separators that are normally part of those dates. Here’s a discussion on how this can be done in Excel, along with the benefits and drawbacks of using such an approach. (This tip works with Microsoft Excel 97, Excel 2000, Excel 2002, Excel 2003, and Excel 2007.)

Different people enter data in different ways. When you enter information into a cell, Excel tries to figure out what type of information you are entering. If you enter a number such as 08242008, Excel assumes you are entering a numeric value, and treats it accordingly. What if the number you enter actually is a date, without any separators? Can Excel understand what you are entering?

Unfortunately Excel cannot. Why? Because you have given it no indication that this should be a date. (Excel keys on separators, not on numeric values.) If you or your data entry people cannot change their input habits so that separators are also entered, then you will need some sort of a workaround to convert the entered information to an actual date value.

Your first thought might be that you could use a custom format to display the information. Consider the following custom format:

##"/"##"/"####

This format would display the number 08152008 as 8/15/2008. The only problem is that it only changes the display of the number—if you want to use the date as a real Excel date, you cannot do so because you haven't converted the value into something that Excel recognizes as a date.

If the values input were very consistent in their format, and if they were input as text instead of as numeric values, then there is an easy way you can convert them to dates. By very consistent, I mean that the input always used two digits for the month, two for the day, and four for the year. In addition, the cells containing the values must be formatted as text. In this instance, you can follow these steps:

  1. Select the column of dates.
  2. Make sure there is nothing in the column just to the right of the dates.
  3. Choose Text to Columns from the Data menu. (In Excel 2007, select Text to Columns from the Data tab of the ribbon.) Excel displays the Convert Text to Columns Wizard. (Click here to see a related figure.)
  4. Select the Fixed Width option, then click on Next.
  5. Click on Next again.
  6. In the Column Data Format area, choose Date.
  7. Select the range in the Destination box, then in the worksheet click the cell just to the right of the first value you selected in step 1.
  8. Click on Finish.

If all went well, Excel should have parsed the text values as dates, and you can delete the original column. If this did not work, then it means that either the original values were not formatted as text, or eight digits were not used to enter all the dates.

Another possible solution is to use a formula to convert the entered values into actual dates. The following is one such formula:

=DATE(RIGHT(A1,4),LEFT(A1,IF(LEN(A1) = 8,2,1)),LEFT(RIGHT(A1,6),2))

This formula assumes that the entered date (the one without separators) is in cell A1. The formula will work with either seven- or eight-digit dates.

If you prefer custom functions, you can create one in VBA that examines the data being passed, converts it to a date/time format, and then returns the result. The following function is very versatile in this regard; it will work with both American and European date formats:

Function DateTime(dblDateTime As Double, _
  Optional bAmerican As Boolean = True)
'Converts Date and time "number" without
'delimiters into an excel serialdate (which
'can then be formatted with the Excel
'date/time formats)

'If optional argument is TRUE (or missing),
'function assumes value is of form:
'   [m]mddyyyy.hhmm  (leading "0" not required)

'If optional argument is FALSE, function
'assumes value is of form:
'   [d]dmmyyyy.hhmm  (leading "0" not required)

    Dim iYear As Integer
    Dim iMonth As Integer
    Dim iDay As Integer
    Dim iHour As Integer
    Dim iMin As Integer

    iYear = Int((dblDateTime / 10000 - _
      Int(dblDateTime / 10000)) * 10000)
    iDay = Int((dblDateTime / 1000000 - _
      Int(dblDateTime / 1000000)) * 100)
    iMonth = Int((dblDateTime / 1000000))
    iHour = Int((dblDateTime - Int(dblDateTime)) * 100)
    iMin = Int((dblDateTime * 100 - _
      Int(dblDateTime * 100)) * 100 + 0.5)

    If bAmerican Then
        DateTime = DateSerial(iYear, iMonth, iDay)
    Else
        DateTime = DateSerial(iYear, iDay, iMonth)
    End If

    DateTime = DateTime + (iHour + iMin / 60) / 24
End Function

This macro function assumes that the data being passed to it is a numeric value, as would normally happen when inputting dates without separators. (Refer back to the logic on this at the beginning of the tip.)

As you can tell, there are a number of workarounds, but none of them is as simple as just entering separators when entering the dates. If training yourself or your data input people to do this is difficult, you might consider setting up some data validation rules for the input cells. These rules can check to make sure that you are entering information using a specific format (such as a date with separators), and stop you if you are not. (How you create data validation rules has been covered in other issues of ExcelTips.)

Tip #2039 applies to Microsoft Excel versions: 97 | 2000 | 2002 | 2003 | 2007


PivotTables Got You Perplexed? PivotTables for the Faint of Heart shows how you can start using Excel's PivotTable tool right away to spin your data into gold! You discover how easy it really is to crunch the numbers you need to crunch. Uncover the power of creating PivotTables, editing them, formatting them, customizing them, and much more.
 
Check out PivotTables for the Faint of Heart today!

Helpful Links

Ask an Excel Question
Make a Comment

Tips.Net Home

ExcelTips FAQ
ExcelTips Premium

Learn Access Now

Bugs and Pests Tips
ExcelTips
Family Tips
Health Tips
Home Tips
Organizing Tips
WordTips

Advertise on the
ExcelTips Site

 

Great Info!

Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your e-mail address and click "Subscribe."
     
(Your e-mail address will never be shared with anyone, ever.)