Picture/Table Excel 中功能区上未显示工具

Picture/Table Tools does not appear on the ribbon in Excel

我有一个包含图片和 table 的工作表。我还有一个用作倒数计时器的用户窗体。用户窗体包含以下代码:

Option Explicit
Const AllowedTime As Double = 10 ' Total time in minutes

Private Sub UserForm_Activate()

Dim T, E, M, S As Double

T = Timer

Do
    E = CDbl(Time) * 24 * 60 * 60 - T
    M = AllowedTime - 1 - Int(E / 60)
    S = 59 - Round((E / 60 - Int(E / 60)) * 60, 0)
    
    TimeLabel.Caption = Format(CStr(M), "00") & ":" & Format(CStr(S), "00")
    
    DoEvents
Loop Until (Timer - T) / 60 >= AllowedTime   

MsgBox "Time Over!"
Unload Me

End Sub

以上代码不是我的。我在 Excel 论坛上找到它。它只显示 10 分钟的倒计时计时器,当时间 运行 结束时,用户窗体将卸载。

问题是当用户表单 运行ning 时,当我单击工作表中的图片或 table 时,通常出现在功能区上的图片 Tools/Table 工具没有出现。我插入了图表,图表工具也没有出现。

我发现当我 运行 通过注释掉上面的计时器代码来创建用户窗体时,一切正常。我尝试在 3 台不同的 PC 上打开 Excel 文件,它们都出现了同样的问题。定时器代码有问题吗?

它的发生是因为 Do-Loop。它不仅限于 Picture Tools/Table Tools,还包括 PivotTable Tools.

等其他菜单

这里有一个例子可以重现您的问题。当循环为 运行ning 时,菜单将不会显示。将此代码粘贴到模块中。

Option Explicit

Sub Sample()
    Dim loopCount As Long
    
    Do
        loopCount = loopCount + 1
        
        '~~> Wait for 1 second
        Wait 1
        
        If loopCount > 15 Then Exit Sub
    Loop
End Sub

Private Sub Wait(ByVal nSec As Long)
    nSec = nSec + Timer
    While nSec > Timer
        DoEvents
    Wend
End Sub

您可以使用 Application.OnTime 来显示计时器,而不是使用循环。这是一个例子。顺便说一句,在 VBA.

中也有用于处理计时器的 API

注意:

在使用或测试计时器之前备份您的数据或使用新的工作簿。

用户表单代码:

插入一个新的用户表单。我们称它为 frmTimer。贴上一张标签。我们称它为 TimeLabel。现在将此代码粘贴到用户表单中。

Option Explicit

Dim nextMoment As Date

Private Sub UserForm_Activate()
    Timer_Event
End Sub

Private Sub UserForm_QueryClose(Cancel As Integer, CloseMode As Integer)
    Timer_Stop
End Sub

Sub Timer_Event()
    nextMoment = Now + TimeValue("00:00:01")
    Application.OnTime nextMoment, "Module1.OnTimer"
End Sub

Sub Timer_Stop()
    Application.OnTime nextMoment, "Module1.OnTimer", Schedule:=False
End Sub

Public Sub OnTimer()
    TimeLabel.Caption = Time
    Timer_Event
End Sub

模块代码:

插入一个模块。我们称它为 Module1。 (如果这是一个新工作簿,这应该是默认名称)。将此代码粘贴到此处

Option Explicit

Sub OnTimer()
    frmTimer.OnTimer
End Sub

Sub ShowForm()
    frmTimer.Show vbModeless
End Sub

现在 运行 Sub ShowForm()。你会看到菜单现在没有消失。

在行动