简体   繁体   中英

VBA - Date Format “mm/dd/yyyy”

My excel file format for date is Mar 30 2016 10:12:27:396AM i want to change the format to mm/dd/yyyy . but i'm having an error on my code..

Run-time error '13': Type mismatch

this is the line i got the error SecondDate = Trim(Sheet2.Range("B" & RowNo).Value)

Note : The data from Column A and B are dates

My Code:

Dim Result, RowNo As Long
Dim FirstDate As Variant
Dim SecondDate As Variant

RowNo = 2

    FirstDate = Trim(Sheet2.Range("A" & RowNo).Value)
    SecondDate = Trim(Sheet2.Range("B" & RowNo).Value)
    FirstDate = Format(FirstDate, "mm/dd/yyyy")
    SecondDate = Format(SecondDate, "mm/dd/yyyy")

    Do Until Sheet2.Cells(RowNo, 1) = ""

    FirstDate = Sheet2.Cells(RowNo, 1)
    SecondDate = Sheet2.Cells(RowNo, 2)

        If DateDiff("d", FirstDate, SecondDate) >= 15 Then
        Sheet2.Cells(RowNo, 3) = "1"
        Sheet2.Cells(RowNo, 4) = "2"

    End If

   RowNo = RowNo + 1
   Loop

i do feel like im in the right path..am i missing something?

Update : My script will get the data from Column A and Column B and using the DateDiff function i will subtract both columns if the difference is <15 tag it from Column C and D as 1, 2

note:

  • i only subtract the days from both columns.. but is it possible to subtract both days and years?
  • it's working if i use the format mm/dd/yyyy in the excel but if i use other format i got the error mismatch

Updated 2 :

       Column A            | Column B                   | Column C
Mar 1  2016 10:12:27:396AM | Mar 30 2016 10:12:27:396AM |
Mar 1  2016 10:12:27:396AM | Mar 30 2016 10:12:27:396AM |

in my file this is the date format, if you try this as sample data it won't work maybe because VBA doesnt recognize this as dateformat?

Perhaps this will address what you are trying to do.

Test Input:

在此处输入图片说明

Test Output:

在此处输入图片说明

Code:

Sub DateStuff()
    Dim FirstDate As Range, SecondDate As Range
    Dim RowNo As Long

    Set FirstDate = Sheet4.Range("A1")
    Set SecondDate = Sheet4.Range("A2")

    FirstDate.NumberFormat = "mm/dd/yyyy"
    SecondDate.NumberFormat = "mm/dd/yyyy"

    RowNo = 2
    Do Until Sheet4.Cells(RowNo, 1) = ""
        Set FirstDate = Sheet4.Cells(RowNo, 1)
        Set SecondDate = Sheet4.Cells(RowNo, 2)
        If IsDate(FirstDate.Value) And IsDate(SecondDate.Value) Then
            If DateDiff("d", FirstDate, SecondDate) >= 15 Then
                Sheet4.Cells(RowNo, 3) = 1
                Sheet4.Cells(RowNo, 4) = 2
            End If
        End If
        RowNo = RowNo + 1
    Loop
End Sub

Please spend a few minutes to understand this code:

  • FirstDate and SecondDate are Range data types, not Date or Variant. The data in the cells they point to are Date or something else.

  • When setting the format, it sets how it is displayed on the worksheet. It is not formatting a character string in VBA.

  • Before making the DateDiff call, ensure both FirstDate and SecondDate really contain Date datatypes in the cells, this will prevent many errors.

  • Operations on Date datatypes using Date functions are NOT dependent on formatting. They use the internal Date type. Formatting ONLY affects how a date is converted to a string and/or displayed on the worksheets.

You asked about checking year instead of day. There are built in VBA functions (eg Year, Month, etc.) that will help with this.

As to your update to your question:

I am not sure where you are getting your time codes from, but Excel does not recognize :000 for milliseconds. It uses .000 . See below ...

在此处输入图片说明

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