切换 – 根据单元值隐藏图片

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

我正在切换基于P52值的图片,完美的作品,但是我想根据单元格值P117来切换不同的图片,并且这部分代码实际上并不适合我。 我在代码中缺less什么?

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 

谢谢你的帮助。

想想你的if语句的逻辑,导致你退出子。

如果这个单元格是P117,你将会碰到第一个if语句,这会导致你立即退出这个子类。 所以你永远不会去你的第二次检查。

将每个操作的逻辑embeddedif语句(如我在此处展示的内容)中,并且如果单元格范围是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 

如果您要进行大量的检查,您可能还需要为Target.Address创build一个Select case语句。 根据你在这里提出的问题,很难说哪个更好。