尝试将工作表分配给变量时,我遇到“运行时错误'13'类型不匹配”。它适用于所有其他工作表,但似乎只发生在这个工作表中。
这是我的代码:
Sub TS()
Dim RD, Dep, QC, MM, Pro As Workbook
With ThisWorkbook
Set RD = .Sheets("RawData")
Set Dep = .Sheets("Departments")
Set QC = .Sheets("QC")
Set MM = .Sheets("MM")
Set Pro = .Sheets("Production") 'I have a problem with this line
End With
End Sub
我的工作表标签如下:
答案 0 :(得分:7)
错误与您的错误声明有关:
Pro As Workbook
您应该将其声明为Worksheet
,或仅指定variant
而不指定类型,例如:
Dim RD, Dep, QC, MM, Pro
更好的方法是使用强类型声明:
Dim RD As Worksheet, Dep As Worksheet, QC As Worksheet, MM As Worksheet, Pro As Worksheet
希望这可能会有所帮助。
答案 1 :(得分:3)
您的代码应该是什么样的:
Sub TS()
Dim RD as Worksheet
Dim Dep as Worksheet
Dim QC as Worksheet
Dim MM as Worksheet
Dim Pro as Worksheet
With This.Workbook
Set RD = .Sheets("RawData")
Set Dep = .Sheets("Departments")
Set QC = .Sheets("QC")
Set MM = .Sheets("MM")
Set Pro = .Sheets("Production")
End With
End Sub
您正在设置一堆工作表,因此需要将它们分配给Worksheet
个变量。
您的代码在Set RD = ...
上崩溃的唯一原因是除了Pro
之外的所有变量都被声明为Variant
。