简体   繁体   English

为什么类型不匹配错误?

[英]why the type mismatch error?

Any idea why I'm getting a type mismatch error here? 知道为什么我在这里遇到类型不匹配错误吗? The cursor highlights the second last "&" in the filename string at the bottom of this code. 光标突出显示该代码底部文件名字符串中的倒数第二个“&”。 Something to do with the variable dt which is meant to be a date. 与变量dt有关,后者是日期。 Any ideas? 有任何想法吗?

Code: 码:

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")

You have declared: 您已声明:

Dim dt() As Date

That's an array. 那是一个数组。 You can't use the "&" string concatenation operator on an array. 您不能在数组上使用“&”字符串串联运算符。 Just remove the (). 只需删除()。

Try using something like this: 尝试使用如下所示的内容:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM