从第二个下拉列表中选择时,隐藏在下拉列表选项上的行将被取消隐藏

时间:2016-12-14 07:57:05

标签: excel vba excel-vba rows

在工作表“输入”中,我有两个下拉列表。第一个是在支付之间进行选择,第二个是在观察类型之间进行选择。

在工作表“输出”中隐藏行。

当我选择一个支付时,隐藏指定的行是好的但是在选择了观察类型后,支付的行被取消隐藏,这是不好的。

我正在使用以下代码:

With Sheets("Output")
Sheets("Output").Rows("112:137").Hidden = False
Sheets("Output").Rows("138:151").Hidden = False
Select Case Target.Value
Case "Bonus Capped Single Index"
Sheets("Output").Rows("112:154").Hidden = True
Case "Bonus Capped Single Share"
Sheets("Output").Rows("112:154").Hidden = True
Case "Bonus Uncapped Single Index"
Sheets("Output").Rows("112:154").Hidden = True
Sheets("Output").Rows("256:257").Hidden = True
Case "Bonus Uncapped Single Share"
Sheets("Output").Rows("112:154").Hidden = True
Sheets("Output").Rows("256:257").Hidden = True
Case "Bonus Uncapped Single Index"
Sheets("Output").Rows("112:154").Hidden = True
Sheets("Output").Rows("256:257").Hidden = True
Case "Bonus Capped Worst of Indices"
Sheets("Output").Rows("112:154").Hidden = True
Case "Bonus Capped Worst of Shares"
Sheets("Output").Rows("112:154").Hidden = True
Case "Bonus Uncapped Worst of Indices"
Sheets("Output").Rows("112:154").Hidden = True
Sheets("Output").Rows("256:257").Hidden = True
Case "Bonus Uncapped Worst of Shares"
Sheets("Output").Rows("112:154").Hidden = True
Sheets("Output").Rows("256:257").Hidden = True
Case "Phoenix Single Share"
Sheets("Output").Rows("116:133").Hidden = True
Sheets("Output").Rows("139").Hidden = True
Sheets("Output").Rows("254:257").Hidden = True    
Case "Phoenix Single Index"
Sheets("Output").Rows("116:133").Hidden = True
Sheets("Output").Rows("138").Hidden = True
Sheets("Output").Rows("254:257").Hidden = True
Case "Phoenix Yeti Single Index"
Sheets("Output").Rows("138").Hidden = True
Sheets("Output").Rows("116:131").Hidden = True
Sheets("Output").Rows("254:257").Hidden = True
Case "Phoenix Yeti Single Share"
Sheets("Output").Rows("139").Hidden = True
Sheets("Output").Rows("116:131").Hidden = True
Sheets("Output").Rows("254:257").Hidden = True
Case "Worst of Indices Phoenix"
Sheets("Output").Rows("138").Hidden = True
Sheets("Output").Rows("254:257").Hidden = True
Sheets("Output").Rows("116:131").Hidden = True
Case "Worst of Shares Phoenix"
Sheets("Output").Rows("138").Hidden = True
Sheets("Output").Rows("254:257").Hidden = True
Sheets("Output").Rows("116:131").Hidden = True
Case "Worst of Shares Phoenix Yeti"
Sheets("Output").Rows("116:131").Hidden = True
Sheets("Output").Rows("138").Hidden = True
Sheets("Output").Rows("254:257").Hidden = True
Case "Worst of Indices Phoenix Yeti"
Sheets("Output").Rows("139").Hidden = True
Sheets("Output").Rows("116:131").Hidden = True
Sheets("Output").Rows("254:257").Hidden = True
Case "Autocall Single Index"
Sheets("Output").Rows("254:257").Hidden = True
Sheets("Output").Rows("112:131").Hidden = True
Sheets("Output").Rows("138").Hidden = True
Case "Autocall Single Share"
Sheets("Output").Rows("254:257").Hidden = True
Sheets("Output").Rows("112:131").Hidden = True
Sheets("Output").Rows("139").Hidden = True
Case "Autocall Worst of Shares"
Sheets("Output").Rows("254:257").Hidden = True    
Sheets("Output").Rows("112:131").Hidden = True
Sheets("Output").Rows("139").Hidden = True
Case "Autocall Worst of Indices"
Sheets("Output").Rows("254:257").Hidden = True
Sheets("Output").Rows("112:131").Hidden = True
Sheets("Output").Rows("138").Hidden = True
Case "Reverse Convertible Single Share"
Sheets("Output").Rows("116:133").Hidden = True
Sheets("Output").Rows("138:154").Hidden = True
Sheets("Output").Rows("254:257").Hidden = True
Sheets("Output").Rows("162:164").Hidden = True
Sheets("Output").Rows("173:175").Hidden = True
Case "Reverse Convertible Single Index"
Sheets("Output").Rows("116:133").Hidden = True
Sheets("Output").Rows("138:154").Hidden = True    
Sheets("Output").Rows("254:257").Hidden = True
Sheets("Output").Rows("162:164").Hidden = True
Sheets("Output").Rows("173:175").Hidden = True
Case "Worst of Shares Reverse Convertible"
Sheets("Output").Rows("116:133").Hidden = True
Sheets("Output").Rows("138:154").Hidden = True
Sheets("Output").Rows("254:257").Hidden = True
Sheets("Output").Rows("162:164").Hidden = True
Sheets("Output").Rows("173:175").Hidden = True
Case "Worst of Indices Reverse Convertible"
Sheets("Output").Rows("116:133").Hidden = True
Sheets("Output").Rows("138:154").Hidden = True    
Sheets("Output").Rows("254:257").Hidden = True
Sheets("Output").Rows("162:164").Hidden = True
Sheets("Output").Rows("173:175").Hidden = True
Case "Coupon Linker Index"
Sheets("Output").Rows("160:164").Hidden = True
Sheets("Output").Rows("254:257").Hidden = True
Sheets("Output").Rows("171:175").Hidden = True
Sheets("Output").Rows("116:133").Hidden = True
Sheets("Output").Rows("138:154").Hidden = True
Case "Fix Coupon Express Single Index"
Sheets("Output").Rows("116:133").Hidden = True
Sheets("Output").Rows("254:257").Hidden = True
Sheets("Output").Rows("136:137").Hidden = True
Sheets("Output").Rows("139").Hidden = True
Case "Fix Coupon Express Single Share"
Sheets("Output").Rows("116:133").Hidden = True
Sheets("Output").Rows("254:257").Hidden = True
Sheets("Output").Rows("136:137").Hidden = True
Sheets("Output").Rows("138").Hidden = True
Case "Fix Coupon Worst of Shares"
Sheets("Output").Rows("116:133").Hidden = True
Sheets("Output").Rows("254:257").Hidden = True
Sheets("Output").Rows("136:137").Hidden = True
Sheets("Output").Rows("138").Hidden = True
Case "Fix Coupon Worst of Indices"
Sheets("Output").Rows("116:133").Hidden = True
Sheets("Output").Rows("254:257").Hidden = True
Sheets("Output").Rows("136:137").Hidden = True
Sheets("Output").Rows("138").Hidden = True
Case Else
Sheets("Output").Rows("112:137").Hidden = False
Sheets("Output").Rows("138:151").Hidden = False
End Select
End With

