icstarz2
icstarz2

Reputation: 1

Excel File Open Macro

So I'm trying to create a Macro That will update a cell when the file is opened. I'm getting the 424 Error and so I tried to do a better job defining my code/object but It still wasn't succesfull. I think I'm missing/overlooking something really easy but I can't figure it out and this is my first project so I'm trying to learn and gain a better understanding then just googling a segment of code that will work.

Private Sub Auto_Open()

    Dim Try1 As String
    Try1 = ActivateSheet.Cells(3, 2).Select
    Tryl = "-"

    ' My first attempt is shown below
    '
    'Sheets("Current Calc").Activate
    'ActivateSheet.Cells(3, 2).Value = "-"

End Sub

Upvotes: 0

Views: 113

Answers (2)

user8753746
user8753746

Reputation:

Try with the following sub:

Private Sub Workbook_Open()

    Dim Try1 As String

    Try1 = ActiveSheet.Cells(3, 2).Select
    Tryl = "-"

End Sub

Some advices:

  1. write your code in lowercase. When you change a line, your code will change to upper and lower case automatically, this way you will detect if you have some typo error.
  2. write a function or object ant type . This will open a dropdown list, this way you will also avoid typo error.

Upvotes: 1

Maldred
Maldred

Reputation: 1104

You've got a typo in your commented code

What you have...

Sheets("Current Calc").Activate
ActivateSheet.Cells(3, 2).Value = "-"

What it should be...

Sheets("Current Calc").Activate
ActiveSheet.Cells(3, 2).Value = "-"

Also, I should mention you should avoid using .Activate and .Select unless necessary. With that being said, I'd suggest the below code instead...

Sheets("Current Calc").Cells(3, 2).Value = "-"

EDIT:

When using Auto_Open, Excel must be opened MANUALLY in order for the code to execute; thus if it is opened via VBA this event will NOT trigger. If you want an event to trigger via VBA as well as manually, I'd suggest using Workbook_Open

Upvotes: 2

Related Questions