我基本上将一个excel文件表复制到excel文件的特定表(与我编写宏的文件相同的文件)。我得到错误 - 运行时错误'9':下标超出范围WbTarget.Sheets( “FPP”)的范围。( “A1:E654”)。PasteSpecial的
我在VBA中不是那么好 - 请帮忙吗?
Sub XMLR()
Dim output As String
output = CreateObject("WScript.Shell").Exec("R CMD BATCH filepath.R").StdOut.ReadAll
Call XML
End Sub
Sub XML()
Dim wbTarget
Dim wbThis
Dim strName
Set wbThis = Workbooks.Open("file.xlsx")
wbThis.Activate
strName = ActiveSheet.Name
Set wbTarget = ActiveWorkbook
Application.CutCopyMode = False
wbThis.Sheets("Sheet1").Range("A1:E654").Copy
wbTarget.Sheets("FPP").Range("A1:E654").PasteSpecial
Application.CutCopyMode = False
wbTarget.Save
wbTarget.Close
wbThis.Close
Set wbTarget = Nothing
Set wbThis = Nothing
End Sub
答案 0 :(得分:0)
略有混淆,但试试这个:
Sub XML()
Dim wbTarget As Workbook
Dim wbThis As Workbook
Dim strName As String
Set wbThis = Workbooks.Open("file.xlsx")
ThisWorkbook.Sheets("FPP").Range("A1:E654").Value = wbThis.Sheets("Sheet1").Range("A1:E654").Value
wbTarget.Save
wbTarget.Close
wbThis.Close
Set wbTarget = Nothing
Set wbThis = Nothing
End Sub
答案 1 :(得分:0)
这里的问题是WbTarget和WbThis都引用相同的工作簿。
您应该更改声明:
Set wbTarget = ActiveWorkbook
到
Set wbTarget = Application.ThisWorkbook