这是我的代码:
Sub FillInternetForm()
Dim fromPath As String
Dim toPath As String
fromPath = InputBox(prompt:="Enter FROM File FULL Path:", _
Title:="FROM FILE", Default:="")
toPath = InputBox(prompt:="Enter TO File FULL Path:", _
Title:="TO FILE", Default:="")
Workbooks(fromPath).Sheets("Sheet1").Range("F10").Copy
Workbooks(toPath).Sheets("Sheet1").Range("F10").PasteSpecial
End Sub
我有3个工作簿(Macros.xlsm,CopyFrom.xlsx和CopyTo.xlsx)。上面的代码存储在Macros.xlsm工作簿的模块1中。
工作簿(CopyFrom.xlsx和CopyTo.xlsx)已经打开...
我需要做的是:将CopyFrom.xlsx工作簿中的单元格(F10)中的值复制到CopyTo.xlsx工作簿中的单元格(F10)...
我一直收到以下错误:“下标超出范围”
感谢任何帮助!
答案 0 :(得分:1)
您需要为这些工作簿创建对象。
Sub FillInternetForm()
Dim fromPath As String
Dim toPath As String
Dim wbFrom As Workbook
Dim wbTo As Workbook
Dim fromValue As String
fromPath = InputBox(prompt:="Enter FROM File FULL Path:", _
Title:="FROM FILE", Default:="")
toPath = InputBox(prompt:="Enter TO File FULL Path:", _
Title:="TO FILE", Default:="")
Set wbFrom = Workbooks.Open(Filename:=fromPath, ReadOnly:=True)
Set wbTo = Workbooks.Open(Filename:=toPath, ReadOnly:=False)
wbFrom.Activate
fromValue = ActiveWorkbook.Sheets(1).Range("F10").Value
wbTo.Activate
ActiveWorkbook.Sheets(1).Range("F10").Value = fromValue
End Sub
我不确定为什么你说他们已经开放然后你要求他们的完整路径。也许你可以解释一下。