When new year comes, new worksheet is created and so is a table in this new worksheet. I've recorded a macro to create a table. A simple function (as seperate module) returns current year so new worksheet gets its name and I wanted to name the table in this new worksheet with current year but it doesn't work properly. When I check the table name after it's created it's like _2016.
After creating the table macro is naming columns with my names but it crashes with first column with error method range of object _global failed.
I am not sure how to properly pass the result of my function to name the table and use it to refer to the table in the macro to change columns names. When macro was recorded instead of:
Range("Data.GetYear[[#Headers],[Kolumna1]]").Select
there was:
Range("Table1[[#Headers],[Kolumna1]]").Select
and it worked so basically the question is how to replace Table1 with a variable so I could refer easily to the table as there can't be multiple tables with the same name in a workbook.
Function GetYear() As String
GetYear = Format(Date, "yyyy")
End Function
Creating table:
ActiveSheet.ListObjects.Add(xlSrcRange, Range("$B$8:$F$8"), , xlYes).Name = Data.GetYear
Range("Data.GetYear[[#Headers],[Kolumna1]]").Select
ActiveCell.FormulaR1C1 = "Lp."
Range("Data.GetYear[[#Headers],[Kolumna2]]").Select
ActiveCell.FormulaR1C1 = "Data przychodu"
Range("Data.GetYear[[#Headers],[Kolumna3]]").Select
ActiveCell.FormulaR1C1 = "Kwota przychodu"
Range("Data.GetYear[[#Headers],[Kolumna4]]").Select
ActiveCell.FormulaR1C1 = "Podatek"
Range("Data.GetYear[[#Headers],[Kolumna5]]").Select
ActiveCell.FormulaR1C1 = "Dochód"
Range("Data.GetYear[#All]").Select
Selection.Columns.AutoFit
Range("Data.GetYear[[#Headers],[Lp.]]").Select
Selection.AutoFilter
method range of object _global failederror ? - kkris77