jknight291
New Member
- Joined
- Jun 6, 2017
- Messages
- 8
I am writing a code that automates an annual work process that involve copying several worksheets and changing formulas/macros. I am stuck with trying to code a formula VLOOKUP that references a newly made worksheet. When I run the macro, it doesn't not recognize "tableyear" as a variable and the formula returns as NA. The problem is the VLOOKUP at the bottom.
Can anyone help?
Can anyone help?
VBA Code:
Sub createnewtable()
Dim tableprevyear As String
Dim tableyear As String
Dim tablenewyear As String
'------------------------------------------------------------------
'Copy Previous Year Tables
'Copy Monthly Table
tableprevyear = ActiveSheet.Range("W1").Value
tableyear = ActiveSheet.Range("W1").Value + 1
tablenewyear = "MonthlyTable" & tableyear
Sheets("MonthlyTable" & tableprevyear).Copy before:=Sheets("MonthlyTable" & tableprevyear)
ActiveSheet.Name = tablenewyear
'Copy & Prepare Train 1 Data Sheet
Sheets("Train1" & tableprevyear).Copy after:=Sheets(tablenewyear)
ActiveSheet.Name = "Train1" & tableyear
ActiveSheet.Range("A2:Z400").ClearContents
'Copy & Prepare Train 2 Data Sheet
Sheets("Train2" & tableprevyear).Copy after:=Sheets("Train1" & tableyear)
ActiveSheet.Name = "Train2" & tableyear
ActiveSheet.Range("A2:Z400").ClearContents
'Copy Train 1 BDP Data Sheet
Sheets("Train1BDP" & tableprevyear).Copy after:=Sheets("Train2" & tableyear)
ActiveSheet.Name = "Train1BDP" & tableyear
Train1BDPShtNm = ActiveSheet.Name
'Copy Train 2 BDP Data Sheet
Sheets("Train2BDP" & tableprevyear).Copy after:=Sheets("Train1BDP" & tableyear)
ActiveSheet.Name = "Train2BDP" & tableyear
'------------------------------------------------------------------
'Deactivate Previous Year Monthly Table
Sheets("MonthlyTable" & tableprevyear).Activate
ActiveSheet.Range("B5:N5").Copy
ActiveSheet.Range("B5").PasteSpecial Paste:=xlPasteValues
Application.CutCopyMode = False
'------------------------------------------------------------------
'Converting New Table Formulas to New Year
Sheets(tablenewyear).Activate
ActiveSheet.Range("W1").Value = tableyear
FirstDayofYear = ActiveSheet.Range("C7").Value
'------------------------------------------------------------------
'Exract First Line of Data
ThisWorkbook.Sheets("Train1" & tableyear).Activate
'the next lines fill in pertinent formulas
Range("O2").Formula = "=VLOOKUP(F2,INDIRECT(""Train1BDP""&tableyear&""!$A$1:$B$90""), 2, False)"
End Sub