我收到令人讨厌的Type mismatch错误,无法解决。下面的代码对我来说似乎一切正常。
Dim C_Source As Workbook
Set C_Source = Workbooks.Open(ThisWorkbook.Sheets("Sheet1").Range("B4").Text)
'**Referring a cell to get the full path of file to open and the file opened..
If ActiveSheet.AutoFilterMode Then Cells.AutoFilter
tws = ActiveSheet.Name
Dim ws1 As Excel.Worksheet
'**Renaming here to maintain uniformity later..
Set ws1 = Workbooks(C_Source).Worksheets(tws)
'**This line gives Type Mismatch Error, have tried Sheet1 which is the actual name in place of tws here but the same error..
以上代码给出
运行时错误13,类型不匹配
在此先感谢您的帮助
答案 0 :(得分:4)
您需要更改此内容
Set ws1 = Workbooks(C_Source).Worksheets(tws)
对此:
Set ws1 = C_Source.Worksheets(tws)
C_Source
是一个工作簿对象变量。 Workbooks()
参数需要一个字符串变量,字符串文字或索引号。
但是,如果您 truly 想要使用变量的.Name
属性,那么这是合法的:
Set ws1 = Workbooks(C_Source.Name).Worksheets(tws)
您正确使用tws
-尽管我建议您使用Dim语句显式定义它的数据类型。