vba 命名工作表的对象需要错误
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21404021/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Object required error for named worksheet
提问by srt
Set Nightletter = ActiveWorkbook
Nightletter.Sheets("XPT SUMMARY").Activate
Range("A2").Activate
for this code also it shows Object Required error on 2nd line. I changed it to
对于此代码,它还在第二行显示 Object Required 错误。我把它改成
Nightletter.Sheets("XPT SUMMARY").Activate
Activesheet.Range("A2").Activate
It also didn't work.
它也没有奏效。
回答by Floris
In case the suggestions in the comments above haven't yet solved your issue, please try to do the following.
如果上述评论中的建议尚未解决您的问题,请尝试执行以下操作。
Open a fresh copy of Excel. Create a new workbook, and save it as "myWorkbook.xlsm" - a workbook with macros enabled. Make sure it has at least two sheets.
打开 Excel 的新副本。创建一个新工作簿,并将其另存为“myWorkbook.xlsm”——启用宏的工作簿。确保它至少有两张纸。
Now open the VBA editor, create a new module, and enter the following code:
现在打开 VBA 编辑器,创建一个新模块,然后输入以下代码:
Option Explicit
Sub actSheet()
' simple code to activate Sheet2 in myWorkbook.xlsm
Dim wb as Workbook
Dim ws as Worksheet
Set wb = Application.Workbooks("myWorkbook.xlsm")
Set ws = wb.Sheets("Sheet2")
If Not ws.Visible = xlSheetVisible Then ws.Visible = xlSheetVisible
ws.Activate
End Sub
This is using what I believe are Best Practices for what you are trying to do (although it's almost never necessary to Activate a worksheet - see the excellent link in Siddharth Rout's comment above). Specifically:
这是使用我认为是您尝试执行的操作的最佳实践(尽管几乎不需要激活工作表 - 请参阅上面 Siddharth Rout 评论中的优秀链接)。具体来说:
- Start with
Option Explicit
(so you must declare each variable) - Include a comment to describe your function
- Declare all variables used
- Build up reference to sheet from workbook, and by name: name less likely to change than order
- Make sure that the sheet you want to activate is Visible
- 开始
Option Explicit
(所以你必须声明每个变量) - 添加注释来描述您的功能
- 声明所有使用的变量
- 从工作簿建立对工作表的引用,并按名称:名称比顺序更不可能改变
- 确保您要激活的工作表可见
Finally - compare to what you are doing with the above; if you really can't tell the difference, and you are still getting the error, come back and tell us.
最后 - 与你在上面做的比较;如果您真的无法分辨出差异,并且仍然出现错误,请回来告诉我们。