更好的做事方式? VBA

时间:2018-04-24 14:48:01

标签: excel vba function class

我有以下代码。但是,此代码在不同的Userforms上重复多次。但是,此代码中的更改是所有对象的名称,因为它位于不同的userform

Sub FirstOne ()
Call RunFast_Begin
    Me.Position_MYORG = ""
    Select Case Me.TheORG
        Case ""
            Position_elswhere.Visible = False
            'Small text informing the user it needs to select the Branch
            Branch_TEXT.Visible = False
            'Dropdown list of branch and regions at my Org
            Branch.Visible = False
            'Small text informing the user needs to select the Position
            Position_TEXT.Visible = False
            'Dropdown for My OrgPositions
            Position_MYORG.Visible = False
            'Positions drop down for known
            Postion_Drop_Down_Elsewhere.Visible = False

    '* For all departments where there are many positions *
        Case "My Org"
            Me.Position_MYORG.RowSource = "MYORG"
            'in case user selected wrong option first
            Position_elswhere.Visible = False
           Postion_Drop_Down_Elsewhere.Visible = False
            'My Org info
            Branch_TEXT.Visible = True
            Branch.Visible = True
            Position_TEXT.Visible = True
            Position_MYORG.Visible = True
End Select
        Call RunFast_End
        End sub

所以在另一个userform上会出现相同的代码,但如下所示:

Sub SecondOne()
Call RunFast_Begin
    Me.Position_MYORG2 = ""
    Select Case Me.TheORG2
        Case ""
            Position_elswhere2.Visible = False
            'Small text informing the user it needs to select the Branch
            Branch_TEXT2.Visible = False
            'Dropdown list of branch and regions at My Org
            Branch2.Visible = False
            'Small text informing the user needs to select the Position
            Position_TEXT2.Visible = False
            'Dropdown for My OrgPositions
            Position_MYORG2.Visible = False
            'Positions drop down for known
            Postion_Drop_Down_Elsewhere2.Visible = False

    '* For all departments where there are many positions *
        Case "My Org"
            Me.Position_MYORG2.RowSource = "MYORG"
            'in case user selected wrong option first
            Position_elswhere2.Visible = False
           Postion_Drop_Down_Elsewhere2.Visible = False
            'My Org info
            Branch_TEXT2.Visible = True
            Branch2.Visible = True
            Position_TEXT2.Visible = True
            Position_MYORG2.Visible = True
       End select 
Call RunFast_End
           End sub

正如你可以想象的那样,当我需要进行更改时这是一个痛苦...这个代码在三个不同的Userform上,并且它出现了5次,所以基本上每次改变时我都要做15次更改。

我听说过功能和课程,但是不知道,即使你把它弄清楚我怎么能把我的情况与那些相匹配。我知道我不应该要求编码,但我的编码工作,这不是问题。我只是想更聪明地工作,让我的编码对我来说不那么麻烦: - )

非常感谢任何帮助 非常感谢你

1 个答案:

答案 0 :(得分:1)

这是我所谈论的缩写版本:

Sub SetUpControls(frm As Object, i As Long)
    RunFast_Begin
    frm.Controls("Position_MYORG" & i) = ""
    Select Case frm.Controls("TheORG" & i)
        Case ""

            frm.Controls("Position_elswhere" & i).Visible = False
            frm.Controls("Branch_TEXT" & i).Visible = False
            'etc
        Case "My Org"
            frm.Controls("Position_MYORG" & i).RowSource = "MYORG"
            frm.Controls("Position_elswhere" & i).Visible = False
            'etc...
    End Select
    RunFast_End
End Sub

您可以通过任何形式将其称为:

SetUpControls Me, 2