In different parts of the world, there are different languages spoken and written. With this, a VBA programmer also faces language related issues while writing a VBA program. Here is a common date related problem solved which converts a date written in MM.DD.YYYY format into Excel standard date.
'This function converts a date from MM.DD.YYYY to system date format
Public Function ConvertToDate(strInputDate As String) As Date
'Variable Declaration
Dim iMonth As Integer
Dim iDay As Integer
Dim lYear As Long
'
'Get month from first 2 digits
If IsNumeric(Left(strInputDate, InStr(1, strInputDate, ".") - 1)) = True Then
iMonth = Left(strInputDate, InStr(1, strInputDate, ".") - 1)
Else
ConvertToDate = Null
Exit Function
End If
'Get day from input date
If IsNumeric(Mid(strInputDate, InStr(1, strInputDate, ".") + 1, 2)) = True Then
iDay = Mid(strInputDate, InStr(1, strInputDate, ".") + 1, 2)
Else
ConvertToDate = Null
Exit Function
End If
'Get year from last 4 digits
If IsNumeric(Right(strInputDate, 4)) = True Then
lYear = Right(strInputDate, 4)
Else
ConvertToDate = Null
Exit Function
End If
'Create a complete date and return the value
ConvertToDate = CDate(iDay & "-" & MonthName(iMonth) & "-" & lYear)
'
End Function
6. You may get the converted date in number format like below
7. To change the date format into the desired format, right-click on the cell and select ‘Format Cells…’
8. In the ‘Number’ tab, click on ‘Date’ category and select the format type you want to display (here I have selected DD-MMM-YYYY format)
9. Done, your date is converted. You can drag the formula to other cells as well
To help you practice this code, we have made this code available through practice file. Click on the below link to download the practice file.
Table of Content How does testing help? How should I test my tool or application? How does Dummy Data Generator tool helps in testing? How do I use this tool? From many years I have…
Working with huge data is always exciting and challenging. From 2007 version onward, Excel is supporting more than a million rows in each worksheet. One of the common problems with huge data is “Duplicates” and the bigger problem is to identify and remove these duplicates. In this article, we will be sharing 4 ways to delete duplicate records from your data.
Excel Add-in helps you to extend the features of Excel application. Using Excel Add-in, you can perform custom actions in Excel such as formatting the data, doing complex calculations which are not possible through Excel formulas, Reading or Writing data in other Excel files and so many more actions.
Video: How to Hide Worksheet in Excel? Hide Sheet in Excel When I was creating an excel dashboard, there were multiple sheets which I used for calculation purpose and never wanted anybody to make any…
What is the Usage of sheet color in Excel? When we prepare a report or a dashboard it is easy to identify or analyze reports with a change of color sheet tabs. Analysts generally give…
Outlook Email Management Tool is an Excel based tool which works with Outlook 2010 or above version and helps you to read, copy or move multiple emails from an Outlook folder and sub-folders on click of a button. The tool also supports extracting attachments from emails as well.