以模块化格式调用函数和子函数的问题

时间:2018-06-16 18:10:49

标签: excel-vba vba excel

我将整合几个类似结构的子如下:

Private Sub txtToNextLVEXP_Recheck()

    If txtToNextLVEXP.Value Like "ABC" Then
        txtToNextLVEXP.Value = txtToNextLVEXP_Min & " AND " & txtToNextLVEXP_Max
        txtToNextLVEXP_check
    End If

End Sub

Private Sub txtTargetLV_Recheck()

    If txtTargetLV.Value Like "ABC" Then
        txtTargetLV.Value = txtTargetLV_Min & " And " & txtTargetLV_Max
        txtTargetLV_check
    End If

End Sub

txtToNextLVEXP_Min,txtToNextLVEXP_Max,txtTargetLV_Min和txtTargetLV_Max是函数; txtTargetLV和txtTargetLV是MSForms.Control; txtToNextLVEXP_check和txtTargetLV_check是Sub(s)。

可以将它们合并为以下内容吗?

Private Sub Recheck(target as MSForms.Control)

    If target.Value Like "ABC" Then
        target.Value = (target & "_Min") & " AND " & (target & "_Max")
        target & "_check"
    End If

End Sub

我坚持重命名target_Min,target_max和target_check,请提出建议,非常感谢。

3 个答案:

答案 0 :(得分:0)

我会选择类似的东西:

Private Sub Recheck(target As MSForms.Control)
Dim strTarget As String
    strTarget = "txtTargetLV"           'or the bellow?
    'strTarget = target.Name

With yourFormName                       'if you are not on userform, you can access controls via userform name, otherwise "Me" will do
    If target.Value Like "ABC" Then
        target.Value = .Controls(strTarget & "_Min").Value & " AND " & .Controls(strTarget & "_Max").Value
        Application.Run (strTarget & "_check")       'If you are trying to call a function based on the name of your control...
    End If
End With

End Sub

我不是100%确定我理解你想要实现的目标,但希望这会有所帮助。

答案 1 :(得分:0)

如果您的功能在userform代码模块中,您可以这样做:

Private Sub cmCheck_Click()
    Recheck Me.txtA
    Recheck Me.txtB
End Sub

Private Sub Recheck(target As MSForms.Control)
    If target.Value Like "*ABC*" Then

        'Use CallByName to run the functions...
        target.Value = CallByName(Me, target.Name & "_Min", VbMethod) & " AND " & _
                       CallByName(Me, target.Name & "_Max", VbMethod) & " (check)"

        CallByName Me, target.Name & "_check", VbMethod

    End If
End Sub

'functions being called start here....
Function txtA_Min()
    txtA_Min = 1
End Function

Function txtA_Max()
    txtA_Max = 10
End Function

Function txtB_Min()
    txtB_Min = 11
End Function

Function txtB_Max()
    txtB_Max = 20
End Function

答案 2 :(得分:0)

很抱歉再次烦恼,callbyname函数非常好,我的程序运行正常,结构类似于以下程序:

Public Function InputCorr(Target As MSForms.Control) As Boolean

    If FrmAddRecord1Shown Then
        Target.Value = CallByName(frmAddRecord1, Target.Name & "_Min", VbMethod)
    ElseIf FrmAddRecord2Shown Then
        Target.Value = CallByName(frmAddRecord2, Target.Name & "_Min", VbMethod)
    End If

End Function

但是我想进一步缩小它是这样的:

Public Function InputCorr(Target As MSForms.Control) As Boolean

    Dim UF As UserForm

    If FrmAddRecord1Shown Then
        set UF = frmAddRecord1
    ElseIf FrmAddRecord2Shown Then
        set UF = frmAddRecord2
    EndIf 

    Target.Value = CallByName(UF, Target.Name & "_Min", VbMethod)

End Function

FrmAddRecord1Shown和FrmAddRecord2Shown是布尔值,表示哪个userform(frmAddRecord1或frmAddRecord2)处于活动状态。

我被困在" UF"部分。非常感谢你们真诚的帮助。

相关问题