Move Worksheets

Maintained on

Basics: Moving with the Move Method

Use the Move method to move a sheet, and specify the destination with Before (in front of a sheet) or After (behind a sheet).

Move
Public Sub MoveSheet()
    ThisWorkbook.Worksheets("Sheet1").Move After:=ThisWorkbook.Worksheets("Sheet3")
End Sub
When both Before and After are omitted

If you specify neither Before nor After, the sheet is moved into a new workbook (it is not copied) and disappears from the original workbook. Normally always specify one of them so this does not happen by accident.

Moving to the Start or End

Move
Public Sub MoveSheetToStart()
    With ThisWorkbook
        .Worksheets("Sheet1").Move Before:=.Worksheets(1)
    End With
End Sub
Move
Public Sub MoveSheetToEnd()
    With ThisWorkbook
        .Worksheets("Sheet1").Move After:=.Worksheets(.Worksheets.Count)
    End With
End Sub

Worksheets.Count is the number of worksheets. If you want to place the sheet after chart sheets as well, use Sheets instead of Worksheets.

Difference between Worksheets and Sheets

Worksheets contains only worksheets, while Sheets also includes chart sheets. Index numbers follow the order of the sheets in the workbook (hidden sheets included).

Moving Multiple Sheets at Once

Pass sheet names as an array with the Array function to move several sheets in one call.

Move
Public Sub MoveMultipleSheets()
    With ThisWorkbook
        .Worksheets(Array("Sheet1", "Sheet2")).Move After:=.Worksheets("Sheet4")
    End With
End Sub

Specifying multiple sheets works the same way as when selecting them.

Select Worksheets
Learn how to select sheets with the Excel VBA Select method: by name or index, multiple sheets at once, and adding sheet

Checking That the Sheets Exist

Specifying a sheet name that does not exist raises run-time error 9 (“Subscript out of range”). Checking beforehand is safer.

Check
Public Sub SafeMoveSheet()
    If Not SheetExists("Sheet1") Or Not SheetExists("Sheet3") Then
        MsgBox "The source or destination sheet does not exist."
        Exit Sub
    End If

    ThisWorkbook.Worksheets("Sheet1").Move After:=ThisWorkbook.Worksheets("Sheet3")
End Sub

Private Function SheetExists(ByVal sheetName As String) As Boolean
    Dim ws As Worksheet
    For Each ws In ThisWorkbook.Worksheets
        If ws.Name = sheetName Then
            SheetExists = True
            Exit Function
        End If
    Next ws
End Function

Moving to Another Workbook

Specify a sheet in another workbook for Before / After to move the sheet into that workbook.

Move
Public Sub MoveSheetToOtherWorkbook()
    Dim destination As Workbook
    Set destination = Workbooks("Report.xlsx") ' an open workbook

    With destination
        ThisWorkbook.Worksheets("Sheet1").Move After:=.Worksheets(.Worksheets.Count)
    End With
End Sub

The moved sheet is removed from the original workbook. If you want to keep the original, use the Copy method instead.

Things to Watch Out For

  • Formula references: When you move a sheet to another workbook, formulas referring to other sheets may become external references.
  • Protection: If the workbook structure is protected, Move raises an error.
  • At least one sheet must remain: You cannot empty a workbook by moving away all of its visible sheets.

Summary

  • Move a sheet with the Move method and Before or After
  • Omitting both moves the sheet into a new workbook, so be careful
  • Multiple sheets can be specified at once with Array
  • A non-existent sheet name causes error 9, so check beforehand
#VBA #Excel #Sheet