本文介绍了Userform激活首先启动的工作表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个用户表单,有几个按钮可供不同的宏运行。在宏的执行结束时,它隐藏了用户。我已经使用以下代码重新使用userform的关闭按钮:

I have a userform with few buttons for different macros to run. At the end of execution of macro it hides the userfrom. I have resablled the close button of userform with the following code:

Private Sub UserForm_QueryClose _
  (Cancel As Integer, CloseMode As Integer)
'   Prevents use of the Close button
    If CloseMode = vbFormControlMenu Then
        hmm
        Cancel = True
    End If
End Sub

hmm隐藏用户。现在的挑战是假设有3个工作簿。

hmm hides the userfrom. Now the challenge is suppose there are 3 workbook.


  1. 工作簿A:启用宏的工作簿,其中我编写了代码用户形式和子例程

  1. Workbook A: The macro enabled workbook in which I have written the code for userform and sub routines

工作簿B:我按下组合键,并显示用户从

Workbook B: Where I press the key combination and this shows the userfrom

工作簿C:我想在同一用户的帮助下执行相同的宏。

Workbook C: Where I want to execute same macro with the help of same userfrom

现在,当我按组合键(当工作簿C被激活时),它会激活其他工作簿(首先启动用户的工作簿),然后显示用户。虽然当我按下用户的某个按钮时,它会运行相关的宏,但会激活首先启动表单的工作簿。

Now when I press key combination (while Workbook C is activated) it activates other workbook (the one where I initiated the userfrom first) and then show the userfrom. Though when I press the some button on userfrom it runs the relevant macro but activates the workbook where the form was initiated first.

我想保持相同的工作簿激活,按键组合显示用户(所以不去上一个工作簿)。请帮助

I want to keep the same workbook activated where this key combination was pressed to show the userfrom (so it do not go to the previous workbook). Please help

代码:

这是关键组合,已保存在Microsoft Excel Objects的ThisWorkbook中

This is the key combinantion and has been saved in ThisWorkbook of Microsoft Excel Objects

Private Sub Workbook_Open()
    Application.OnKey "%{s}", "LoadMenu"
End Sub

这些过程都在代码模块中,并加载并隐藏用户(名为MainMenu)

These procedures are in a code module and loads and hides the userfrom (named as MainMenu)

Private Sub LoadMenu()

MainMenu.Show

End Sub



Sub hideMenu()

MainMenu.Hide

End Sub

这是一个示例子例程,当我们点击userfrom上的一个按钮时运行,

This is an example sub routine which is run when we click a button on the userfrom,

Sub highlightYellow()
    selection.Interior.Color = vbYellow
    hideMenu ' This hides menu after the execution
End Sub

现在,我已经在一些工作簿上运行宏(通过首次调用userform并单击其上的按钮)第一次像我之前所说的那样,然后当我去一些其他的工作簿,然后再次按这个组合键,我需要我回到第一个工作簿,当我第一次使用这个东西,当我按高亮按钮突出显示了我按下组合键的工作簿,但最后不知道为什么第一个工作簿自动激活。

Now once i have run the macro on some workbook (by calling the userform first time and by clicking a button on it) for the first time like as I said before, and then when I go to some other workbook and then again press this key combination it takes me back to the first workbook when I used this thing for the first time and when I press the highlighting button it highlights the workbook where I pressed the key combination but at the end do not know why the first workbook automatically becomes activate.

请参阅以下链接了解工作簿,请打开2-3个新的工作簿,然后一起尝试。

Please refer the below link for the workbook, please open 2-3 new workbooks and then try on them together.

推荐答案

所有用户形式为对象。在每次运行时分开它们以具有独立的实例。
尝试这个

All userforms as objects. Separate them at every run to have independent instances.Try this

Private Sub LoadMenu()

Dim frmTemp as MainMenu

设置frmTemp =新建MainMenu

frmTemp.Show

卸载frmTemp

End Sub

这篇关于Userform激活首先启动的工作表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-23 21:44