代码修改,因此它现在将打开一个xlsm文件

时间:2018-10-15 10:11:37

标签: excel vba excel-vba

我当前的代码可以完美地打开XLS文件,但是现在我们接收到xlsm格式的文件。我试图简单地将代码从.xls更改为.XLSM,但是它报告找不到该文件。

这是当前代码:

Sub CopyRangeToAnotherSheet()
Dim wbTarget As Workbook
Dim wbThis As Workbook

With Application
.ScreenUpdating = False
.EnableEvents = False
End With

Set wbThis = ActiveWorkbook
Set wbThis = Workbooks.Open(Range("Z1") & Range("AA1") & ".xls")
Sheets("Sheet1").Range("a1:m10000").Copy

Set wbTarget = ThisWorkbook
wbTarget.Sheets("Sheet1").Range("a1").PasteSpecial
Paste:=xlPasteValues
Sheets("sheet1").Activate
Application.CutCopyMode = False
wbThis.Close
 Sheets("Menu Tab").Activate
End Sub

任何帮助表示赞赏!

1 个答案:

答案 0 :(得分:0)

尝试捕获错误并尝试新的扩展程序:

Sub CopyRangeToAnotherSheet()
Dim wbTarget As Workbook
Dim wbThis As Workbook

With Application
.ScreenUpdating = False
.EnableEvents = False
End With

Set wbThis = ActiveWorkbook
On Error Goto open_xlsm
Set wbThis = Workbooks.Open(Range("Z1") & Range("AA1") & ".xls")
On Error Goto 0  ' resume normal error "handling"
Goto open_done
open_xlsm:
On Error Goto 0
Set wbThis = Workbooks.Open(Range("Z1") & Range("AA1") & ".xlsm")    
open_done:
Sheets("Sheet1").Range("a1:m10000").Copy

Set wbTarget = ThisWorkbook
wbTarget.Sheets("Sheet1").Range("a1").PasteSpecial
Paste:=xlPasteValues
Sheets("sheet1").Activate
Application.CutCopyMode = False
wbThis.Close
 Sheets("Menu Tab").Activate
End Sub

这将按顺序检查文件foo.xslfoo.xslm