With Sheets("Output") 'Observation and delivery
Sheets("Output").Rows("158:212").Hidden = False
Sheets("Output").Rows("246:253").Hidden = False
Select Case Target.Value
Case "American Cash"
Sheets("Output").Rows("177:212").Hidden = True
Sheets("Output").Rows("165").Hidden = True
Sheets("Output").Rows("176").Hidden = True
Case "American Physical"
Sheets("Output").Rows("162:164").Hidden = True
Sheets("Output").Rows("177:212").Hidden = True
Sheets("Output").Rows("173:175").Hidden = True
Sheets("Output").Rows("246:253").Hidden = True
Case "European Cash"
Sheets("Output").Rows("170:172").Hidden = True
Sheets("Output").Rows("159:164").Hidden = True
Sheets("Output").Rows("165").Hidden = True
Sheets("Output").Rows("176").Hidden = True
Case "European Physical"
Sheets("Output").Rows("159:164").Hidden = True
Sheets("Output").Rows("170:175").Hidden = True
Sheets("Output").Rows("246:258").Hidden = True
Case Else
Sheets("Output").Rows("158:212").Hidden = False
Sheets("Output").Rows("246:253").Hidden = False
End Select
End With
End Sub

1 个答案:

答案 0 :(得分:0)

由于您在代码中使用了Target,我猜您使用了Worksheet_Change模块来运行代码。但是,您的代码看起来应该是这样的,以实现您所需的行为。 另外,请参阅上面的评论

我使用A1和A2作为下拉列表的位置。您需要相应地更改这些

Private Sub Worksheet_Change(ByVal Target As Range)
    If Not (Intersect(Range("A1"), Target) Is Nothing) Then
        Call showObservation(Target.value)  '@nightcrawler23
        ElseIf Not (Intersect(Range("A2"), Target) Is Nothing) Then
        Call showPayoff(Target.value)  '@nightcrawler23
    End If
End Sub

Sub showPayoff(myCase as String)  '@nightcrawler23
    With Sheets("Output")
        .Rows("112:137").Hidden = False
        .Rows("138:151").Hidden = False
        Select Case myCase  '@nightcrawler23
            Case "Bonus Capped Single Index"
                .Rows("112:154").Hidden = True
            Case "Bonus Capped Single Share"
                .Rows("112:154").Hidden = True
            Case "Bonus Uncapped Single Index"
                .Rows("112:154").Hidden = True
                .Rows("256:257").Hidden = True
            Case "Bonus Uncapped Single Share"
                .Rows("112:154").Hidden = True
                .Rows("256:257").Hidden = True
            Case "Bonus Uncapped Single Index"
                .Rows("112:154").Hidden = True
                .Rows("256:257").Hidden = True
            Case "Bonus Capped Worst of Indices"
                .Rows("112:154").Hidden = True
            '...
            '...
            '...
            '...
        End Select
    End With
End Sub

Sub showObservation(myCase as String) '@nightcrawler23
    With Sheets("Output") 'Observation and delivery
        .Rows("158:212").Hidden = False
        .Rows("246:253").Hidden = False

        Select Case myCase '@nightcrawler23
            Case "American Cash"
                .Rows("177:212").Hidden = True
                .Rows("165").Hidden = True
                .Rows("176").Hidden = True
            Case "American Physical"
                .Rows("162:164").Hidden = True
                .Rows("177:212").Hidden = True
                .Rows("173:175").Hidden = True
                .Rows("246:253").Hidden = True
            '...
            '...
            '...
            '...
        End Select
    End With
End Sub