为什么类型不匹配错误?

时间:2014-10-31 19:45:42

标签: excel excel-vba vba

我知道为什么我在这里遇到类型不匹配错误?光标突出显示倒数第二个“&”在此代码底部的文件名字符串中。与变量dt有关,这是一个约会。有什么想法吗?

代码:

Sub daily_report_data()

Dim strpath1 As String
Dim strpath2 As String
Dim wb As Workbook
Dim wb2 As Variant
Dim ws As Worksheet
Dim nm As String
Dim dt() As Date

strpath1 = "\\ironhide\[folder name]\[folder name]\[folder name]\[folder name]\"
strpath2 = "c:\Users\[user.name]\desktop\data files\"

Workbooks.Open Filename:=strpath1 & "[file name].xlsx", ReadOnly:=True

Set wb = Workbooks("[file name]")
Set ws = Workbooks("[file name]").Sheets("data")
nm = ws.Name

dt = Date

Workbooks.Add
DoEvents

ActiveWorkbook.SaveAs strpath2 & nm & "_" & dt & ".xlsx"
Set wb2 = Workbooks(nm & dt & ".xlsx")

2 个答案:

答案 0 :(得分:1)

您已声明:

Dim dt() As Date

那是一个阵列。你不能使用"&"数组上的字符串连接运算符。只需删除()。

答案 1 :(得分:0)

尝试使用以下内容:

Dim FileNPath As String
Dim strDate As String
Dim dt As Date

strDate = Format(dt, "ddmmyyyy")
FileNPath = strpath2 & nm & "_" & strDate & ".xlsx"

ActiveWorkbook.SaveAs fileName:=FileNPath