Justplainj
Board Regular
- Joined
- Apr 15, 2021
- Messages
- 50
- Office Version
- 365
- Platform
- Windows
Hi All,
Apologies if I do not explain correctly.
I am struggling to figure out the logical flow of a portion of my code.
Part of my code I am busy with backs up the sheet with data that I have and renames it to a date value in a cell. (the date is time stamped from the previous time the code ran)
Example of this.
If I ran the code on the 20th of Sept 2022, then it will time stamp the updated data in cell M1 with 20-09-22 (DD-MM-YY)
If I run the code today, it will first create a copy of the data sheet and rename it to the date in M1 with the word Backup after the date and add the integer 1
I achieve this rename with the following code with
However from recent it sometimes gets updated 3 or 4 times a day now, and before the date in M1 is today's date via the NOW() code the sheet name already exists and it gives an error.
What i am trying to achieve is to loop through the sheets and find if the sheet name matches in example 20-09-22 Backup1 and then +1 to X, making X = 2
Then it will check again in example 20-09-22 Backup2 and if it matches an existing sheet then + X with 1 again, making X = 3, etc and loops through until it finds a number 1, 2, 3, 4, etc that does not match an existing sheet name and then name the sheet with the date and the word Backup & a number.
I tried the following but it does not seem to work.
Thanks for the help
J
Apologies if I do not explain correctly.
I am struggling to figure out the logical flow of a portion of my code.
Part of my code I am busy with backs up the sheet with data that I have and renames it to a date value in a cell. (the date is time stamped from the previous time the code ran)
Example of this.
If I ran the code on the 20th of Sept 2022, then it will time stamp the updated data in cell M1 with 20-09-22 (DD-MM-YY)
If I run the code today, it will first create a copy of the data sheet and rename it to the date in M1 with the word Backup after the date and add the integer 1
I achieve this rename with the following code with
VBA Code:
Dim X As Integer
X = 1
With ActiveSheet
.Name = Format(ActiveSheet.Range("M1").Value, ("DD-MM-YY")) & " Backup" & X
End With
However from recent it sometimes gets updated 3 or 4 times a day now, and before the date in M1 is today's date via the NOW() code the sheet name already exists and it gives an error.
What i am trying to achieve is to loop through the sheets and find if the sheet name matches in example 20-09-22 Backup1 and then +1 to X, making X = 2
Then it will check again in example 20-09-22 Backup2 and if it matches an existing sheet then + X with 1 again, making X = 3, etc and loops through until it finds a number 1, 2, 3, 4, etc that does not match an existing sheet name and then name the sheet with the date and the word Backup & a number.
I tried the following but it does not seem to work.
VBA Code:
Dim X As Integer
Dim sht As Worksheet
X = 1
For Each sht In ThisWorkbook.Worksheets
If ActiveSheet.Name <> Format(ActiveSheet.Range("M1").Value, ("DD-MM-YY")) & " Backup" & X Then
With ActiveSheet
.Name = Format(ActiveSheet.Range("M1").Value, ("DD-MM-YY")) & " Backup" & X
End With
Else
X = X + 1
Next sht
Thanks for the help
J