[英]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). 我正在尝试编写VBA代码,这样每当我打开excel中的任何文件时,它会自动转到所有工作表中的Cell A1(无论上次保存时选择了哪些单元格)。 I found something online that suggested putting the following code in my Personal .xlsb project: 我在网上发现了一些建议在我的Personal .xlsb项目中添加以下代码:
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. 你不能在每张表中转到Cell A1。 But if you would like to go to Cell A1 of a single sheet you could do the following. 但是,如果您想要单张表格的单元格A1,您可以执行以下操作。
Create a class ExcelEvents with the following code 使用以下代码创建一个ExcelEvents类
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 并在ThisWorkbook中添加
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 保存工作簿,重新打开它,workbook_open事件中的代码将运行,这意味着一旦打开另一个工作簿,代码将转到工作表1的单元格A1
EDIT If you really mean to select A1 in every single sheet you could change the code as follows 编辑如果您真的想在每张表中选择A1,您可以按如下方式更改代码
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. 在工作表中使用MicScoPau循环将以下代码放在Personal.xlsb的ThisWorkbook模块中:您必须重新打开excel才能使其首次运行。 If your Personal.xlsb is hidden, then you will have some issues with the each sheet in activeworkbook. 如果您的Personal.xlsb被隐藏,那么您将在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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.