如何在此代码中而不是msgbox中添加用户表单?

问题描述 投票:0回答:1

我目前有此代码

Private Sub Worksheet_Change(ByVal Target As Range)
Dim myCell As Range

For Each myCell In Range("G4:G160")

    If (Not IsEmpty(myCell)) And myCell.Value <> 17521 And myCell.Value <> "" Then

        DisplayUserForm

        Exit Sub
    End If

Next myCell
End Sub

并在我的用户表单中使用它

Sub DisplayUserForm()

Dim form As New WarningBox
form.LOL.Caption = "INCORRECT!"
form.Show
Set form = Nothing

结束子

为了使它出现而不是msgbox来提醒任何输入数据的人,我必须做的其他事情就是显示“ INCORRECT!”。粗体并被红色包围。请查看下面我要显示的图像enter image description here

vba userform msgbox
1个回答
0
投票

请执行以下步骤:

  1. 通过右键单击您的VBA项目并在UserForm选项下选择Insert,插入新表单。enter image description here
  2. 单击创建的表单一次,然后按“ F4 key to open the属性”窗口。enter image description here
  3. Properties窗口上,表单的默认名称为UserForm1。根据需要将其更改为任何新值(例如WarningBox)
  4. ToolBox窗口中,将Label拖放到窗体上,并调整其大小,字体,字体颜色以及Properties窗口中存在的所有其他属性。请将标签重命名为message。稍后在调用要显示的表单时,我将使用此名称。enter image description here
  5. [如果需要,如步骤4一样,将CommandButton添加到表单中,并将其名称更改为例如okButton,然后根据需要调整其他属性。enter image description here
  6. 双击按钮以编写此按钮的代码。编写代码如下:
Private Sub okButton_Click()

    'Close the form
    Unload Me
End Sub
  1. 现在,如下修改您的DisplayUserForm()子:
Sub DisplayUserForm()

    Dim form As New warningBox
    form.message.Caption = "write your message here"
    form.Show
    Set form = Nothing

End Sub

您将根据需要完成所有操作!

© www.soinside.com 2019 - 2024. All rights reserved.