gpt4 book ai didi

vba - Excel - 如何在 Workbook_Open 事件上创建按钮

转载 作者:行者123 更新时间:2023-12-04 20:42:06 25 4
gpt4 key购买 nike

我正在尝试制作一个 Excel 加载项以在打开任何工作簿时创建一个简单的按钮,但我得到了

Object variable or With Block variable not set


我认为这是因为技术上还没有“ActiveWorkbook”。
我要做的第一件事是删除当前工作表上的所有按钮。然后我想放置一个按钮。
有谁知道如何做到这一点?
代码
Private Sub Workbook_Open()
ActiveWorkbook.ActiveSheet.Buttons.Delete
Dim CommandButton As Button
Set CommandButton = ActiveWorkbook.ActiveSheet.Buttons.Add(1200, 100, 200, 75)
With CommandButton
.OnAction = "Test_Press"
.Caption = "Press for Test"
.Name = "Test"
End With
End Sub
然后我有一个 Private Sub Test_Press()显示一个 MsgBox。虽然没有创建按钮。

最佳答案

归功于 http://www.jkp-ads.com/Articles/FixLinks2UDF.asp
注:我有另一个我没有在下面发布的模块,其中包含我绑定(bind)到我放置在工作簿上的按钮的宏 Project_Count,前提是工作簿名称为 TT_GO_ExceptionReport
我还有一个 VBScript,它可以下载插件,将它放在用户插件文件夹中,然后安装它。如果您想知道如何做到这一点,请发表评论。
解决问题的插件代码:
本工作手册

Option Explicit
Private Sub Workbook_Open()
' Purpose : Code run at opening of workbook
'-------------------------------------------------------------------------
'Initialise the application
InitApp
modProcessWBOpen.TimesLooped = 0

Application.OnTime Now + TimeValue("00:00:03"), "CheckIfBookOpened"
End Sub
模块 1 名为 模块初始化
选项显式
'Create a module level object variable that will keep the instance of the
'event listener in memory (and hence alive)
Dim moAppEventHandler As cAppEvents

Sub InitApp()
'Create a new instance of cAppEvents class
Set moAppEventHandler = New cAppEvents
With moAppEventHandler
'Tell it to listen to Excel's events
Set .App = Application
End With
End Sub
模块 2 名为 modProcessWB打开
选项显式
'Counter to keep track of how many workbooks are open
Dim mlBookCount As Long

'Counter to check how many times we've looped
Private mlTimesLooped As Long

' Purpose : When a new workbook is opened, this sub will be run.
' Called from: clsAppEvents.App_Workbook_Open and ThisWorkbook.Workbook_Open
'-------------------------------------------------------------------------
Sub ProcessNewBookOpened(oBk As Workbook)

If oBk Is Nothing Then Exit Sub
If oBk Is ThisWorkbook Then Exit Sub
If oBk.IsInplace Then Exit Sub

CountBooks

'This checks to make sure the name of the new book matches what I
'want to place the button on
If oBk.Name = "TT_GO_ExceptionReport.xlsm" Then
Dim CommandButton As Button
Set CommandButton = Workbooks("TT_GO_ExceptionReport.xlsm").Sheets(1).Buttons.Add(1200, 100, 200, 75)
With CommandButton
.OnAction = "Project_Count"
.Caption = "Press for Simplified Overview"
.Name = "Simplified Overview"
End With
End If
End Sub

Sub CountBooks()
mlBookCount = Workbooks.Count
End Sub

Function BookAdded() As Boolean
If mlBookCount <> Workbooks.Count Then
BookAdded = True
CountBooks
End If
End Function

' Purpose : Checks if a new workbook has been opened
' (repeatedly until activeworkbook is not nothing)
'-------------------------------------------------------------------------
Sub CheckIfBookOpened()

If BookAdded Then
If ActiveWorkbook Is Nothing Then
mlBookCount = 0
TimesLooped = TimesLooped + 1
'May be needed if Excel is opened from Internet explorer
Application.Visible = True
If TimesLooped < 20 Then
Application.OnTime Now + TimeValue("00:00:01"), "CheckIfBookOpened"
Else
TimesLooped = 0
End If
Else
ProcessNewBookOpened ActiveWorkbook
End If
End If
End Sub

Public Property Get TimesLooped() As Long
TimesLooped = mlTimesLooped
End Property

Public Property Let TimesLooped(ByVal lTimesLooped As Long)
mlTimesLooped = lTimesLooped
End Property
名为 的类模块cAppEvents
' Purpose   : Handles Excel Application events
'-------------------------------------------------------------------------
Option Explicit

'This object variable will hold the object who's events we want to respond to
Public WithEvents App As Application

Private Sub App_WorkbookOpen(ByVal Wb As Workbook)

'Make sure newly opened book is valid
ProcessNewBookOpened Wb
End Sub

Private Sub Class_Terminate()
Set App = Nothing
End Sub

关于vba - Excel - 如何在 Workbook_Open 事件上创建按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30855358/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com