VBA CODE TO CONVERT TO PROPER DATE

NeelsBorstlap

New Member
Joined
Jul 23, 2010
Messages
25
Hi

I get an excel file from 3rd party with the dates in a column like this:
Total Variable InsuranceFirst Instalment DateFinal Payment Due DateLast Receipt Date
0.0015/02/202115/02/202115/02/2021
0.0012/02/202112/02/202115/02/2021
0.0019/02/202119/04/202123/04/2021
0.0019/02/202119/02/202119/02/2021
I want to use VBA code that will convert the data in the columns with the dates info in it to look like this correct date format:

15-Jan-21
12-Jan-21
19-Jan-21
19-Jan-21

and another file from someone else with date in a column like this:
Inst.CycleInst. DtTrack.Amount
1Monthly2021/12/10 Fri7 Day3,167.55
1Monthly2021/12/11 Sat7 Day8,470.93
2Monthly2021/12/12 Sun7 Day3,370.61

I want to use VBA code that will convert the data in the columns with the dates info in it to look like this correct date format:
10-Jan-21
11-Jan-21
12-Jan-21

I am not good with code and most of the time only record the macro but can net get this to work.
Hope someone can help me.
 
Please see post 8, your mini sheet doesn't provide the necessary information for me to write your code.
 
Upvote 0

Excel Facts

Can Excel fill bagel flavors?
You can teach Excel a new custom list. Type the list in cells, File, Options, Advanced, Edit Custom Lists, Import, OK
Your mini sheet is not a true example of your data set.

Without knowing exactly what columns the dates are in means the code cannot be written.

Unless you wanted to say select the column manually , then run the code on the selected column.
 
Upvote 0
This should do the job, please test it out and let me know
VBA Code:
Dim Dates As Date
For i = 2 To 5
Dates = CDate(ThisWorkbook.Worksheets(1).Range("A" & i))
NewDates = Format(Dates, "d-mmm-yy")
ThisWorkbook.Worksheets(1).Range("B" & i) = NewDates
Next
 
Upvote 0
It can be done much simpler and without wasting time looping, we're just waiting for the OP to clarify the correct ranges.
I am sure it can and looking forward to seeing what the better solution as I am trying to learn more and more myself well helping as much as I know.
 
Upvote 0
It would be nice to know the Regional Settings etc.
Formulas, Data Text to Columns or VBA can provide the solution. I tested the suggestions; system Regional Settings are dd-mm-yy.
You can custom format the dates Control 1 Numbers Custom and/or edit the VBA.
The 2 subs converted a copy of A2:A5 and C2:C5; see A9:A12 and C9:C12.
Check the list of shortcut keys Apply the Date format using day, month, and year Ctrl-Shift-#

Another way is to copy a blank cell and the edit paste special Add; then format to your preference.

Date and Time.xlsm
ABCD
1BeforeAfterBeforeAfter
215-02-2115-Feb-212021/12/10 Fri10-Dec-21
312-02-2112-Feb-212021/12/10 Fri10-Dec-21
419-04-2119-Apr-212021/12/11 Sat11-Dec-21
519-02-2119-Feb-212021/12/10 Fri10-Dec-21
6
7VBA
8ConvertTextToDates11aConvertToDate11b()
915-Feb-2110-Dec-21
1012-Feb-2110-Dec-21
1119-Apr-2111-Dec-21
1219-Feb-2110-Dec-21
24a
Cell Formulas
RangeFormula
B2:B5B2=A2+0
D2:D5D2=--LEFT(C2,10)


Try Data TextToColumns with macro recorder and then edit the code.

VBA Code:
Sub ConvertTextToDates11a()
' text like system date 19-02-2021 to dates
' edit format to your preference

With Selection
.NumberFormat = "d-mmm-yy"
.Value = .Value
End With

End Sub

Sub ConvertToDate11b()
' text like 2021/12/10 Fri convert to date
' edit format to your preference

    With Selection
      .TextToColumns DataType:=xlDelimited, _
        TextQualifier:=xlDoubleQuote, ConsecutiveDelimiter:=1, Tab:=1, _
        Semicolon:=0, Comma:=0, Space:=1, Other:=0, FieldInfo _
        :=Array(Array(1, 5), Array(2, 9)), TrailingMinusNumbers:=1
      .NumberFormat = "d-mmm-yy"
    End With
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,818
Messages
6,181,152
Members
453,021
Latest member
Justyna P

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