Error when Opening workbbok

mayoung

Active Member
Joined
Mar 26, 2014
Messages
259
Office Version
  1. 365
Platform
  1. Windows
  2. Mobile
I run this code two different ways. First way is by clicking on the workbook icon itself and when the workbook opens the code excutes perfectly. I also have a another workbook with a form button that when pressed opens this workbook and this code runs. When the workbook opens the code errors in the module at this line: Sheets("COS Attach").Select. Any Suggestions on how to fix? Thank

This code is in ThisWorkbook:
Code:
Private Sub Workbook_Open()
    Application.ScreenUpdating = False
    'Module102
    Application.Run "Module102.WorkBookOpen"
    Application.ScreenUpdating = True
End Sub

This code is in the module:
Code:
Private Sub WorkbookOpen()
    Dim j As Integer
    Dim i As Integer
    Dim SmallScroll As Long
    Dim ToLeft As Long
  ' Application.Wait (Now + TimeValue("0:00:05"))
    Sheets("COS Attach").Select
    Range("A7").Select
    Application.GoTo reference:=ActiveCell, Scroll:=True
    With ActiveWindow
        i = .VisibleRange.Rows.Count / 2
        j = .VisibleRange.Columns.Count / 2
        .SmallScroll Up:=i, ToLeft:=j
    End With
    ActiveWindow.SmallScroll Down:=-20
End Sub
 

Excel Facts

Who is Mr Spreadsheet?
Author John Walkenbach was Mr Spreadsheet until his retirement in June 2019.
Problem Solved:

Found my answer in a MR Excel post dated Apr 4th, 2004 by Dmcoffman Titled: Runtime error '9':Subscript out of range


Per dmcoffman, This is the scenerio I encountered.


As I pointed out the code worked when a single worksheet was opened. The problem was only evident when multiple worksheets were opened.


Don's belief is that Excel is opening all the workbooks before executing any code, thus it was attempting to select a sheet not available in the workbook that was active. Based on this I changed this code:

Code:
Sheets(COS Attach).Select
Range("A7").Select


-to this-

Code:
Workbooks("COS Sheets 2015.xlsm").Worksheets("COS Attach").Activate
    Range("A7").Select


If you change the .Activate to .Select you will get the time error, must end in .Activate.


Good Job!!
 
Upvote 0

Forum statistics

Threads
1,223,164
Messages
6,170,444
Members
452,326
Latest member
johnshaji

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top