- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试制作一个 Excel 加载项以在打开任何工作簿时创建一个简单的按钮,但我得到了
Object variable or With Block variable not set
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/
我正在尝试编写一段代码,将以下代码放入已创建的新 Excel 工作簿中。生成的代码应该是 Private Sub Workbook_Open() ThisWorkbook.RefreshAll E
我正在使用 Workbook_Open 在打开应用程序时调用用户窗体,这工作正常。但是我希望它只在第一次打开时运行。我试过这个,如果我从编辑器运行子程序,但当我打开文件时,它就可以工作。 Sub Wo
我有一个 Excel 加载项,如果满足某些条件,我尝试使用它来插入代码以自动重命名工作簿。从我们的存储库导出时,所有工作簿都具有相同的名称,这导致“Excel 无法同时打开两个具有相同名称的工作簿”错
我有一个现有的工作表“StudentSheet1”,我需要根据用户需要多次添加。 例如,如果用户在单元格“A1”中输入 3,则保存并关闭工作簿。 下次打开工作簿时,我想要三张工作表:“StudentS
我有一个 excel带有 VBA 宏的文件,该宏在用户打开工作簿时启动。 其中一位用户向此文件添加了保护登录密码,现在 Workbook_open() 不再 react 。 是excel-2003 有
我有这个代码: Private Sub Workbook_Open() Dim wSheet As Worksheet Dim my_code As String Dim x As Long, y A
我在同一文件夹中有两个工作簿:bkOpenErrorTest.xlsm和bkOpenErrorTest_dict.xlsm . bkOpenErrorTest_dict.xlsm ThisWorkbo
该程序应该创建一个按钮,用户可以按下该按钮来激活不同的子程序。从我的在线搜索来看,打开工作簿时似乎应该激活下面的子项,但事实并非如此? 我做错了什么? Option Explicit Private
Excel 加载项中的 Workbook_Open 事件仅适用于第一个文件,但如果我打开下一个文件保持第一个文件处于打开状态,则它不起作用,谁能帮我解决这个问题? 我在 Thisworkbook 模块
我的 WorkBook 的 Sheet2 的 A 和 B 列中有引用数据.我有一张空白纸 - Sheet1 - 我需要从我的数据库中填写。 我想使用 Excel VBA 根据 Sheet2 中 A 和
我正在尝试制作一个 Excel 加载项以在打开任何工作簿时创建一个简单的按钮,但我得到了 Object variable or With Block variable not set 我认为这是因为技
我正在尝试拥有 Workbook_Open 事件填充控件组合框 这样当用户转到 Worksheet("Benchmarking") ,他们有一个预先填充的列表可供选择,其中包括数组 datesArr
我已经设置了一个应用程序级事件类来监视何时创建/打开新工作簿,方法是 CPearson's guide 。这在孤立的情况下工作得很好。但是,它旨在作为我正在编写的加载项的一部分,其中在“Workboo
我最近升级到 Office 365/Excel 2016 导致了一些不需要的行为变化。 Workbook("Portfolio Appreciation") 包含一个 Workbook_open 过程
我试图强制用户保存新填充的模板(使用 VBA 和其中的宏创建)以避免重新打开时数据丢失(因此用户不必在每次创建执行程序后删除数据) , 而我 read关于启用和禁用事件的一些东西,我试过了,但没有运气
所以我有一个 Workbook_open子创建 Long打开工作簿时的变量: Private Sub Workbook_open() MsgBox ("Workbook opened") Di
Excel 2016(或 365)似乎无法可靠地或更准确地触发 Workbook_Open() 子,根本没有!简单的事件子 Private Sub Workbook_Open() MsgBox
我是 Office.js 的新手在我的 VBA 加载项中,我使用 Workbook_Open() 事件在单元格位置上动态添加按钮,这些按钮在工作表上执行功能。谷歌搜索并没有得到 office.js 的
我的情况是,我正在尝试从工作簿 B 打开工作簿 A 并运行 Workbook_Open 子例程。仅使用 Workbooks.Open("c:\myworkbook.xls") 的问题是,我需要在 Wo
基本上,我试图在 Windows 中打开 powerpoint 文件后立即运行一些 VBA 代码。我不想在演示开始时运行它,但我想这将是一种替代方案。我也不太确定该怎么做。 在 Excel 中我会使用
我是一名优秀的程序员,十分优秀!