Excel 分析到日期的字符串时出现语法错误

Excel 分析到日期的字符串时出现语法错误,excel,vba,Excel,Vba,我尝试在VB中解析Excel宏中的字符串,如下所示: Sub report_macro() Dim nRow As Long Dim sDueDate As String Dim dueDate As Date For nRow = 5 To 65536 If Not Range("A" & nRow).Value = "" Then sDueDate = Range("G" & nRow).Value

我尝试在VB中解析Excel宏中的字符串,如下所示:

Sub report_macro()

    Dim nRow As Long
    Dim sDueDate As String
    Dim dueDate As Date

    For nRow = 5 To 65536
        If Not Range("A" & nRow).Value = "" Then
            sDueDate = Range("G" & nRow).Value
            dueDate = Date.ParseExact(sDueDate, "yyyy-MM-dd", System.Globalization.DateTimeFormatInfo.InvariantInfo)
            If Range("H" & nRow).Value = 57600 Then

                Range("A" & nRow & ":I" & nRow).Select
                Selection.Font.Bold = True
                With Selection.Interior
                    .Color = 13551615
                End With
            End If
        Else
            Exit For
        End If
    Next nRow

End Sub
我在线路上发现Sytnax错误:

dueDate = Date.ParseExact(sDueDate, "yyyy-MM-dd", System.Globalization.DateTimeFormatInfo.InvariantInfo)

我做错了什么?

Date.ParseExact
是一种VB.NET方法。您正在Excel中运行宏,该宏使用VBA而不是VB.NET()

最好的选择可能是直接将值分配给日期变量(
value
属性实际上是一个变量):

或者,您可以使用


如果您真的想解析它,请查看以下答案:

谢谢。从格式为日期的单元格向日期变量分配日期不是更简单吗?这似乎是一件很常见的事情,所以我认为这很简单。要从某个单元格中为变量指定一个日期,为什么不干脆:Dim DT as Date:If IsDate(myCell),那么DT=myCell
dueDate = Range("G" & nRow).Value