简体   繁体   English

打开弹出消息窗口的编辑工作表

[英]Edit worksheet with popup message window open

I'm using the following macro to open a popup message box pulling in specific cell values. 我正在使用以下宏打开一个弹出消息框,以提取特定的单元格值。 When the message box is open I can't edit the worksheet. 打开消息框后,我将无法编辑工作表。 When I move my mouse pointer off the message box it turns to a spinning circle. 当我将鼠标指针从消息框移开时,它变成一个旋转的圆圈。 How do I open my message box, have it stay open and edit the worksheet underneath? 如何打开我的消息框,使其保持打开状态并编辑下面的工作表?

Sub PopUp()
    Dim vMsg
    Dim r As Integer
    For r = 4 To 16
    vMsg = vMsg & Range("AD" & r).Value & Range("AE" & r).Value & Range("AF" & r).Value & Range("AG" & r).Value & Range("AH" & r).Value & Range("AI" & r).Value & Range("AJ" & r).Value & vbCrLf
    Next
    MsgBox vMsg, , "Values"
End Sub 

The built in MsgBox is modal, which means you cannot manipulate the worksheet until the user dismisses, in some fashion, the message box. 内置的MsgBox是模态的,这意味着您无法操作工作表,直到用户以某种方式关闭了消息框。 The typical workaround appears to be to create your own custom UserForm to display the message and call it with UserForm1.Show vbModeless , which will display the message and allow the user to still edit the worksheet. 典型的解决方法似乎是创建您自己的自定义UserForm来显示消息,并使用UserForm1.Show vbModeless调用它,它将显示消息并允许用户仍然编辑工作表。

However, there is an alternative way than creating your own custom UserForm . 但是,除了创建自己的自定义UserForm之外,还有另一种方法。 You can use the Windows API instead of the VBA interface. 您可以使用Windows API代替VBA接口。

To do this, start by adding this code to the top of your module (before all subroutines and functions): 为此,首先将以下代码添加到模块顶部(在所有子例程和函数之前):

Private Declare Function MessageBox _
    Lib "User32" Alias "MessageBoxA" _
       (ByVal hWnd As Long, _
        ByVal lpText As String, _
        ByVal lpCaption As String, _
        ByVal wType As Long) _
    As Long

The syntax above takes the Windows API function MessageBoxA and allows you to call it by calling MessageBox (it is aliased). 上面的语法采用Windows API函数MessageBoxA并允许您通过调用MessageBox (它是别名)来调用它。

You can then update your PopUp subroutine to use the new MessageBox function. 然后,您可以更新PopUp子例程以使用新的MessageBox函数。

Sub PopUp()
    Dim vMsg
    Dim r As Integer
    For r = 4 To 16
    vMsg = vMsg & Range("AD" & r).Value & Range("AE" & r).Value & Range("AF" & r).Value & Range("AG" & r).Value & Range("AH" & r).Value & Range("AI" & r).Value & Range("AJ" & r).Value & vbCrLf
    Next
    MessageBox &H0, vMsg, "Values", vbOkayOnly
End Sub 

This will allow the user to access the sheet while the message box is displayed. 显示消息框时,这将允许用户访问工作表。 Additionally, the line: 此外,该行:

MessageBox &H0, vMsg, "Values", vbOkayOnly

Can be changed to 可以更改为

MessageBox &H0, vMsg, "Values", vbSystemModal

This will allow you to access the sheet, but will keep the message box on top at all times (it will do this for all applications, not just Excel, but nothing is perfect). 这将允许您访问工作表,但始终将消息框置于顶部(它将对所有应用程序执行此操作,不仅是Excel,而且还不是完美的)。

The MessageBox function takes in four parameters: MessageBox函数采用四个参数:

  • hWnd is the special identifier of the window which owns this message box, the trick is to pass in a null with &H0 so that no window owns it hWnd是拥有此消息框的窗口的特殊标识符,诀窍是用&H0传递一个null,以便没有窗口拥有它
  • lpText is the message to be displayed (same as the prompt parameter of the VBA MsgBox function) lpText是要显示的消息(与VBA MsgBox函数的提示参数相同)
  • lpCaption is the text to be displayed as the title (same as the title parameter of the VBA MsgBox function) lpCaption是要显示为标题的文本(与VBA MsgBox函数的title参数相同)
  • wType is the type of message box that will be displayed (identical to the buttons parameter of the VBA MsgBox function) wType是将显示的消息框的类型(与VBA MsgBox函数的button参数相同)

You can read more here . 您可以在这里阅读更多内容。

Unfortunately, Message boxes are Modal - so they will take the focus until they are dismissed. 不幸的是,消息框是模态的-因此它们将成为焦点,直到它们被消除为止。

The way around this is to create a custom userform which you can use as a messagebox. 解决方法是创建一个自定义用户窗体,您可以将其用作消息框。 There is an option on userforms to make them non-modal - which will allow you to carry on with other actions, while they are open. 用户表单上有一个选项可以使它们成为非模式的-当它们打开时,您可以继续进行其他操作。

I hope that this helps 我希望这个对你有用

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM