VBA excel压缩模式问题

大家下午好。 我用你的一些帮助创build了一个有史以来第一个macros,它工作正常,直到我testing了来自Service-Now报告的实际源文件和唯一的选项.XLS,所以当我打开Excel 2013中的源文件时,它将以兼容模式和macros给我'运行时错误'9'“下标超出范围”。 我该怎么做才能使它工作?

Sub HELLO() Dim x As Workbook Sheets("Sheet1").Cells.Clear '## Open workbook first: Set x = Workbooks.Open("C:\Users\500722\Desktop\dashboard\task.xls") 'Now, transfer values from x to y: Sheet1.Cells(1, 1) = x.Sheets("Sheet1").Range("A1") With x.Sheets("Sheet1").UsedRange 'Now, paste to y worksheet: Sheet1.Range("A1").Resize( _ .Rows.Count, .Columns.Count) = .Value End With x.Close End Sub 

由于在打开的工作簿中没有名为“Sheet1”的工作表,因此当您尝试访问该工作表时,代码将失败。

将所有出现的x.Sheets("Sheet1")更改为x.Sheets("Page1") ,您的问题可能会消失。