虽然这看起来非常基本,但在我的宏中尝试在我的工作簿上的某个工作表中选择单元格时,我不断收到错误消息。有谁知道为什么这不起作用?我收到错误消息运行时错误'1004'。 工作表名称为“Sheet1”,我的代码如下:
Application.ActiveWorkbook.Worksheets("Sheet1").Range("N2").Select
答案 0 :(得分:-1)
在您不需要时使用ActiveWorkbook
是不好的做法。将工作簿和工作表设置为可以调用的实际变量总是更好。我认为您的代码正在激活另一个工作簿,然后尝试在工作表中选择一个无法找到的范围。
Sub TryThis()
Dim wbk As Workbook
Dim ws As Worksheet
Set wbk = Workbooks("myWorkbook.xlsm")
Set ws = wbk.Worksheets("Sheet1")
'Now when we say "ws." it is actually "Workbooks("myWorkbook.xlsm").Worksheets("Sheet1")."
'This is okay to start with but it's better to work with the cells directly
ws.Select
Range("N2").Select
Selection = "myText"
'This is much faster and you won't have to worry about what is currently selected
ws.Range("N2") = "myText"
End Sub