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:
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