Excel 创建通用命令以清除userform中的值
我正在学习创建一个公共sub,可以在任何用户窗体中使用它来清除指定的值。 但是,我遇到了一个错误<代码>“Me”。不能作为对指定用户表单的引用 我读了另一篇文章,但还是不明白。正确的参考方法是什么 这是当前的代码 模块内Excel 创建通用命令以清除userform中的值,excel,vba,Excel,Vba,我正在学习创建一个公共sub,可以在任何用户窗体中使用它来清除指定的值。 但是,我遇到了一个错误“Me”。不能作为对指定用户表单的引用 我读了另一篇文章,但还是不明白。正确的参考方法是什么 这是当前的代码 模块内 Public Sub ClrV() Dim ctrl As Control For Each ctrl In Me.Controls Select Case TypeName(ctrl) Case Is = "TextBox"
Public Sub ClrV()
Dim ctrl As Control
For Each ctrl In Me.Controls
Select Case TypeName(ctrl)
Case Is = "TextBox"
If ctrl.Name <> "txtDate" Then ctrl.Value = ""
Case Is = "Label"
If ctrl.Name Like "Label*" Or ctrl.Name = "Title" Then
Else
ctrl.Caption = ""
End If
End Select
Next ctrl
End Sub
deleted as it is moved to the userform code
您可以将表单作为
对象
传递给public sub,然后迭代所有控件并根据需要执行操作。将以下代码写入标准模块
Public Sub ClrV(frm As Object)
Dim ctrl As Control
For Each ctrl In frm.Controls
Select Case TypeName(ctrl)
Case Is = "TextBox"
If ctrl.Name <> "txtDate" Then ctrl.Value = ""
Case Is = "Label"
If ctrl.Name Like "Label*" Or ctrl.Name = "Title" Then
Else
ctrl.Caption = ""
End If
End Select
Next ctrl
End Sub
如果您不想提及UserForm1
,请通过关键字me
传递当前表单-
Private Sub CommandButton1_Click()
Call ClrV(Me)
End Sub
您可以将表单作为
对象
传递给public sub,然后迭代所有控件并根据需要执行操作。将以下代码写入标准模块
Public Sub ClrV(frm As Object)
Dim ctrl As Control
For Each ctrl In frm.Controls
Select Case TypeName(ctrl)
Case Is = "TextBox"
If ctrl.Name <> "txtDate" Then ctrl.Value = ""
Case Is = "Label"
If ctrl.Name Like "Label*" Or ctrl.Name = "Title" Then
Else
ctrl.Caption = ""
End If
End Select
Next ctrl
End Sub
如果您不想提及UserForm1
,请通过关键字me
传递当前表单-
Private Sub CommandButton1_Click()
Call ClrV(Me)
End Sub
你的方法有很多问题
Public Sub ClrV()
Dim ctrl As Control
For Each ctrl In Me.Controls
Select Case TypeName(ctrl)
Case Is = "TextBox"
If ctrl.Name <> "txtDate" Then ctrl.Value = ""
Case Is = "Label"
If ctrl.Name Like "Label*" Or ctrl.Name = "Title" Then
Else
ctrl.Caption = ""
End If
End Select
Next ctrl
End Sub
deleted as it is moved to the userform code
在用户表单代码中
Public Sub ClrV()
Dim ctrl As Control
For Each ctrl In Me.Controls
Select Case TypeName(ctrl)
Case "TextBox"
If ctrl.Name <> "txtDate" Then ctrl.Value = vbnullstring
Case "Label"
If NOT (ctrl.Name Like "Label*" Or ctrl.Name = "Title") Then
ctrl.Caption = vbnullstring
End If
End Select
Next ctrl
End Sub
公共小组委员会
按ctrl键作为控件
对于Me.Controls中的每个ctrl键
选择案例类型名称(ctrl)
案例“文本框”
如果ctrl.Name“txtDate”,则ctrl.Value=vbnullstring
案例“标签”
如果不是(ctrl.Name如“Label*”或ctrl.Name=“Title”),则
ctrl.Caption=vbnullstring
如果结束
结束选择
下一个ctrl键
端接头
您的方法有很多问题
Public Sub ClrV()
Dim ctrl As Control
For Each ctrl In Me.Controls
Select Case TypeName(ctrl)
Case Is = "TextBox"
If ctrl.Name <> "txtDate" Then ctrl.Value = ""
Case Is = "Label"
If ctrl.Name Like "Label*" Or ctrl.Name = "Title" Then
Else
ctrl.Caption = ""
End If
End Select
Next ctrl
End Sub
deleted as it is moved to the userform code
在用户表单代码中
Public Sub ClrV()
Dim ctrl As Control
For Each ctrl In Me.Controls
Select Case TypeName(ctrl)
Case "TextBox"
If ctrl.Name <> "txtDate" Then ctrl.Value = vbnullstring
Case "Label"
If NOT (ctrl.Name Like "Label*" Or ctrl.Name = "Title") Then
ctrl.Caption = vbnullstring
End If
End Select
Next ctrl
End Sub
公共小组委员会
按ctrl键作为控件
对于Me.Controls中的每个ctrl键
选择案例类型名称(ctrl)
案例“文本框”
如果ctrl.Name“txtDate”,则ctrl.Value=vbnullstring
案例“标签”
如果不是(ctrl.Name如“Label*”或ctrl.Name=“Title”),则
ctrl.Caption=vbnullstring
如果结束
结束选择
下一个ctrl键
端接头
Me命令始终是对代码所在模块的引用。因此,如果您的sub不在userform模块中,那么它将无法工作。只需将Me.Controls
更改为您的用户名(例如Userform1.Controls
),它就会消除这个问题。是的。我知道这一点。但这不允许我将public sub用于任何我想要的用户表单。有解决办法吗?类似于excel公式中的间接命令?Me
命令始终是对代码所在模块的引用。因此,如果您的sub不在userform模块中,那么它将无法工作。只需将Me.Controls
更改为您的用户名(例如Userform1.Controls
),它就会消除这个问题。是的。我知道这一点。但这不允许我将public sub用于任何我想要的用户表单。有解决办法吗?类似于excel公式中的间接?这是我过去的做法,尽管我的目标是实际控件类型而不是名称。这是我过去的做法,尽管我的目标是实际控件类型而不是名称。您的解释很好,但如果您在表单模块中编写代码,则必须反复编写相同的代码每一张表格。是的。这就是我在回答中所说的。我还说过,如果代码是以通用方式编写的,那么您可以使用c