Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/blackberry/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Excel VBA公式作为变量_Excel_Vba_If Statement_Variables - Fatal编程技术网

Excel VBA公式作为变量

Excel VBA公式作为变量,excel,vba,if-statement,variables,Excel,Vba,If Statement,Variables,我正在编写一个VBA表单,它查看一张数据表,并根据所选的下拉框缩小选择范围。数据表包含不同标题下每个下拉框的选项,例如 下拉框1-请选择您的公司->缩小下拉框2的内容范围 下拉框2-请选择您的部门->缩小下拉框3的内容范围 下拉框3-是否提出或批准->缩小到可选择的列表 我利用脚本字典和许多嵌套的if语句实现了上述功能,如下所示。基本上,这会用符合相关条件的任何行填充字典 有没有更整洁的方法?这意味着每次进入嵌套的if语句时都要检查完整的条件,因此 If Not Dic.Exists(rCe

我正在编写一个VBA表单,它查看一张数据表,并根据所选的下拉框缩小选择范围。数据表包含不同标题下每个下拉框的选项,例如

  • 下拉框1-请选择您的公司->缩小下拉框2的内容范围
  • 下拉框2-请选择您的部门->缩小下拉框3的内容范围
  • 下拉框3-是否提出或批准->缩小到可选择的列表
我利用脚本字典和许多嵌套的if语句实现了上述功能,如下所示。基本上,这会用符合相关条件的任何行填充字典

有没有更整洁的方法?这意味着每次进入嵌套的if语句时都要检查完整的条件,因此

If Not Dic.Exists(rCell.Value) And rCell.Value...
语句在每个if语句下越来越长

我想知道是否有办法将该行的组件作为变量嵌入,例如

If Not Dic.Exists(rCell.Value) And rCell.Value <> "" And Statement1 And Statement2 
然后,我可以根据不同的If值更改语句,因此如果我们查看display access,则应该是上面的内容,但如果要批准,则应该是上面的内容

Statement 1 = rCell.Offset(0, -2).Value = CompanySelect.Value
Statement 2 = rCell.Offset(0, 1).Value = "Approval"
当前代码:

If CompanySelect.Value <> "" Then

If SAPDisplay.Value = False And SRMUse.Value = False And SAPEdit.Value = False Then
RoleSelector.RoleList.Clear
For Each rCell In ws.Range("C2", ws.Cells(Rows.Count, "C").End(xlUp))
    If Not Dic.Exists(rCell.Value) And rCell.Value <> "" And rCell.Offset(0, -2).Value = CompanySelect.Value And rCell.Offset(0, -2).Value = CompanySelect.Value Then
    Dic.Add rCell.Value, Nothing
    End If
Next rCell
For Each Key In Dic
    RoleSelector.RoleList.AddItem Key
Next
    Dic.RemoveAll

ElseIf SAPDisplay.Value = True Then
If SRMUse = False Then
    RoleSelector.RoleList.Clear
    For Each rCell In ws.Range("C2", ws.Cells(Rows.Count, "C").End(xlUp))
        If Not Dic.Exists(rCell.Value) And rCell.Value <> "" And rCell.Offset(0, -2).Value = CompanySelect.Value And rCell.Offset(0, -2).Value = CompanySelect.Value And rCell.Offset(0, 1).Value = "Display access" And rCell.Offset(0, 2).Value = "N/A" Then
            Dic.Add rCell.Value, Nothing
        End If
    Next rCell
    For Each Key In Dic
        RoleSelector.RoleList.AddItem Key
    Next
    Dic.RemoveAll

    ElseIf SRMUse = True Then

        If SRMDisplay.Value = False And SRMCreate.Value = False And SRMApprove.Value = False Then
        RoleSelector.RoleList.Clear
        For Each rCell In ws.Range("C2", ws.Cells(Rows.Count, "C").End(xlUp))
            If Not Dic.Exists(rCell.Value) And rCell.Value <> "" And rCell.Offset(0, -2).Value = CompanySelect.Value And rCell.Offset(0, -2).Value = CompanySelect.Value And rCell.Offset(0, 1).Value = "Display access" And rCell.Offset(0, 2).Value <> "N/A" Then
                Dic.Add rCell.Value, Nothing
            End If
            Next rCell
            For Each Key In Dic
            RoleSelector.RoleList.AddItem Key
            Next
            Dic.RemoveAll
如果公司选择.Value“”,则
如果sapsdisplay.Value=False,SRMUse.Value=False,SAPEdit.Value=False,则
角色选择器。角色列表。清除
对于ws.范围内的每个rCell(“C2”,ws.单元格(行数,“C”)。结束(xlUp))
如果不存在Dic.Exists(rCell.Value)和rCell.Value“”以及rCell.Offset(0,-2)。Value=CompanySelect.Value和rCell.Offset(0,-2)。Value=CompanySelect.Value,则
Dic.添加rCell.值,无
如果结束
下一个rCell
对于Dic中的每个键
RoleSelector.RoleList.AddItem键
下一个
雷莫维尔
ElseIf sapsdisplay.Value=True然后
如果SRMUse=False,则
角色选择器。角色列表。清除
对于ws.范围内的每个rCell(“C2”,ws.单元格(行数,“C”)。结束(xlUp))
如果不存在Dic.Exists(rCell.Value)和rCell.Value“”和rCell.Offset(0,-2)。Value=CompanySelect.Value和rCell.Offset(0,-2)。Value=CompanySelect.Value和rCell.Offset(0,1)。Value=“显示访问”和rCell.Offset(0,2)。Value=“N/A”则
Dic.添加rCell.值,无
如果结束
下一个rCell
对于Dic中的每个键
RoleSelector.RoleList.AddItem键
下一个
雷莫维尔
ElseIf SRMUse=则为真
如果SRMDisplay.Value=False,SRMCreate.Value=False,srmaprove.Value=False,则
角色选择器。角色列表。清除
对于ws.范围内的每个rCell(“C2”,ws.单元格(行数,“C”)。结束(xlUp))
如果不存在Dic.Exists(rCell.Value)和rCell.Value“”和rCell.Offset(0,-2)。Value=CompanySelect.Value和rCell.Offset(0,-2)。Value=CompanySelect.Value和rCell.Offset(0,1)。Value=“显示访问”和rCell.Offset(0,2)。Value“不适用”
Dic.添加rCell.值,无
如果结束
下一个rCell
对于Dic中的每个键
RoleSelector.RoleList.AddItem键
下一个
雷莫维尔

您可以使用
Boolean
变量,是的。
If CompanySelect.Value <> "" Then

If SAPDisplay.Value = False And SRMUse.Value = False And SAPEdit.Value = False Then
RoleSelector.RoleList.Clear
For Each rCell In ws.Range("C2", ws.Cells(Rows.Count, "C").End(xlUp))
    If Not Dic.Exists(rCell.Value) And rCell.Value <> "" And rCell.Offset(0, -2).Value = CompanySelect.Value And rCell.Offset(0, -2).Value = CompanySelect.Value Then
    Dic.Add rCell.Value, Nothing
    End If
Next rCell
For Each Key In Dic
    RoleSelector.RoleList.AddItem Key
Next
    Dic.RemoveAll

ElseIf SAPDisplay.Value = True Then
If SRMUse = False Then
    RoleSelector.RoleList.Clear
    For Each rCell In ws.Range("C2", ws.Cells(Rows.Count, "C").End(xlUp))
        If Not Dic.Exists(rCell.Value) And rCell.Value <> "" And rCell.Offset(0, -2).Value = CompanySelect.Value And rCell.Offset(0, -2).Value = CompanySelect.Value And rCell.Offset(0, 1).Value = "Display access" And rCell.Offset(0, 2).Value = "N/A" Then
            Dic.Add rCell.Value, Nothing
        End If
    Next rCell
    For Each Key In Dic
        RoleSelector.RoleList.AddItem Key
    Next
    Dic.RemoveAll

    ElseIf SRMUse = True Then

        If SRMDisplay.Value = False And SRMCreate.Value = False And SRMApprove.Value = False Then
        RoleSelector.RoleList.Clear
        For Each rCell In ws.Range("C2", ws.Cells(Rows.Count, "C").End(xlUp))
            If Not Dic.Exists(rCell.Value) And rCell.Value <> "" And rCell.Offset(0, -2).Value = CompanySelect.Value And rCell.Offset(0, -2).Value = CompanySelect.Value And rCell.Offset(0, 1).Value = "Display access" And rCell.Offset(0, 2).Value <> "N/A" Then
                Dic.Add rCell.Value, Nothing
            End If
            Next rCell
            For Each Key In Dic
            RoleSelector.RoleList.AddItem Key
            Next
            Dic.RemoveAll