[Fully automated] Macro to set cursor to A1 on all sheets (add-in)

01/29/2023

When you send an Excel file to your boss or customer, do you align the cursor to A1?

If you have one or two sheets, but if you have a lot of sheets, it’s a lot of work just to align the cursor.
And if there is a correction, you have to do it every time.

With macros, you can do such tedious work in a single shot.

In this article, I will introduce a macro that sets the cursor of all sheets to A1.

With this macro, you can fix the cursor of all sheets to A1 with one click.

It is intended for the following people.

  • Aligning the cursor every time I submit a document.
  • I have a lot of sheets in my Excel file and it’s hard to keep up.
  • I have too much work to do and want to know how to do it easier.

Macro to set cursor to A1 on all sheets

Sub A1Select()
'Align the cursor and scroll position to A1 in all sheets of the active book.

    Application.ScreenUpdating = False
    
    With ActiveWorkbook
        'Get the currently active sheet
        Dim actSht As Variant: Set actSht = .ActiveSheet
    
        On Error Resume Next
        Dim sht_i As Long
        For sht_i = .Worksheets.Count To 1 Step -1
            Application.Goto Reference:=.Worksheets(sht_i).Range("A1"), Scroll:=True
        Next
    End With
    
    Application.ScreenUpdating = True
    actSht.Activate

End Sub

If you run this macro, it will move all the cursors in the active Excel book to A1.

        Dim sht_i As Long
        For sht_i = .Worksheets.Count To 1 Step -1
            Application.Goto Reference:=.Worksheets(sht_i).Range("A1"), Scroll:=True
        Next

This is the part where you move the cursor, and the rest are just extras.

In this macro, the last step is to make the “originally active sheet" active again.

    actSht.Activate

You can also comment out this line to change it so that the first worksheet is active at the end.

Add-ins make it more convenient.

This macro is designed to work as an add-in.

If you make it an add-in, you can also use it for a regular Excel book without macros.

The file you want to set the cursor to A1 is the one you give to others, so you don’t want to have a book that contains macros.

So, I recommend using an add-in.

Click here to read other VBA articles.

VBA

Posted by やろまい