切换 - 根据单元格值隐藏图片

时间:2014-04-16 13:39:59

标签: image excel vba

我不是VBA的专家,我所知道的只是基于浏览互联网,但是一些简单的代码对我很有帮助。

我正在根据P52值切换图片,效果很好,但后来我想根据单元格值P117切换不同的图片,那部分代码对我来说并不适用。我在代码中缺少什么?

Private Sub Worksheet_Change(ByVal Target As Range)
Application.ScreenUpdating = False

    If Target.Address <> "$P$52" Then Exit Sub
    With ActiveSheet
        Select Case Target.Value
            Case "Horizontal - feet"
                .Pictures("B3A").Visible = True
                .Pictures("V1A").Visible = False
                .Pictures("V1AF").Visible = False
            Case "Vertical - simple"
                .Pictures("B3A").Visible = False
                .Pictures("V1A").Visible = True
                .Pictures("V1AF").Visible = False
            Case "Vertical - lantern"
                .Pictures("B3A").Visible = False
                .Pictures("V1A").Visible = False
                .Pictures("V1AF").Visible = True
        End Select
    End With


    If Target.Address <> "$P$117" Then Exit Sub
    With ActiveSheet
        Select Case Target.Value
            Case "Right"
                .Pictures("3P1").Visible = True
                .Pictures("3P1M").Visible = False
            Case "Left"
                .Pictures("3P1").Visible = False
                .Pictures("3P1M").Visible = True

        End Select
    End With
End Sub

感谢您的帮助。

1 个答案:

答案 0 :(得分:0)

仔细考虑if语句的逻辑,导致你退出sub。

如果单元格是P117,您将点击第一个if语句,该语句会立即退出sub。所以你永远不会进行第二次检查。

将我的每个操作的逻辑嵌入到我在此处显示的if语句中,如果单元格范围是P52或P117,您将能够“做一些事情”。

Private Sub Worksheet_Change(ByVal Target As Range)
Application.ScreenUpdating = False
    'only do the following operation if your cell address is P52 - don't exit out
    'of your entire code if it's not
    If Target.Address = "$P$52" Then
        With ActiveSheet
            Select Case Target.Value
                Case "Horizontal - feet"
                    .Pictures("B3A").Visible = True
                    .Pictures("V1A").Visible = False
                    .Pictures("V1AF").Visible = False
                Case "Vertical - simple"
                    .Pictures("B3A").Visible = False
                    .Pictures("V1A").Visible = True
                    .Pictures("V1AF").Visible = False
                Case "Vertical - lantern"
                    .Pictures("B3A").Visible = False
                    .Pictures("V1A").Visible = False
                    .Pictures("V1AF").Visible = True
            End Select
        End With
    End If

    'you skip to down here if it is NOT P52, which then lets you check again to 
    'see if it's P117
    If Target.Address = "$P$117" Then
        With ActiveSheet
            Select Case Target.Value
                Case "Right"
                    .Pictures("3P1").Visible = True
                    .Pictures("3P1M").Visible = False
                Case "Left"
                    .Pictures("3P1").Visible = False
                    .Pictures("3P1M").Visible = True

            End Select
        End With
    End If
End Sub

如果要进行大量此类检查,您可能还需要为Select case创建Target.Address语句。鉴于你在这里提出的要求,很难说哪个更适合你。