简体   繁体   中英

EXCEL VBA - Date formatting

Edited) is this right? I need to do the same thign for column P. Should I do another for loop?

Dim i As Long
        For i = 1 To Rows.Count
        If Len(Cells(i, "Q").Value) <= 4 Then
        Cells(i, 1).NumberFormat = "01/01/yyyy"
        Else: Cells(i, "Q").NumberFormat = "MM/DD/YYYY"
        End If
        Next i

When I import excel file to access, the column that has either the years(19xx) or a full date don't display properly after imported. The years seem fine but full date is changed to a random number like 39213, etc. So I tried to format the whole column to text and export to access, the same thing happens. What should I do? The original column is in 'general' format

01/01/1962
01/01/1966
01/01/1956
  1964
01/01/1943
01/01/1943
01/01/1964
  1964
01/01/1972
01/01/1948
01/01/1961
01/01/1953
01/01/1961
01/01/1963
01/01/1963
01/01/1973
  1960
01/01/1956
01/01/1940
  1958
1958
1955
01/01/1948
01/01/1948
01/01/1970
  1959
  1964
01/01/1975
  1966

This becomes

22647
24108
20455
1964
15707
15707
19
23377
1964
26299
17533
22282
19360
22282
23012
23012
26665
1960
20455
14611
1958
1958
1955
17533
17533
25569
1959
1964
27395
1966

And the latter column is in 'Text' format. I dont know what is wrong

EDITED TO CORRECT FORMAT ISSUE!!

Depending on which approach you want to take (A. Fix in Excel; B. Fix in Access), your solution will vary. To use your code, I modified slightly.

WARNING!! If only a year, my code will destroy content of column 1!! Change to suit your needs.

Sub Convert_Dates()
Dim lLastRow    As Long
Dim i           As Long

' Find last row
lLastRow = Cells.Find(What:="*", After:=Range("A1"), SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
' Loop until last row
For i = 1 To lLastRow
    If Len(Cells(i, "Q").value) <= 4 Then           ' IF only a year found
        Cells(i, "Q") = "01/01/" & Cells(i, "Q")    ' Change date value from yyyy to mm/dd/yyyy
        Cells(i, "Q").NumberFormat = "mm/dm/yyyy"   ' Set format
    Else
        Cells(i, "Q").NumberFormat = "mm/dd/yyyy"   ' Else just set format.
    End If
Next i
End Sub

I think the date format should be "MM/DD/YYYY" across all the rows of "Q". I have modified your code slightly.

Sub test1()
            k = Cells(Rows.Count, "Q").End(xlUp).row
            Dim i As Long
            For i = 1 To k
            If Len(Cells(i, "Q").Value) <= 4 Then
            Cells(i, "Q").Value = "01/01/" & Cells(i, "Q").Value
            Cells(i, "Q").NumberFormat = "MM/DD/YYYY"
            Else: Cells(i, "Q").NumberFormat = "MM/DD/YYYY"
            End If
            Next i
End Sub

After executing all the dates in the column "Q" will be formatted to "MM/DD/YYYY"

And if you want to keep the formatting year with 4 digits in the Q column and format that year separately in column 1, then use the below code

Sub test1()
            k = Cells(Rows.Count, "Q").End(xlUp).row
            Dim i As Long
            For i = 1 To k
            If Len(Cells(i, "Q").Value) <= 4 Then
            Cells(i, 1).Value = "01/01/" & Cells(i, "Q").Value
            Cells(i, 1).NumberFormat = "MM/DD/YYYY"
            Else: Cells(i, "Q").NumberFormat = "MM/DD/YYYY"
            End If
            Next i
End Sub

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM