0

I am trying to get a number in Microsoft Excel to increase by 1 whenever a Excel document is opened. It increases when I hit the run button but not when the sheet is opened.

Private Sub Workbook_Open()
    Dim currentValue As Integer
    currentValue = Range("A1").Value
    Range("A1").Value = currentValue + 1
End Sub
pgSystemTester
  • 8,979
  • 2
  • 23
  • 49
Dion H
  • 1
  • 1
  • 1
    Make sure this macro is in the WORKBOOK container... not Module, also `Long` instead of `Integer` as [this first-rate answer illustrates](https://stackoverflow.com/a/51689021/11732320) (though it isn't the issue you're dealing with). – pgSystemTester Feb 23 '23 at 03:17
  • Works perfectly now. Thank you. I'm new to VBA so still learning. – Dion H Feb 23 '23 at 03:45
  • Also tell the code which sheet you want A1 changed otherwise it will look at whatever sheet's active when the file opens. `Worksheets("Sheet1").Range("A1")`. The correct file will be active as it's just opened, but usually you'd also use something like `ThisWorkbook.Worksheets("Sheet1").Range("A1")`. `ThisWorkbook` being a reference to the file containing the code. – Darren Bartrup-Cook Feb 23 '23 at 08:13

0 Answers0