解决日期类型不匹配问题

时间:2014-05-08 14:25:35

标签: date excel-vba vba excel

我正在尝试运行下面的代码。它运行完全正常,直到我尝试添加日期参数。我收到类型不匹配错误。我试过#和' (单引号)有相同的错误。 locals窗口将两个日期变量显示为variant / string,我认为是正确的。任何帮助,将不胜感激!有些东西很容易搞清楚,有些让我头疼。

Sub Macro2()
'
' Macro2 Macro
'

'
Dim b_date
Dim e_date
b_date = Format(Range("Beg_Date"), "mm/dd/yyyy")
e_date = Format(Range("End_Date"), "mm/dd/yyyy")
    With ActiveSheet.ListObjects.Add(SourceType:=0, Source:=Array( _
        "OLEDB;Provider=SQLOLEDB.1;Password=********;Persist Security Info=True;User     ID=SqlLinkServer;Initial Catalog=SPFT;Data Source=001MSDSQL" _
        , _
        "01;Use Procedure for Prepare=1;Auto Translate=True;Packet Size=4096;Workstation ID=001MSDSTS02;Use Encryption for Data=False;Tag with column collation when possible=False"), _
        Destination:=Range("$a$7")).QueryTable
        .CommandType = xlCmdSql
        .CommandText = Array("/*AccountTransactions Current Financials Journal**/ select [Journal Entry], [TRX Date], [Account Number], [Acc" _
        , _
        "ount Description], [Debit Amount], [Credit Amount], [Source Document], [User Who Posted] from AccountTransactions where [TRX Date] >= #" & b_date & "# and [TRX  Date] <= #" & e_date & "# and [segment4] = '" & Range("Segment4").Value & "'  and [segment1] = '" & Range("Segment1").Value & "' and [segment2] = '" & Range("Segment2").Value & "' and [segment3] = '" & Range("Segment3").Value & "' and [Histo" _
        , "ry TRX] = 'No' order by [Journal Entry]")
        .RowNumbers = False
        .FillAdjacentFormulas = False
        .PreserveFormatting = True
        .RefreshOnFileOpen = False
        .BackgroundQuery = True
        .RefreshStyle = xlInsertDeleteCells
        .SavePassword = True
        .SaveData = True
        .AdjustColumnWidth = True
        .RefreshPeriod = 0
        .PreserveColumnInfo = True
        .ListObject.DisplayName = "Table_ExternalData_13"
        .Refresh BackgroundQuery:=False
    End With
End Sub

1 个答案:

答案 0 :(得分:0)

我会试试这个:

"...[TRX Date] >= '" & Format(b_date, "yyyy-mm-dd")  & "'... "

如果上述内容不正确(取决于区域设置),请将您的日期格式更改为另一种格式。

相关问题