如何将用户表单而不是 msgbox 添加到此代码中? [英] How to add userform into this code instead of msgbox?

查看:9
本文介绍了如何将用户表单而不是 msgbox 添加到此代码中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前有这个代码

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

并将其用于我的用户表单

and have this for my userform

Sub DisplayUserForm()

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

结束子

我还必须做什么才能让这个出现而不是 msgbox 提醒正在输入数据的人将显示不正确!"以粗体和红色包围.请看下面我想展示的图片

What else must I do in order for this to appear instead of msgbox to alert whoever is entering data will be showing "INCORRECT!" in bold and Surrounded by red. Please see image below of what I am trying to show

推荐答案

请按照以下步骤操作:

  1. 通过右键单击您的 VBA 项目并选择 Insert 选项下的 UserForm 来插入新表单.
  2. 在创建的表单上单击一次,然后按F4键打开Properties"窗口.
  3. Properties 窗口中,表单的默认名称是UserForm1.根据需要将其更改为任何新值(例如,WarningBox)
  4. ToolBox 窗口中,将 Label 拖放到您的表单上并调整其大小、字体、字体颜色以及 上存在的所有其他属性> 属性 窗口.请将标签重命名为 message.稍后我将在调用要显示的表单时使用此名称.
  5. 如果需要,像第 4 步一样,将 CommandButton 添加到表单并将其名称更改为例如 okButton 并根据需要调整其他属性.
  6. 双击按钮为该按钮编写代码.编写代码如下:
  1. Insert a new Form by right-clicking on your VBA project and selecting UserForm under the Insert option.
  2. Click once on the created form and then press the ``F4key to open theProperties``` window.
  3. On the Properties window, the default name for your form is UserForm1. Change it to any new value as you want (e.g., WarningBox)
  4. From the ToolBox window, drag and drop a Label on your form and adjust its size, font, font color, and all other properties that exist on the Properties window. Please rename the label to message. I will use this name later when calling the form to be shown.
  5. If you want, like step 4, add a CommandButton to your form and change its name to for example okButton and adjust other properties as you want.
  6. Double click on the button to write the code for this button. Write the code as follows:

Private Sub okButton_Click()

    'Close the form
    Unload Me
End Sub

  1. 现在,修改您的 DisplayUserForm() 子项,如下所示:
  1. Now, modify your DisplayUserForm() sub as follows:

Sub DisplayUserForm()

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

End Sub

一切都会如你所愿!

这篇关于如何将用户表单而不是 msgbox 添加到此代码中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