Excel VBA for bringing cursor to A1

JDeMaro22
Lounger
Posts: 35
Joined: 16 Oct 2021, 16:22

Excel VBA for bringing cursor to A1

Post by JDeMaro22 »

Hi,

I am looking for a VBA code to put into a button on the Index page of a workbook that will move the cursor on all existing worksheets to B1. This doesn't need to move the cursor on the Index page as well only all the worksheets which may be hidden or visible.

Thank you for your time,

Josh

User avatar
HansV
Administrator
Posts: 73825
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Excel VBA for bringing cursor to A1

Post by HansV »

A1 or B1? I used A1 in the following, and I used Index as the name of the index sheet.

Code: Select all

Sub Button1_Click()
    Dim w As Worksheet
    Application.ScreenUpdating = False
    For Each w In Worksheets
        If w.Visible = xlSheetVisible And w.Name <> "Index" Then
            w.Select
            w.Range("A1").Select
        End If
    Next w
    Worksheets("Index").Select
    Application.ScreenUpdating = True
End Sub
Regards,
Hans

JDeMaro22
Lounger
Posts: 35
Joined: 16 Oct 2021, 16:22

Re: Excel VBA for bringing cursor to A1

Post by JDeMaro22 »

It'll be for B1 just because a1 is hidden. I believe this works great. As per usual thank you Hans for the quick reply and amazing results.

JDeMaro22
Lounger
Posts: 35
Joined: 16 Oct 2021, 16:22

Re: Excel VBA for bringing cursor to A1

Post by JDeMaro22 »

Hi Hans,

Actually I was wondering if there was a modification you could make where it also scrolls each of the worksheets up as well?

User avatar
HansV
Administrator
Posts: 73825
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Excel VBA for bringing cursor to A1

Post by HansV »

Like this:

Code: Select all

Sub Button1_Click()
    Dim w As Worksheet
    Application.ScreenUpdating = False
    For Each w In Worksheets
        If w.Visible = xlSheetVisible And w.Name <> "Index" Then
            Application.Goto w.Range("B1"), True
        End If
    Next w
    Worksheets("Index").Select
    Application.ScreenUpdating = True
End Sub
Regards,
Hans