Overlap changing a worksheet using vba macro

FlipEternalX

New Member
Joined
Mar 3, 2023
Messages
24
Office Version
  1. 365
Platform
  1. Windows
Hi everyone. Good day.

I am asking for a help regarding my simple problem that I didn't know how to work on.

I have these excel file that change worksheet name depends on week.
On this screenshot once I run the code it will change the first worksheet based on color (I didn't want it to based on color I just want to highlight where the changing of worksheet will start)

1706087126048.png


Now, the problem is it overlap changing the file when the week for the last sheet is already done.

for example, the "Cleaners 1" is assigned to Lyka for the last week, then after it run it will proceed to Aether which is incorrect, it should be back to Jane. Same problem with orange and green worksheet. can someone help me not to overlap when running the code?

Here is my VBA Macro Code. Thanks!

VBA Code:
Private Sub Workbook_Open()
    Dim OriginalDate As Date
    Dim Weeks As Integer
    Dim SheetNum As Integer
    Dim SheetNum1 As Integer
    Dim SheetNum2 As Integer
    Dim sh As Worksheet
    
    
    OriginalDate = #1/21/2024#
    
    Weeks = DateDiff("ww", OriginalDate, Now)
    SheetNum = Weeks Mod Sheets.Count + 1
    SheetNum1 = Weeks Mod Sheets.Count + 7
    SheetNum2 = Weeks Mod Sheets.Count + 12
       
    If InStr(1, Sheets(SheetNum).Name, "Cleaners 1") < 1 Then

        'Workbook protection password
       ' ThisWorkbook.Unprotect "testpassword"

        For Each sh In Sheets
            sh.Name = Replace(sh.Name, " (Cleaners 1)", "")
            sh.Name = Replace(sh.Name, " (Cleaners 2)", "")
            sh.Name = Replace(sh.Name, " (Cleaners 3)", "")
        Next sh
        Sheets(SheetNum).Name = Sheets(SheetNum).Name & " (Cleaners 1)"
        Sheets(SheetNum1).Name = Sheets(SheetNum1).Name & " (Cleaners 2)"
        Sheets(SheetNum2).Name = Sheets(SheetNum2).Name & " (Cleaners 3)"
        'ThisWorkbook.Protect Structure:=True, Windows:=False
        
    End If
End Sub
 

Excel Facts

Last used cell?
Press Ctrl+End to move to what Excel thinks is the last used cell.
Isn't this the same question you posted before here?
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,173
Members
453,021
Latest member
Justyna P

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top