使用LastSaved函数时,某些项目(但不是其他项目)的VBA接收ByRef参数类型不匹配

时间:2018-06-05 13:18:01

标签: excel vba excel-vba

我试图编写一个脚本,列出上次保存每个六个输入模板的最后时间/日期。前4个似乎工作正常,但当我到达第5& 6日(TemplateWB1& TemplateWB2),我收到以下错误: "编译错误:ByRef参数类型不匹配"

我无法弄清楚为什么其中一些有效,但其他人没有......?我已多次检查并重新检查文件路径,并且它们是正确的。

Sub RefreshSaveDateTimes()
    Dim DateWBpath As String
    Dim DistWBpath As String
    Dim prdWBpath As String
    Dim sparkUPCitemWBpath As String
    Dim myDate As Date
    Dim menuMenuWS As Worksheet
    Dim TemplateWB1, TemplateWB2 As String

    Set menuMenuWS = ActiveWorkbook.Worksheets("Menu")

    DateWBpath = "P:\CPW Planning Tool\DataSources\DateTable.xlsx"
    DistWBpath = "P:\CPW Planning Tool\Input Templates\Confections\Confections Distribution Inputs.xlsm"
    prdWBpath = "P:\CPW Planning Tool\Input Templates\Confections\Confections PRD Inputs.xlsm"
    sparkUPCitemWBpath = "P:\CPW Planning Tool\Input Templates\Confections\Confections Spark UPC Item Table.xlsx"
    TemplateWB1 = "P:\CPW Planning Tool\Input Templates\Confections\Forecast Template 1 - Confy.xlsm"
    TemplateWB2 = "P:\CPW Planning Tool\Input Templates\Confections\Forecast Template 2 - Confy.xlsm"

    myDate = LastSaved(DateWBpath)
        menuMenuWS.Cells(2, 3).Select
        ActiveCell.Value = myDate

    myDate = LastSaved(sparkUPCitemWBpath)
        menuMenuWS.Cells(3, 3).Select
        ActiveCell.Value = myDate

    myDate = LastSaved(prdWBpath)
        menuMenuWS.Cells(4, 3).Select
        ActiveCell.Value = myDate

    myDate = LastSaved(DistWBpath)
        menuMenuWS.Cells(5, 3).Select
        ActiveCell.Value = myDate

    myDate = LastSaved(TemplateWB1)
        menuMenuWS.Cells(6, 3).Select
        ActiveCell.Value = myDate

    myDate = LastSaved(TemplateWB2)
        menuMenuWS.Cells(7, 3).Select
        ActiveCell.Value = myDate

End Sub

1 个答案:

答案 0 :(得分:1)

只是因为当你写这篇文章时:

Dim TemplateWB1, TemplateWB2 As String

TemplateWB1实际上是Variant,而不是String。您必须为每个变量指定类型:

Dim TemplateWB1 As String, TemplateWB2 As String