Hi zayas23 as Welcome to ExcelForum,
I think you are making things more complicated than they have to be. The only thing we really need for comparison is the 'current year'.
Try the following code contained in the attached sample workbook. It also creates a New Worksheet Tab for each 'new year.'
Option Explicit
Sub SeedData()
'This creates Initial Conditions to simulate an Existing Workbook
Dim iSeedYear As Long
Dim iYear As Long
Dim sYear As String
Dim bSheetExists As Boolean
iSeedYear = 2015
'Put Seed Years in the Spreadsheet
ThisWorkbook.Sheets("Sheet1").Range("C10") = iSeedYear
ThisWorkbook.Sheets("Sheet1").Range("D10") = iSeedYear + 1
ThisWorkbook.Sheets("Sheet1").Range("E10") = iSeedYear + 2
'Create Data Tabs if they Don't Exist
For iYear = iSeedYear To iSeedYear + 2
'Convert the Numerical Year to text
'Determine if the Sheet Exists
'If the Sheet Does NOT EXIST, add a new Sheet after 'Sheet1'
sYear = CStr(iYear)
bSheetExists = LjmSheetExists(sYear)
If bSheetExists = False Then
ThisWorkbook.Sheets.Add(After:=ThisWorkbook.Sheets("Sheet1")).Name = sYear
End If
Next iYear
'Put the Focus On 'Sheet1'
Application.ScreenUpdating = True
ThisWorkbook.Activate
ThisWorkbook.Sheets("Sheet1").Select
'Display a Completion Message
MsgBox "SeedData() completed based on Base Year " & iSeedYear & "."
End Sub
Sub SimulateWorkbookOpenCode()
Dim iCurrentYear As Long
Dim iLatestYearInSpreadsheetList As Long
Dim iSeedYear As Long
Dim iYear As Long
Dim sYear As String
Dim bSheetExists As Boolean
'Get the Current Year
iCurrentYear = Year(Date)
'Get the Latest Year in the Spreadsheet List
iLatestYearInSpreadsheetList = ThisWorkbook.Sheets("Sheet1").Range("E10").Value
'Update the Spreadsheet if the 'Current Year' is less than the 'Latest Year in the Spreadsheet List'
If iLatestYearInSpreadsheetList < iCurrentYear Then
'Create a New Seed Year
iSeedYear = iLatestYearInSpreadsheetList - 1
'Put Seed Years in the Spreadsheet
ThisWorkbook.Sheets("Sheet1").Range("C10").Value = iSeedYear
ThisWorkbook.Sheets("Sheet1").Range("D10").Value = iSeedYear + 1
ThisWorkbook.Sheets("Sheet1").Range("E10").Value = iSeedYear + 2
'Create Data Tabs if they Don't Exist
For iYear = iSeedYear To iSeedYear + 2
'Convert the Numerical Year to text
'Determine if the Sheet Exists
'If the Sheet Does NOT EXIST, add a new Sheet after 'Sheet1'
sYear = CStr(iYear)
bSheetExists = LjmSheetExists(sYear)
If bSheetExists = False Then
ThisWorkbook.Sheets.Add(After:=ThisWorkbook.Sheets("Sheet1")).Name = sYear
End If
Next iYear
'Get the NEW 'Latest Year in the Spreadsheet List'
iLatestYearInSpreadsheetList = ThisWorkbook.Sheets("Sheet1").Range("E10").Value
'Put the Focus On 'Sheet1'
Application.ScreenUpdating = True
ThisWorkbook.Activate
ThisWorkbook.Sheets("Sheet1").Select
'Display a Completion Message
If iLatestYearInSpreadsheetList < iCurrentYear Then
MsgBox "SimulateWorkbookOpenCode() updated Years." & vbCrLf & _
"CURRENT CONFIGURATION is still 'Out of Date'." & vbCrLf & _
"Save and Close this File." & vbCrLf & _
"Repeat as required until this file is 'Up to Date'."
Else
MsgBox "SimulateWorkbookOpenCode() updated Years" & vbCrLf & _
"to CURRENT CONFIGURATION."
End If
End If
End Sub
Private Function LjmSheetExists(SheetName As String) As Boolean
'Return value TRUE if sheet exists
On Error Resume Next
If Sheets(SheetName) Is Nothing Then
LjmSheetExists = False
Else
LjmSheetExists = True
End If
On Error GoTo 0
End Function
Here are a couple of tips which may help you in the future:
To prevent typos from ruining days and weeks of work 'Option Explicit' is NEEDED at the top of each code module. This prevents errors caused by missspellings and FORCES every variable to be DECLARED (e.g. Dim i as Integer). http://www.cpearson.com/excel/DeclaringVariables.aspx
Debugger Secrets:
a. Press 'F8' to single step (goes into subroutines and functions).
b. Press SHIFT 'F8' to single step OVER subroutines and functions.
c. Press CTRL 'F8' to stop at the line where the cursor is.
d. 'Left Click' the margin to the left of a line to set (or clear) a BREAKPOINT.
e. Press CTRL 'G' to open the IMMEDIATE WINDOW. 'debug.print' statements send their
output to the IMMEDIATE WINDOW.
f. Select View > Locals to see all variables while debugging.
g. To automatically set a BREAKPOINT at a certain location put in the line:
'Debug.Assert False'
h. To conditionally set a BREAKPOINT at a certain location put in lines similar to:
if i >= 20 and xTV20 > 99.56 then
Debug.Assert False
endif
i. A variable value will be displayed by putting the cursor over the variable name.
To manually set a breakpoint, see http://www.wiseowl.co.uk/blog/s196/breakpoints.htm
Lewis
Bookmarks