Error on short date VBA

Zerrets

New Member
Joined
Jan 24, 2020
Messages
25
Office Version
  1. 2016
Platform
  1. Windows
Hi,
i have an issue when changing some dates into short date in vba, the macro changes some dates into the correct format which is DD/MM/YYYY without the hours, minutes and seconds, and it leaves me some dates withe the hours, minutes and seconds, i made a quick fix which worked partialy but now it modifies the data that have the HH:MM:SS like MM/DD/YYYY, my quick fix changes the format but not the real value, i'll leave an example below:

What you see in the cell 13/04/2020
What you see when you click in the cell 04/13/2020

Hope you can help me, also i'll put my code here:

VBA Code:
Option Explicit
Sub REP_DET()

    Application.ScreenUpdating = False
    Dim mPath As String: mPath = GetFolder
    mPath = mPath & "\"

    Dim iFile As String
    iFile = Dir(mPath & "*.txt")

    Dim wb As Workbook
    Dim ws As Worksheet

    Do While iFile <> ""
        Set wb = Workbooks.Add
        Set ws = wb.Sheets(1)
        'ws.Name = iFile

        With ws.QueryTables.Add(Connection:="TEXT;" & _
            mPath & iFile, Destination:=ws.Range("$A$1"))
            .AdjustColumnWidth = True: .TextFileConsecutiveDelimiter = False
            .TextFileTabDelimiter = False: .TextFileSemicolonDelimiter = False
            .TextFileCommaDelimiter = False: .TextFileSpaceDelimiter = False
            .TextFileDecimalSeparator = ".": .TextFileThousandsSeparator = ","
            .Refresh BackgroundQuery:=False
        End With

        'Do the first parse
        If ws.UsedRange.Columns.Count = 1 Then _
            ws.UsedRange.TextToColumns _
            Destination:=ws.Range("A1"), _
              DataType:=xlDelimited, _
              Tab:=False, _
              Semicolon:=False, _
              Comma:=False, _
              Space:=False, _
              other:=True, _
              OtherChar:="|", _
              FieldInfo:=Array(Array(1, xlTextFormat))
              
        Dim arr As Variant: arr = ws.UsedRange.Value
        Dim i As Long
        For i = 2 To UBound(arr)
            arr(i, 4) = FechaConSinHora(arr(i, 4))
            arr(i, 5) = FechaConSinHora(arr(i, 5))
        Next i
        ws.UsedRange.Value = arr
        ws.Range("D:E").NumberFormat = "dd/mm/yyyy"
        

        'Dim a As Range
        'Set a = Range("A1:J1048576")
        'On Error Resume Next
        'For Each a In ActiveSheet.UsedRange
        'With a
        '.Value = WorksheetFunction.Trim(.Value)
        'End With
        'Next a
        
        Dim iFirstLetterPosition As Integer
        Dim c As Range
        Dim sTemp As String
        For Each c In Range("F2:F1048576")
        If Len(c) > 0 Then
        iFirstLetterPosition = Evaluate("=MATCH(TRUE,NOT(ISNUMBER(1*MID(" & c.Address & ",ROW($1:$20),1))),0)")
        sTemp = Left(c, iFirstLetterPosition - 1) 'get the leading numbers
        sTemp = Format(sTemp, "00000") 'format the numbers
        sTemp = sTemp & Mid(c, iFirstLetterPosition, Len(c)) 'concatenate the remainder of the string
        c.NumberFormat = "@"
        c.Value = sTemp
        End If
        Next
        
        Dim iFirstLetterPositionD As Integer
        Dim d As Range
        Dim sTempD As String
        For Each d In Range("G2:G1048576")
        If Len(d) > 0 Then
        iFirstLetterPositionD = Evaluate("=MATCH(TRUE,NOT(ISNUMBER(1*MID(" & d.Address & ",ROW($1:$20),1))),0)")
        sTempD = Left(d, iFirstLetterPositionD - 1) 'get the leading numbers
        sTempD = Format(sTempD, "0000000") 'format the numbers
        sTempD = sTempD & Mid(d, iFirstLetterPositionD, Len(d)) 'concatenate the remainder of the string
        d.NumberFormat = "@"
        d.Value = sTempD
        End If
        Next
        
        Range("A1:J1").Select
        With Selection.Interior
        .Pattern = xlSolid
        .PatternColorIndex = xlAutomatic
        .Color = 16711680
        .TintAndShade = 0
        .PatternTintAndShade = 0
    End With
    
    Range("A1:J1").Select
    With Selection.Font
        .ThemeColor = xlThemeColorDark1
        .TintAndShade = 0
    End With
    
    With Selection.Borders(xlEdgeLeft)
        .LineStyle = xlContinuous
        .ColorIndex = 0
        .TintAndShade = 0
        .Weight = xlThin
    End With
    With Selection.Borders(xlEdgeTop)
        .LineStyle = xlContinuous
        .ColorIndex = 0
        .TintAndShade = 0
        .Weight = xlThin
    End With
    With Selection.Borders(xlEdgeBottom)
        .LineStyle = xlContinuous
        .ColorIndex = 0
        .TintAndShade = 0
        .Weight = xlThin
    End With
    With Selection.Borders(xlEdgeRight)
        .LineStyle = xlContinuous
        .ColorIndex = 0
        .TintAndShade = 0
        .Weight = xlThin
    End With
    With Selection.Borders(xlInsideVertical)
        .LineStyle = xlContinuous
        .ColorIndex = 0
        .TintAndShade = 0
        .Weight = xlThin
    End With
    With Selection.Borders(xlInsideHorizontal)
        .LineStyle = xlContinuous
        .ColorIndex = 0
        .TintAndShade = 0
        .Weight = xlThin
    End With

        iFile = Dir
        
    Loop

    Application.ScreenUpdating = True
    

End Sub
Private Function GetFolder() As String
    Dim fldr As FileDialog
    Dim sItem As String
    Set fldr = Application.FileDialog(msoFileDialogFolderPicker)
    With fldr
        .Title = "Elige una carpeta"
        .AllowMultiSelect = False
        .InitialFileName = ThisWorkbook.Path
        If .Show <> -1 Then GoTo NextCode
        sItem = .SelectedItems(1)
    End With
NextCode:
    GetFolder = sItem
    Set fldr = Nothing
End Function
Private Function FechaConSinHora(arr As Variant) As Date
    'If IsDate(arr) Or IsNumeric(arr) Then
     '   FechaConSinHora = CDate(Left(arr, 10))
    'Else
        FechaConSinHora = CDate(Left(arr, 10))
    'End If

End Function
 

Excel Facts

Excel motto
Not everything I do at work revolves around Excel. Only the fun parts.
Please give examples of the problems that you describe above. Give five examples of how the data starts out, how you expect it to end up, and how it actually ends up. Include both examples of where the code works as expected and of course when it does not work as expected.

Do you realize that you included a great deal of code that is completely irrelevant to your problem?
 
Upvote 0

Forum statistics

Threads
1,214,599
Messages
6,120,453
Members
448,967
Latest member
grijken

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