简体   繁体   中英

VBA Select a sheet when the workbook is opened

The following program doesn't work when I open my workbook. What are the possible reasons?

' Select the first sheet when the workbook is opened.
Private Sub Workbook_Open()

    Sheet4.Select
    Sheet4.Range("B1").Select

End Sub

If you hit alt+F11 to go to the VBA code editor. On the left side, under the file name you will see the different sheets, and whatever modules you might have in there. If you go under the ThisWorkbook module and place your code there, it will automatically run when you start up the Excel File.

you are using the "Select" Method without an Activating a Sheet First!

Yes, when you have closed your workbook last time, the current sheet will remain in the memory index and when you will again open the same workbook, the pointer search for the most recent sheet used based on the index no.

'Here is the code
Private Sub Workbook_Open()
    Sheet4.Activate
    Sheet4.Select
    Sheet4.Range("B1").Select
End Sub

using "Select Method" without Activating the Parent Object is a Crime. lol

Hope this will help you.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM