VBA for moving certain worksheets within the same workbook

alyos

New Member
Joined
Jan 21, 2022
Messages
13
Office Version
  1. 365
Platform
  1. Windows
Hi all,

Haven't worked with VBA for some time - and unfortunately forgot even the basics, so your help here will be highly appreciated. Went through a few threads, couldn't find what I'm looking for...

Here is what I'm trying to achieve:

1) I have 14 visible worksheets to capture 12 months period (with Start and End worksheets being empty).

1658486758882.png


2) When I have data for new month, I need to: select the latest month (always before 'End' tab - in this example Jun 22), create a copy of it and move the copy after 'End' tab. I then need to return to the original tab (Jun 22), select cells range (B16:I16) and perform PasteSpecial xlPasteValues.

1658487062389.png


1658487489830.png


3)After that the copy (Jun 22 (2)) that was moved after 'End' needs to be moved before 'End' so it looks like this:

1658487516143.png


4) Next step is to move whatever month is after 'Start' (Jul 21 in this example), before 'Start'. Once moved, I need to hide 'Jul 21' tab, so that I end up with another 12 months overview.

1658487544310.png

1658487565738.png


I do realise that I will need variables for this task, or setting 'Start' and 'End' as active sheets since they are the main criteria and other sheets' names will be always different, but that's as far as my understanding goes :(

Many thanks for your help and please shout if the above doesn't make sense at all.
 
Try:
VBA Code:
Sub MoveSheets()
    Application.ScreenUpdating = False
    Sheets(Sheets("End").Index - 1).Copy after:=Sheets(Sheets.Count)
    Sheets(Sheets("End").Index - 1).Range("B16:I16").Value = Sheets(Sheets("End").Index - 1).Range("B16:I16").Value
    Sheets(Sheets.Count).Move Before:=Sheets("End")
    Sheets(Sheets("Start").Index + 1).Move Before:=Sheets("Start")
    Sheets(Sheets("Start").Index - 1).Visible = False
    Application.ScreenUpdating = True
End Sub
 
Upvote 0
Solution

Excel Facts

Will the fill handle fill 1, 2, 3?
Yes! Type 1 in a cell. Hold down Ctrl while you drag the fill handle.
Try:
VBA Code:
Sub MoveSheets()
    Application.ScreenUpdating = False
    Sheets(Sheets("End").Index - 1).Copy after:=Sheets(Sheets.Count)
    Sheets(Sheets("End").Index - 1).Range("B16:I16").Value = Sheets(Sheets("End").Index - 1).Range("B16:I16").Value
    Sheets(Sheets.Count).Move Before:=Sheets("End")
    Sheets(Sheets("Start").Index + 1).Move Before:=Sheets("Start")
    Sheets(Sheets("Start").Index - 1).Visible = False
    Application.ScreenUpdating = True
End Sub

Thanks so much - all resolved with your help!
 
Upvote 0

Forum statistics

Threads
1,223,910
Messages
6,175,318
Members
452,634
Latest member
cpostell

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