运行此代码时,我不断收到错误消息。我试图将一系列单元格从一个工作簿复制到另一个工作簿。谁能告诉我有什么问题?谢谢。
Sub Appointments()
Application.ScreenUpdating = False
''''''''Declare Variables
Dim Appts As Workbook
Dim Source As Workbook
Dim EOD As String
'''''''''Set Variable Values
Set Appts = ActiveWorkbook
'''''''''Open Report
MsgBox "Please Open The End Of Day File"
EOD = Application.GetOpenFilename
If EOD = "False" Then Exit Sub
Workbooks.Open EOD
Set Source = ActiveWorkbook
Appts.Cells(3 & "E", 43 & "E").Value = Source.Cells(3 & "D", 43 & "D").Value
End Sub
答案 0 :(得分:1)
使用Cells
时,两个参数是行索引和列索引。它正在寻找两个数字来表示一个特定的细胞。如果您尝试将范围从E3复制到E43,请使用Range("E3:E43")
。有关如何使用单元格的详细信息,请参阅this link。
答案 1 :(得分:0)
Appts.Cells(3 & "E", 43 & "E").Value = Source.Cells(3 & "D", 43 & "D").Value
可能会出错。
已经thunderblaster解释了为什么cells
不接受字符串。
可能你需要低于代码。
Appts.activesheet.range("E3:E43").Value = Source.activesheet.range("E3:E43").Value
答案 2 :(得分:0)
更改此行:
Set Appts = ActiveWorkbook
到此:
Set Appts = Thisworkbook 'Just to be safe
这一行:
Appts.Cells(3 & "E", 43 & "E").Value = Source.Cells(3 & "D", 43 & "D").Value
到这一行:
Appts.Range(Cells(3, "E"),Cells(43,"E")).Value = _
Source.Range(Cells(3, "D"), Cells(43, "D")).Value
请注意,Cells
语法为Cells(RowIndexnum,ColIndexNum)
,它接受数值
您的代码正在尝试传递String
这样的Cells(3 & "E", 43 & "E")
参数
结果如下:Cells(3E,43E)
因此创建了Runtime Error: 5
。
此外,如果您正在考虑一系列单元格,例如E3:E43
,则不能单独使用单元格。
相反,请使用Range
语法Range(CellAddress,Cell2Address)
。{/ p>
示例:
Range("E3:E43")
等于:
Range("E3", "E43")
使用Cells
等于此。
Range(Cells(3,"E"),Cells(43,"E"))
希望这有帮助。