I could not find this question in the forums. Thought it would be a simple solution. I'm using Excel 2013 and have VBA code to select a worksheet. My worksheets are years 2013,2014, 2015. Depending on the command button on my userform, I update cells in a particular spreadsheet. In my code I want the current year spreadsheet. Here is a little of my code:
This fails on a Runtime error '9'- subscript out of range'. If I use "Set ws = ThisWorkbook.Worksheets("2015")" the code works. But only for the rest of this year....
I even tried "Set ws = ThisWorkbook.Worksheets(Year(Now()))" and if also fails. What am I doing wrong?
Dan
Code:
Dim yr As Integer 'year variable
Dim ws, As Worksheet 'worksheet variable'
yr = Year(Now())
Set ws = ThisWorkbook.Worksheets(yr)
This fails on a Runtime error '9'- subscript out of range'. If I use "Set ws = ThisWorkbook.Worksheets("2015")" the code works. But only for the rest of this year....
I even tried "Set ws = ThisWorkbook.Worksheets(Year(Now()))" and if also fails. What am I doing wrong?
Dan