1

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
Mohsen
  • 175
  • 3
  • 6
  • 18
  • 2
    See [this question for using Sheets(sheet_name).Select](https://stackoverflow.com/questions/4013792/how-to-activate-a-specific-worksheet-in-excel) and [this question for selecting a range](https://stackoverflow.com/questions/17559677/select-range-in-a-particular-sheet-in-excel-vba) - which also points to why you should use Activate – LinkBerest Jul 15 '15 at 03:54
  • Thanks. but still I don't know what the problem is – Mohsen Jul 15 '15 at 03:56
  • look closer, your code is only one word off – LinkBerest Jul 15 '15 at 04:00
  • 1
    If it did not work, could I know what is the error you get? You are using Sheet4 instead or worksheet("Sheet4"). For your information, Sheet4 is the code name of the worksheet. "Sheet4" (with double quotes) is the name of the worksheet – keong kenshih Jul 15 '15 at 05:54
  • I don't think so there is any problem with sheet4. When I open the workbook, the macro doesn't select sheet4. No error comes up. – Mohsen Jul 15 '15 at 06:12
  • 3
    This method should have inside the `ThisWorkbook` module. Where did you put it? – R.Katnaan Jul 15 '15 at 08:03
  • I put it in ThisWorkbook – Mohsen Jul 16 '15 at 01:59

2 Answers2

1

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.

Constuntine
  • 498
  • 2
  • 16
  • 1
    True, but still doesn't work. I think there is a major problem with the workbook, because these codes work in other workbooks. – Mohsen Jul 16 '15 at 01:58
0

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.

Kamal Bharakhda
  • 129
  • 1
  • 12