VBA to Conditionally Reorder/Move Sheets Upon Workbook Open

default_name

Board Regular
Joined
May 16, 2018
Messages
180
Office Version
  1. 365
  2. 2016
Platform
  1. Windows
  2. MacOS
Hey all,

I have a workbook with a tab named 'References'
Based on the current date, I would like the 'References' tab/worksheet to be assigned/moved as follows.

If today's date falls between:'References' Sheet Position in the Workbook
Jan13 - Jan263rd tab from left
Jan27 - Feb94th tab from left
Feb10 - Feb235th tab from left
Feb24 - Mar86th tab from left
....it continues for several more sheets in the workbook (27 to be exact).
I think I can get the rest of the sheet set up as long as the initial pattern is set for the others.

I imagine it would be something like this integrated into a conditional statement?
VBA Code:
Sheets("References").Move before:=Sheets(3)

Thanks in advance for your help!
 
Last edited:

Excel Facts

Links? Where??
If Excel says you have links but you can't find them, go to Formulas, Name Manager. Look for old links to dead workbooks & delete.
I'd probably move the sheet data to a table but something like this


VBA Code:
Sub MoveReferencesTab()
    Dim currentDate As Date
    Dim startDate As Date
    Dim endDate As Date
    Dim referencesSheet As Worksheet
    
    ' dates  (maybe read from a table)
    startDate = DateSerial(Year(Date), 1, 13)
    endDate = DateSerial(Year(Date), 1, 26)
    
    Set referencesSheet = ThisWorkbook.Sheets("References")
    
    ' Get the current date
    currentDate = Date
    
    ' again, I'd put this in a table with the sheet position and just loop it
    If currentDate >= startDate And currentDate <= endDate Then
        referencesSheet.Move Before:=ThisWorkbook.Sheets(3)
    End If
End Sub
 
Upvote 1
I'd probably move the sheet data to a table but something like this


VBA Code:
Sub MoveReferencesTab()
    Dim currentDate As Date
    Dim startDate As Date
    Dim endDate As Date
    Dim referencesSheet As Worksheet
 
    ' dates  (maybe read from a table)
    startDate = DateSerial(Year(Date), 1, 13)
    endDate = DateSerial(Year(Date), 1, 26)
 
    Set referencesSheet = ThisWorkbook.Sheets("References")
 
    ' Get the current date
    currentDate = Date
 
    ' again, I'd put this in a table with the sheet position and just loop it
    If currentDate >= startDate And currentDate <= endDate Then
        referencesSheet.Move Before:=ThisWorkbook.Sheets(3)
    End If
End Sub
Thanks for your help and quick response!
How would you recommend I go about implementing the data into a table?
I am realizing that hard-coding this may end up being much more tedious than I initially anticipated.
Your code works for the first instance (Jan13-Jan26), but how would I implement others? Would I need multiple subs?
 
Last edited:
Upvote 0
Nope, not multiple subs. I would set up a table "tblSheetPosition" and have columns with startDate, endDate and sheetPosition.

Then in sub above, get the table, read it and loop through it to set the proper position of the references sheet based on the dates.

Make sense?
 
Upvote 1
Nope, not multiple subs. I would set up a table "tblSheetPosition" and have columns with startDate, endDate and sheetPosition.

Then in sub above, get the table, read it and loop through it to set the proper position of the references sheet based on the dates.

Make sense?
I have gone in and created the table (see below).
How do I read in and loop through the table in the vba sub?
1705601244035.png
 
Upvote 0
I think this works. Haven't tested it. Make sure to set your sheet (codename) and set the table name.

VBA Code:
Sub MoveReferencesTab()
    Dim currentDate As Date
    Dim startDate As Date
    Dim endDate As Date
    Dim referencesSheet As Worksheet
    Dim ws As Worksheet
    Dim tbl As ListObject
    Dim sheetPosition As Integer
    Dim i As Long
    
    Set ws = shReference  ' your sheet name where the table is
    Set tbl = ws.ListObjects("tblSheetReference") ' set your name of table
    currentDate = Date
    Set referencesSheet = ThisWorkbook.Sheets("References")
    
    For i = 1 To tbl.DataBodyRange.Rows.Count
        startDate = tbl.DataBodyRange.Cells(i, 1)
        endDate = tbl.DataBodyRange.Cells(i, 2)
        sheetPosition = tbl.DataBodyRange.Cells(i, 3)

        If currentDate >= startDate And currentDate <= endDate Then
            referencesSheet.Move Before:=ThisWorkbook.Sheets(sheetPosition)
            Exit For
        End If
    Next
End Sub
 
Upvote 1
Solution
I think this works. Haven't tested it. Make sure to set your sheet (codename) and set the table name.

VBA Code:
Sub MoveReferencesTab()
    Dim currentDate As Date
    Dim startDate As Date
    Dim endDate As Date
    Dim referencesSheet As Worksheet
    Dim ws As Worksheet
    Dim tbl As ListObject
    Dim sheetPosition As Integer
    Dim i As Long
  
[COLOR=rgb(184, 49, 47)]    Set ws = shReference  ' your sheet name where the table is[/COLOR]
    Set tbl = ws.ListObjects("tblSheetReference") ' set your name of table
    currentDate = Date
    Set referencesSheet = ThisWorkbook.Sheets("References")
  
    For i = 1 To tbl.DataBodyRange.Rows.Count
        startDate = tbl.DataBodyRange.Cells(i, 1)
        endDate = tbl.DataBodyRange.Cells(i, 2)
        sheetPosition = tbl.DataBodyRange.Cells(i, 3)

        If currentDate >= startDate And currentDate <= endDate Then
            referencesSheet.Move Before:=ThisWorkbook.Sheets(sheetPosition)
            Exit For
        End If
    Next
End Sub
Looks like the code is getting caught up at:
VBA Code:
    Set ws = shReference  ' your sheet name where the table is
The table is on the sheet called References
I changed the code to:
VBA Code:
    Set ws = "References"  ' your sheet name where the table is
...but it is still throwing an error
 
Upvote 0
I use the codename of the sheet which is set in properties.

You can use the sheet name this way:

Just change the code here:

VBA Code:
    Set ws = ActiveWorkbook.Worksheets("SheetReference")  ' your sheet name where the table is
    Set tbl = ws.ListObjects("tblSheetReference") ' set your name of table
    currentDate = Date
    Set referencesSheet = ThisWorkbook.Worksheets("SheetReference")
 
Upvote 1
That worked! Thank you so much for your patience!

Final code:
VBA Code:
Sub MoveReferencesTab()
    Dim currentDate As Date
    Dim startDate As Date
    Dim endDate As Date
    Dim referencesSheet As Worksheet
    Dim ws As Worksheet
    Dim tbl As ListObject
    Dim sheetPosition As Integer
    Dim i As Long
  
    Set ws = ActiveWorkbook.Worksheets("SheetReference")  ' your sheet name where the table is
    Set tbl = ws.ListObjects("tblSheetPosition") ' set your name of table
    currentDate = Date
    Set referencesSheet = ThisWorkbook.Worksheets("SheetReference")
  
    For i = 1 To tbl.DataBodyRange.Rows.Count
        startDate = tbl.DataBodyRange.Cells(i, 1)
        endDate = tbl.DataBodyRange.Cells(i, 2)
        sheetPosition = tbl.DataBodyRange.Cells(i, 3)

        If currentDate >= startDate And currentDate <= endDate Then
            referencesSheet.Move Before:=ThisWorkbook.Sheets(sheetPosition)
            Exit For
        End If
    Next
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,815
Messages
6,181,136
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