VBA-检查特定用户表单是否已加载 [英] VBA- Check if a particular userform is Loaded or not

查看:33
本文介绍了VBA-检查特定用户表单是否已加载的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在运行代码时检查用户表单是否已加载.我尝试了几种方法,没有人工作.我记得我以前这样做过,但我不记得我的解决方法.任何的想法?

I want to check if a userform is loaded or not while running a code. I tried several ways, no-one is working. I remember I did that before, but I can't recall my workaround. Any idea?

我在这里和其他地方找到了一些解决方案,但它们也不起作用!

I found some solutions here and other places, they also was not working!

问题是……

  1. vba.userforms 只接受索引号,不接受字符串索引,
  2. 在用户表单上的循环中,某些属性(如名称)无法访问以进行检查!

这是我的尝试:

Public Function IsFormVisible(FrmName As String) As Boolean
On Error GoTo errorH
    IsFormVisible = False
    Set Frm = UserForms(FrmName)
    If Not Frm Is Nothing Then IsFormVisible = True
    End Function
errorH:
    IsFormVisible = False
End Function

<小时>

Public Function IsFormVisible(FrmName As String) As Boolean
Dim Frm As UserForm
On Error GoTo errorH
    IsFormVisible = False
    For Each Frm In VBA.UserForms
        If Frm.Name = FrmName Then
           IsFormVisible = True
           Exit Function
        End If
    Next
errorH:
    IsFormVisible = False
End Function

推荐答案

这里有两个简单的选项.首先,您可以将 frm 声明为 Object:

Here are two simple options. First, you could declare frm as Object:

Public Function IsFormVisible(FrmName As String) As Boolean
    Dim Frm As Object
On Error GoTo errorH
    IsFormVisible = False
    For Each Frm In VBA.UserForms
        If LCase$(Frm.Name) = LCase$(FrmName) Then
           IsFormVisible = True
           Exit Function
        End If
    Next
errorH:
    IsFormVisible = False
End Function

或者第二,你可以使用 TypeName 而不是 .Name:

Or second, you could use TypeName instead of .Name:

Public Function IsFormVisible(FrmName As String) As Boolean
    Dim Frm As UserForm
'On Error GoTo errorH
    IsFormVisible = False
    For Each Frm In VBA.UserForms
        If lcase$(TypeName(Frm)) = lcase$(FrmName) Then
           IsFormVisible = True
           Exit Function
        End If
    Next
errorH:
    IsFormVisible = False
End Function

我已经使这两个不区分大小写.

I've made both of these case insensitive.

这篇关于VBA-检查特定用户表单是否已加载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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