Loop & concatenate Day/Month/Year

JohnExcel222

New Member
Joined
Dec 19, 2018
Messages
35
Office Version
  1. 365
Hi there,
I struggle with some date data, which visually looks like a date, but it is note clean (sorting ascending order does work as text values...)
In Excel, I replace "." with "-" and it solves the problem: the data get converted to a date format.

I have tried to do the same in VBA, some of the data gets converted properly (like... 23.12.2022, recongnised as date data) and some of the data get converted into 23-12-2022, which is not recognesed as a date format.

In Excel, using the excel date function looks like it solves the problem DATE(year,month,day) (I think it is an Excel 365 function).

Can you please help me to find a solution in VBA .... This below does not work (the code has surely some syntax errors ....



Private Function CleanDate(xYr, xMth, xDay) As String
Dim DTcell As Range
'Dim colDT As String
'Dim xYr, xMth, xDay As String

'For Each DTcell In Range("F2:F416")
xYr = Right(DTcell, 4)
xMth = Mid(DTcell, 4, 2)
xDay = Left(DTcell, 2)

CleanDate = Application.WorksheetFunction.Date(xYr, xMth, xDay)

' Next DTcell
End Function

Sub CleanOutput()
Dim DTcell As Range
For Each DTcell In Range("F2:F416")

DTcell = CleanDate()
Next DTcell

End Sub
 

Attachments

  • Trash_001.jpg
    Trash_001.jpg
    22.5 KB · Views: 8
  • Trash_002.jpg
    Trash_002.jpg
    18.8 KB · Views: 8

Excel Facts

Excel Can Read to You
Customize Quick Access Toolbar. From All Commands, add Speak Cells or Speak Cells on Enter to QAT. Select cells. Press Speak Cells.
See if something like this works.
VBA Code:
Sub CleanOutput()
    Dim WS As Worksheet
    Dim CellRange As Range
    Dim R As Range
    Dim DT As Variant

    Set WS = ActiveSheet

    With WS
        Set CellRange = .Range("F2", .Range("F" & .Rows.Count).End(xlUp))    'range to last cell in column w/data
    End With

    For Each R In CellRange
        DT = R.Value
        Select Case TypeName(DT)
        Case "Date"
        Case "String"
            R.Value = VBA.DateValue(Replace(DT, ".", "-"))
        End Select
    Next R
End Sub
 
Upvote 0
Solution
Hi there, this results in a "Type mismatch error" - Runt time error 13. The code did not work
 

Attachments

  • Capture.JPG
    Capture.JPG
    25.5 KB · Views: 4
Upvote 0
...actually.... some of the cells do not contain a date.... but this: —
I have deleted the rows with — ...and it worked.
How can we modify the code to cope with this ? Thank you for your help
 
Upvote 0
Sub CleanOutput4()
Dim WS As Worksheet
Dim CellRange As Range
Dim R
Dim DT

Set WS = ActiveSheet

With WS
Set CellRange = .Range("F2", .Range("F" & .Rows.Count).End(xlUp)) 'range to last cell in column w/data
End With

On Error Resume Next
For Each R In CellRange
DT = R.Value
Select Case TypeName(DT)
Case "Date"
Case "String"
R.Value = VBA.DateValue(Replace(DT, ".", "-"))
End Select
Next R
End Sub
 
Upvote 0

Forum statistics

Threads
1,223,237
Messages
6,170,930
Members
452,367
Latest member
TePunaBloke

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