Trying to reference another worksheet in active workbook

偶尔善良 提交于 2019-12-20 04:56:27

问题


I am trying to make another worksheet active when command button is clicked, but I'm staying within the same active workbook:

Sub Submit_Warranty()
    'Set warranty data worksheet as active page
    Sheets("Sheet2").Activate

    'Show the submit warranty user form
    Warranty_Input.Show
End Sub

I keep getting "subscript out of range" error. Any ideas?


回答1:


If the code you posted is everything, then that error pretty much has to be from an invalid reference. So my guess would be that the actual displayed name is something like "Warranty_Data", while "Sheet2" is likely the VBA object name (maybe you're using them in reverse).

There are a lot of ways to select a worksheet, with various advantages and disadvantages. When it comes to selecting by name, the major gotcha to watch out for is that sheets actually have two names assigned, and you're employing both methods of selection in the code you posted. The one name is what's displayed in the sheet's workbook tab, the other name is internal to VBA. Here's a screenshot to demonstrate how to use both types of names.



来源:https://stackoverflow.com/questions/33479352/trying-to-reference-another-worksheet-in-active-workbook

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!