Macro run when the workbook opens

bknight
BronzeLounger
Posts: 1391
Joined: 08 Jul 2016, 18:53

Macro run when the workbook opens

Post by bknight »

I have a workbook that I wish to have a macro run when the workbook opens. In Access there are a list of form open macros. How can I set up a workbook open macro?

ETA:
I have some code but it contains errors.

Code: Select all

Private Sub Workbook_Open()
Worksheets("ShowAllWorkbooks").Activate
   Sub FillCompanyNames()
      Worksheet("Sheet1").Select
      Dim x As Integer
      ' Set numrows = number of rows of data.
      NumRows = Range("A2", Range("A2").End(xlDown)).Rows.Count
      ' Select cell a1.
      Range("A2").Select
      ' Establish "For" loop to loop "numrows" number of times.
      For x = 2 To NumRows
         If Cells(x, 2) = "" Then
            If UCase(Cells(x, 1)) = "ETW" Then
                Cells(x, 2) = "Eaton Vance Tax-Managed Global Buy-Write Opportunities Fund"
            ElseIf UCase(Cells(x, 1)) = "ETY" Then
                Cells(x, 2) = "Eaton Vance Tax-Managed Diversified Equity Income Fund"
            ElseIf UCase(Cells(x, 1)) = "GUT" Then
                Cells(x, 2) = "Gabelli Utility Trust"
            ElseIf UCase(Cells(x, 1)) = "PCF" Then
                Cells(x, 2) = "High Income Securities Fund"
            ElseIf UCase(Cells(x, 1)) = "USOI" Then
                Cells(x, 2) = "Credit Suisse X-Links Crude Oil Shares Covered Call ETNs"
            ElseIf UCase(Cells(x, 1)) = "VGI" Then
                Cells(x, 2) = "Virtus Global Multi-Sector Income Fund"
            End If
        End If
         ' Selects cell down 1 row from active cell.
         ActiveCell.Offset(1, 0).Select
      Next
   End Sub
End Sub
If I move Worksheets("ShowAllWorkbooks").Activate towards the end of the code where it should be any because the work is on Sheet1 anyway. I get an other error "Expected end sub" There are two end subs so I am perplexed.
You do not have the required permissions to view the files attached to this post.

User avatar
StuartR
Administrator
Posts: 12612
Joined: 16 Jan 2010, 15:49
Location: London, Europe

Re: Macro run when the workbook opens

Post by StuartR »

You should not have one sub inside another sub. You need to delete the line
Sub FillCompanyNames()
And delete one of the two end sub lines
StuartR


User avatar
HansV
Administrator
Posts: 78523
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Macro run when the workbook opens

Post by HansV »

Alternatively, move the entire macro Sub FillCompanyName() ... End Sub out of ThisWorkbook to a standard module, and reduce Workbook_Open in ThisWorkbook to

Code: Select all

Private Sub Workbook_Open()
    Call FillCompanyName
    Worksheets("ShowAllWorkbooks").Select
End Sub
Best wishes,
Hans

bknight
BronzeLounger
Posts: 1391
Joined: 08 Jul 2016, 18:53

Re: Macro run when the workbook opens

Post by bknight »

Thanks to both, I chose the delete sub out of the other sub. Now I have one more problem the worksheet(Sheet1) starts running through the loop, so I don't remember what command are to turn off the update, in other words shut down the update and then turn it back on when the macro finishes.

ETA:
Is this the one I'm looking for or is there others?
Application.ScreenUpdating = False

User avatar
HansV
Administrator
Posts: 78523
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Macro run when the workbook opens

Post by HansV »

If you don't want to see what the code does while it's running, indeed:

Application.ScreenUpdating = False

at the beginning, and

Application.ScreenUpdating = True

at the end.
Best wishes,
Hans

bknight
BronzeLounger
Posts: 1391
Joined: 08 Jul 2016, 18:53

Re: Macro run when the workbook opens

Post by bknight »

OK