在工作簿中使用工作簿变量(variablename).close Statement`

时间:2015-09-16 18:35:56

标签: excel vba excel-vba

文件名发生了变化,所以我使用通配符*在Workbooks.Open语句中引用它

最后一行代码抛出类型不匹配错误,如何更改语法以修复代码?

 Dim JohnnyFootball As Workbook
 Workbooks.Open Filename:= _
    "T:\ConA\RIP\Extracts\Reference" & "*.xlsx"     
    Sheets("Sheet1").Select
   Set JohnnyFootball = ActiveWorkbook
   'Other [redacted] code here runs fine
   'Code below throws type mismatch error
  Workbooks(JohnnyFootball).Close SaveChanges:=False

1 个答案:

答案 0 :(得分:2)

最后一行应如下所示:

JohnnyFootball.Close SaveChanges:=False

JohnnyFootballWorkbook变量。要从Workbooks集合中获取元素,您必须传递它的名称或索引。