在MS Excel中嵌套If / Then VB语句

时间:2013-04-24 16:18:47

标签: excel excel-vba vba

我已经和几个人一起工作了一个星期,他们有一些VB经验,但我们无法让所有这些If / Then语句正常工作。所有工作除了引用“otype”+ SCE + CNE和“otype”=专用线路或仅限Internet的语句之外。逻辑似乎是正确的,但我们没有得到预期的结果。救命!下面是我们有问题的VB代码。谢谢。

Private Sub Worksheet_Activate()

End Sub

Private Sub Worksheet_Change(ByVal Target As Range)

    Rem CNE Customer
    If Range("otype") = "SCE+ CNE" Then
        Worksheets("VNC Form").Visible = True
        Worksheets("ISC Portal").Visible = True
        Worksheets("ISC Request Only").Visible = False
        Worksheets("VPN Details").Visible = False

    Else
        Worksheets("VNC Form").Visible = False
        Worksheets("ISC Portal").Visible = False
    End If

    Rem ISM ABC Customer
    If Range("otype") = "SC4ABC" Then
        Worksheets("ISM-ABC Customers").Visible = True
        Worksheets("ISM Portal").Visible = True

    Else
        Worksheets("ISM-ABC Customers").Visible = False
    End If


    Rem VPN Details tab
    If Range("ctype") = "VPN" Then
        Worksheets("VPN Details").Visible = True
        Worksheets("ISC Portal").Visible = True
        Worksheets("VNC Form").Visible = False

    Else
        Worksheets("VPN Details").Visible = False
        Worksheets("ISC Portal").Visible = False
        Worksheets("VNC Form").Visible = False
    End If

    Rem Connections tabs
    If Range("ctype") = "Dedicated Line" Or Range("ctype") = "Internet Only" Then
        Worksheets("ISC Portal").Visible = True
        Worksheets("VPN Details").Visible = False

    Else
        Worksheets("ISC Portal").Visible = False
        Worksheets("VPN Details").Visible = False
    End If

    Rem AT&T and Connection tabs
    If Range("otype") = "SCE+ Reseller" And Range("ctype") = "CNE" Or Range("otype") = "SCE+ Internal" And Range("ctype") = "CNE" Then
        Worksheets("ISC Request Only").Visible = True
        Worksheets("VNC Form").Visible = True
        Worksheets("ISC Portal").Visible = False
        Worksheets("VPN Details").Visible = False

    Else
        Worksheets("ISC Request Only").Visible = False
        Worksheets("VNC Form").Visible = False
        Worksheets("ISC Portal").Visible = True
        Worksheets("VPN Details").Visible = True
    End If

End Sub

1 个答案:

答案 0 :(得分:0)

虽然下面的代码可能有所帮助,但我建议使用QAT上的下拉菜单或功能区上的按钮,以允许用户决定取消隐藏/隐藏哪些工作表。您的代码将以每次运行修改工作表中的任何单元格。

正如Sid建议的那样,可能值得将您的代码封装在CASE语句中,以便仅在Target = otype或ctype ...

时运行

可能会尝试强制执行此操作,并强制空格为零,如下所示:

Rem CNE Customer  

If Trim(UCase(replace(Range("otype").TEXT," ",""))) = "SCE+CNE" Then  
    Worksheets("VNC Form").Visible = True
    Worksheets("ISC Portal").Visible = True
    Worksheets("ISC Request Only").Visible = False
    Worksheets("VPN Details").Visible = False

Else
    Worksheets("VNC Form").Visible = False
    Worksheets("ISC Portal").Visible = False
End If

Rem Connections tabs
If (TRIM(UCase(replace(Range("ctype")," ",""))) = "DEDICATEDLINE") Or (TRIM(UCASE(REPLACE(Range("ctype")," ",""))) = "INTERNETONLY") Then
    Worksheets("ISC Portal").Visible = True
    Worksheets("VPN Details").Visible = False

Else
    Worksheets("ISC Portal").Visible = False
    Worksheets("VPN Details").Visible = False
End If

Rem AT&T and Connection tabs
If (TRIM(UCase(replace(Range("otype")," ",""))) = "SCE+RESELLER" And TRIM(UCase(replace(Range("ctype")," ",""))) = "CNE") Or (TRIM(UCase(replace(Range("otype")," ",""))) = "SCE+INTERNAL" And TRIM(UCase(replace(Range("ctype")," ",""))) = "CNE") Then
    Worksheets("ISC Request Only").Visible = True
    Worksheets("VNC Form").Visible = True
    Worksheets("ISC Portal").Visible = False
    Worksheets("VPN Details").Visible = False

Else
    Worksheets("ISC Request Only").Visible = False
    Worksheets("VNC Form").Visible = False
    Worksheets("ISC Portal").Visible = True
    Worksheets("VPN Details").Visible = True
End If