Formatting Correct Date Thread VBA

HTMLGhozt

New Member
Joined
Jun 4, 2015
Messages
38
Hello Again!

I'm using Excel 2010

I have a range of dates which are 2012012 (DMMYYYY) and I need them to be in the DD/MM/YYYY format. I have little VBA experience but simply formatting the cells to date provides me with 9/12/7408 from 2012012 or simply hashtags.

I don't have any snippets of code which relate to formatting, but here's what I'm using to transfer the information to sheet.

Code:
Sub Carrier_EFF_DT(ws, ws2, aCell, rng, col, LastRow, colName)


'This statement finds the EFF_DT column in the carrier report.
    With ws
        Set aCell = .Range("A1:ZZ1").Find(What:="EFF_DT", LookIn:=xlValues, LookAt:=xlWhole, _
                    MatchCase:=False, SearchFormat:=False)


        'If the EFF_DT column is found, then:
        If Not aCell Is Nothing Then
            col = aCell.Column
            colName = Split(.Cells(, col).Address, "$")(1)
            
            LastRow = .Range(colName & .Rows.Count).End(xlUp).Row
            
            'This is the range of rows in the column specified (EFF_DT).
            Set rng = .Range(colName & "2:" & colName & LastRow)
            
            Debug.Print rng.Address
            
            'This copies your range, and pastes it at H4, or the Effective Date column in the Master Spreadsheet.
            rng.Copy Destination:=ws2.Range("H4")


        'If the macro was unable to find the EFF_DT column, then it will alert you.
        Else
            MsgBox "Effective date not found!"
        End If
        
    End With
    
End Sub
 

Excel Facts

Last used cell?
Press Ctrl+End to move to what Excel thinks is the last used cell.
Try this manual procedure... select the column with the values you want to convert to dates, call up the "Text To Columns" dialog box (Data tab, Data Tools panel, Text To Columns button)... click the Next button twice and then select the Date option button (upper left of dialog box) and then select DMY from the drop down next to it... click the Finish button. Your values should now be real dates.
 
Upvote 0
Tried the manual procedure! Out of the range cells which had two digit months formatted correctly, the rest did not format at all.

Also I made a mistake the date format is MDDYYYY, and I need it to be MM/DD/YYYY.
 
Last edited:
Upvote 0
Out of the range cells which had two digit months formatted correctly, the rest did not format at all.
:confused: You said "I have a range of dates which are 2012012 (DMMYYYY)"... the MM means that all your dates would be two digits with a leading zero for those months that are single digits. Are you saying that I not true? If so, does that mean today's date could be entered like this...

462015

I hope not, because if so, how would you ever know whether 1232015 was January 23rd or December 3rd?
 
Upvote 0
Unfortunately, That is what I am saying. Today's date would be 4062015, January 23rd of this year would be 1232015 and December 3rd 12032015. The format is actually MDDYYYY.
 
Upvote 0
You could try adding the below directly after line "rng.Copy Destination:=ws2.Range("H4")" in your existing macro

Code:
With ws2.Range("H4").Resize(rng.Rows.Count)
    .Cells = Evaluate("=INDEX(TEXT(" & .Address & ",""00\/00\/0000"")+0,0)")
End With
 
Upvote 0
It changed the format from 2012012 to 40940. Which I don't even understand.

You could try adding the below directly after line "rng.Copy Destination:=ws2.Range("H4")" in your existing macro

Code:
With ws2.Range("H4").Resize(rng.Rows.Count)
    .Cells = Evaluate("=INDEX(TEXT(" & .Address & ",""00\/00\/0000"")+0,0)")
End With
 
Upvote 0
Did a little research found out that was Excel's way of storing dates I am now writing a code to reformat the column.
 
Upvote 0
Did a little research found out that was Excel's way of storing dates I am now writing a code to reformat the column.

Correct, for formatting try:

Rich (BB code):
With ws2.Range("H4").Resize(Rng.Rows.Count)
    .Cells = Evaluate("=INDEX(TEXT(" & .Address & ",""00\/00\/0000"")+0,0)")
    .NumberFormat = "MM/DD/YYYY"
End With
 
Upvote 0

Forum statistics

Threads
1,214,641
Messages
6,120,694
Members
448,979
Latest member
DET4492

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