简体   繁体   中英

Automatically open an excel file to cell A1 in all worksheets (using VBA)

I am trying to write VBA code so that whenever I open any file in excel, it automatically goes to Cell A1 in all sheets (no matter what cells were selected when it was last saved). I found something online that suggested putting the following code in my Personal .xlsb project:

Sub kTest()

    Dim i As Long, s() As String, a As String, n As Long

    With ActiveWorkbook
        For i = 1 To .Worksheets.Count
            a = a & .Worksheets(i).Name
            n = n + 1
            ReDim Preserve s(1 To n)
            s(n) = .Worksheets(i).Name
            If Len(a) > 224 Then
                .Worksheets(s).Select
                .Worksheets(s(1)).Activate
                [a1].Select
                n = 0: a = "": Erase s
            End If
        Next
        If Len(a) Then
            .Worksheets(s).Select
            .Worksheets(s(1)).Activate
            [a1].Select
        End If
        Application.Goto .Worksheets(1).Range("a1")
    End With

End Sub

But nothing happens when I open a file. Please help!

You cannot go to Cell A1 in every sheet. But if you would like to go to Cell A1 of a single sheet you could do the following.

Create a class ExcelEvents with the following code

Option Explicit

Private WithEvents App As Application

Private Sub App_WorkbookOpen(ByVal Wb As Workbook)
    App.Goto Wb.Worksheets(1).Range("A1")
End Sub

Private Sub Class_Initialize()
    Set App = Application
End Sub

And in ThisWorkbook add

Option Explicit

Private xlApp As ExcelEvents

Private Sub Workbook_Open()
    Set xlApp = New ExcelEvents
End Sub

Save the workbook, re-open it and the code in the workbook_open event will run and that means as soon as you open another workbook the code will goto cell A1 of sheet 1

EDIT If you really mean to select A1 in every single sheet you could change the code as follows

Private Sub App_WorkbookOpen(ByVal Wb As Workbook)
Dim sh As Worksheet
    App.ScreenUpdating = False
    For Each sh In Wb.Worksheets
        sh.Select
        sh.Range("A1").Select
    Next
    App.Goto Wb.Worksheets(1).Range("A1")
    App.ScreenUpdating = True
End Sub

A simple solution:

    For Each Sheet In ActiveWorkbook.Worksheets
        Sheet.Select
        Range("A1").Select
    Next

Using MicScoPau's loop through the worksheets Place the following code in the ThisWorkbook module of Personal.xlsb: You'll have to reopen excel for this to work the first time. If your Personal.xlsb is hidden, then you will have some issues with the each sheet in activeworkbook.

Private WithEvents app As Application

Private Sub app_WorkbookOpen(ByVal Wb As Workbook)
    For Each Sheet In ActiveWorkbook.Worksheets
        Sheet.Select
        Range("A1").Select
    Next
End Sub

Private Sub Workbook_Open()
  Set app = Application
End Sub

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM