Excel 用变量替换用户名
我在“Module1”中设置了以下函数,用于确定用户表单中名为“frmAddRecord2”的文本框可以输入的最大值 txtLV和txtMaxLV是“frmAddRecord2”下的文本框 txtMaxLV_Pass也是一个放在frmAddRecord2下的布尔变量。在这个阶段,这个函数可以正常工作Excel 用变量替换用户名,excel,vba,Excel,Vba,我在“Module1”中设置了以下函数,用于确定用户表单中名为“frmAddRecord2”的文本框可以输入的最大值 txtLV和txtMaxLV是“frmAddRecord2”下的文本框 txtMaxLV_Pass也是一个放在frmAddRecord2下的布尔变量。在这个阶段,这个函数可以正常工作 Public Function txtLV_Max() As Long With frmAddRecord2 If .txtLV.Value >= 99 Or Not
Public Function txtLV_Max() As Long
With frmAddRecord2
If .txtLV.Value >= 99 Or Not .txtMaxLV_Pass Then
txtLV_Max = 109
Else
txtLV_Max = .txtMaxLV.Value - 1
End If
End With
End Function
因为我会有“frmAddRecord1”,“frmAddRecord2”,“frmAddRecord3”。。。等等,所以我想在frmAddRecord1或frmAddRecord2时调用以下sub
已激活
Public Sub SetActiveUserForm(Optional UserFormName As String)
If UserFormName = "frmAddRecord1" Then
Set ActiveUserForm = frmAddRecord1
UserFormShown = True
ElseIf UserFormName = "frmAddRecord2" Then
Set ActiveUserForm = frmAddRecord2
UserFormShown = True
Else
Set ActiveUserForm = Nothing
UserFormShown = False
End If
End Sub
我想将该职能重组为:
Public Function txtLV_Max() As Long
With activeuserform
If .txtLV.Value >= 99 Or Not .txtMaxLV_Pass Then
txtLV_Max = 109
Else
txtLV_Max = .txtMaxLV.Value - 1
End If
End With
End Function
但是,在“If.txtLV.Value>=99或不.txtMaxLV\u PassThen”行出现错误。经过测试,在我重新构造函数后,发现调用txtMaxLV_过程失败。如果我将txtMaxLV_Pass移动到Module1下的public,它又可以正常工作了
但是我想问,如果我想在userform下保持txtMaxLV_传递,我应该在声明userform变量时做什么更改。请建议,我已经在网站和书籍中研究过这个问题,但仍然无法解决。感谢您的帮助。全局变量
ActiveUserForm
的类型为Userform
。但是,要从UserForm变量访问UserForm属性,它实际上需要是Object
或Variant
类型,您的代码模块顶部是否有选项explicit
?ActiveUserForm
是您正在引用的全局变量吗?是的,我将option explicit放在模块1和frmAddRecord2的顶部。ActiveUserForm是模块1中的全局变量;txtMaxLv是frmAddRecord2中的全局变量。当函数使用时,userform仍然显示在屏幕上。您有什么类型的变量ActiveUserForm
as?我认为它可能需要是Variant
才能访问userform属性。我将ActiveUserForm设置为userform尝试将Dim ActiveUserForm更改为userform
以Dim ActiveUserForm作为Object
。