Want2BExcel
Board Regular
- Joined
- Nov 24, 2021
- Messages
- 114
- Office Version
- 2016
- Platform
- Windows
I can't figure out how to make this work without manually interaction in the code before every run.
The reason is these two main problems. Marked MAIN PROBLEM #1 and #2 at the bottom of the code (specific the part in BOLD, because they change al the time)
#1 ActiveSheet.ListObjects("Budget_TEMPLATE25").Name = [A1]
#2 ActiveCell.FormulaR1C1 = "=SUM(R[-1]C+Budgetkonto_2024[@December])"
.....but I've also commented two small problems that I'm trying to figure out, so if anybody have a solution for those, don't hold back The macro works despite of these!!!!
The reason is these two main problems. Marked MAIN PROBLEM #1 and #2 at the bottom of the code (specific the part in BOLD, because they change al the time)
#1 ActiveSheet.ListObjects("Budget_TEMPLATE25").Name = [A1]
#2 ActiveCell.FormulaR1C1 = "=SUM(R[-1]C+Budgetkonto_2024[@December])"
.....but I've also commented two small problems that I'm trying to figure out, so if anybody have a solution for those, don't hold back The macro works despite of these!!!!
VBA Code:
Sub New_Year()
‘
Sheets("Budget_TEMPLATE").Select
Sheets("Budget_TEMPLATE").Copy Before:=Sheets(2)
Sheets("Budget_TEMPLATE (2)").Select
' SMALL PROBLEM #2 It annoys me, that the sheets are placed backwards, e.g. Budgetkonto_2024-2023-2022 etc. but it's the only way I could come up with, where I don't have to
' change the 2 in "Before:Sheets(2)" in the code. I would prefer to automate it, if it's possible. If not then maybe it could be solved ' with user-interaction, to choose,
' where it should be placed. The sheets are always named Budgetkonto_YEAR and I would like, if they come continuously, after the previous year.
'
newname = InputBox("Name your sheet: Budgetkonto_YYYY")
ActiveSheet.Name = newname
' SMALL PROBLEM #1 Is it possible to make the first part, [Budgetkonto_], of the sheetname fixed, so the user only could type in a year, like 2023?
'
Range("A1").Select
ActiveCell.Value = newname
' Sets the value in cell A1 to the new sheetname, with the purpose to make the next line work.
'
ActiveSheet.ListObjects("Budget_TEMPLATE25").Name = [A1]
' MAIN PROBLEM #1, I try to rename the tablename (DesignTab). But it doesn't work!!! I have to manually change this part “Budget_TEMPLATE25” or else the rest dosn't work.
' I'm looking for a way to use a random TableName OR retrieve the active TableName and using that. I'm sure the text part would never change, but the numbers in the end, could be whatever, depending
' of who's using the workbook.
' Right now, every time I copy the sheet, it gets a different tablename, e.g. here "Budget_TEMPLATE25" etc. which generates and error, next time I run the macro, without
' changing to "Budget_TEMPLATE26" manually.
'
Range("D107").Select
ActiveCell.FormulaR1C1 = "=SUM(R[-1]C+Budgetkonto_2024[@December])"
' MAIN PROBLEM #2 This last line links the new year account balance, to the previous year. I don't know just yet, how this can be solved. Maybe with some kind of
' option for the user? "Which year do you want to link to?" with an option to 'choose from the available sheets e.g. "Budgetkonto_XXXX"
End Sub