首页 文章

在excel vba中使用用户表单添加IF规则

提问于
浏览
0

我想让用户能够通过用户表单向我的代码添加一些IF规则,然后运行程序 . 规则是这样的:

If Xarray(i,j-1)= "X" and Xarray(i,j+1)= "Y" Then Xarray(i,j)= "Z"

其中“X”,“Y”和“Z”是用户填写的文本框,使用用户表单并且当用户点击它时有“添加规则”按钮,规则将以编程方式添加到代码中 . 后来我可以将规则扩展到更复杂的规则 . 我的问题是如何为Excel VBA创建此类过程?

谢谢 .

这是一个例子:

我们有一个用户表单,其中有三个 Text-Boxes 和一个 "Add Rule" 按钮 . 我创建了这个代码示例:

Private Sub UserForm_Initialize()

    Dim LeftCell As String, RightCell As String, CenterCell As String
    Dim iRule As String

     UserForm1.Show
    LeftCell = txtLeft.Text
    CenterCell = txtCenter.Text
    RightCell = txtRight.Text

    iRule = "If " & "Xarr(I, J - 1) = " & LeftCell & " And" & " Xarr(I, J + 1) = " & RightCell & " Then" & Chr(10) & _
            " Xarr(I, J) = " & CenterCell & Chr(10) & _
            "End If"

    MsgBox iRule
End Sub

问题是我如何在我的主代码中使用iRule(这是一个字符串)作为“IF语句” .

2 回答

  • 1

    我建议使用2D规则“规则” . 当用户添加规则时,诸如规则类型(相等,不等)和要测试的参数之类的信息将被输入到数组中 . 最后,在进行检查时,您可以使用循环内if..then语句内的参数来测试数组的所有元素 . 如果规则都与AND运算符组合在一起,那么您可以将布尔变量设置为false并退出循环 . 如果您需要更多详细信息或代码示例,请发布一些我可以使用的试用代码 .

    使用代码编辑:

    我创建了一个可以用于此目的的课程:

    Option Explicit
    
    'Class Parameters
    Dim pRules() As Variant 'note the variant data type
    Dim pCountRules As Long
    
    Private Sub class_initialize()
        pCountRules = 0
    End Sub
    
    Public Sub AddRule(Parameter As Variant, Condition As Variant)
    'note the variant data types
        If TypeName(Parameter) <> TypeName(Condition) Then
            'one possible exception I can think of, handle this here
            Exit Sub
        End If
        If pCountRules = 0 Then
            pCountRules = 1
            ReDim pRules(1 To 2, 1 To 1)
            pRules(1, 1) = Parameter
            pRules(2, 1) = Condition
        Else
            pCountRules = pCountRules + 1
            ReDim Preserve pRules(1 To 2, 1 To pCountRules)
            pRules(1, pCountRules) = Parameter
            pRules(2, pCountRules) = Condition
        End If
    End Sub
    
    Public Sub ResetRules()
        Erase pRules
        pCountRules = 0
    End Sub
    
    Public Function CheckRules() As Boolean
    Dim i As Integer
        If pCountRules = 0 Then
            CheckRules = True   'or false, depends on your logic
        Else
            CheckRules = True
            For i = 1 To pCountRules
                If pRules(1, i) <> pRules(2, i) Then
                    CheckRules = False
                    Exit For
                End If
            Next i
        End If
    End Function
    
    Private Sub Class_Terminate()
        Erase pRules
    End Sub
    

    请注意变量数据类型的使用 . 我可以避免这种情况,你需要大量的异常处理 . 如果确定了您的数据类型,则可以更改此数据并包括正确的验证 . 我测试了这个类如下:

    Option Explicit
    
    Sub test()
    Dim Rules As clsRules
    Dim testarr(1 To 1, 1 To 3) As String
    Dim testparam(1 To 3) As String
        testarr(1, 1) = "a"
        testarr(1, 2) = "b"
        testarr(1, 3) = "c"
        testparam(1) = "a"
        testparam(2) = "b"
        testparam(3) = "c"
        'values match
        Set Rules = New clsRules
        Rules.AddRule testarr(1, 1), testparam(1)
        Rules.AddRule testarr(1, 2), testparam(2)
        Rules.AddRule testarr(1, 3), testparam(3)
        'will print true
        Debug.Print Rules.CheckRules
    
        'change parameter so values do not match
        testparam(3) = "a"
        Rules.ResetRules
        Rules.AddRule testarr(1, 1), testparam(1)
        Rules.AddRule testarr(1, 2), testparam(2)
        Rules.AddRule testarr(1, 3), testparam(3)
        'will print false
        Debug.Print Rules.CheckRules
    
        'clean up
        Erase testarr
        Erase testparam
        Set Rules = Nothing
    End Sub
    

    我希望这对你有用 .

  • 0

    我能想到的一种方法是使用您的字符串(您要执行的代码)在新模块中创建子例程并运行该子例程

相关问题