我们可以单击特定的单元格并执行以显示/隐藏其他单元格中的图像吗?

时间:2019-03-29 19:57:42

标签: excel image click hide show

示例A1被列为John Doe1,B1为空白,单击A1时,B2应显示John Doe1的图片,如果单击任何空白单元格,该图像将消失,如果单击A2,Jane Doe1将显示在B1,而不是B2,图像只会始终显示在B1中。 我仍在学习编码,请多加注意我的婴儿编码技能(如果您甚至说出这么多的技能)。

If Selection.Count = 1 Then
    If Range("A:A").Value = "John Doe1" Then
        ActiveSheet.Pictures("John Doe1").Visible = True
        ActiveSheet.Pictures("John Doe2").Visible = False
        ActiveSheet.Pictures("John Doe2").Visible = False
        ActiveSheet.Pictures("Jane Doe1").Visible = False
        ActiveSheet.Pictures("Jane Doe2").Visible = False
        ActiveSheet.Pictures("Jane Doe3").Visible = False
    Else if Range("A:A").Value = "John Doe2" Then
        ActiveSheet.Pictures("John Doe1").Visible = False
        ActiveSheet.Pictures("John Doe2").Visible = True
        ActiveSheet.Pictures("John Doe2").Visible = False
        ActiveSheet.Pictures("Jane Doe1").Visible = False
        ActiveSheet.Pictures("Jane Doe2").Visible = False
        ActiveSheet.Pictures("Jane Doe3").Visible = False
    End If
End If

1 个答案:

答案 0 :(得分:2)

不错的第一次尝试!这里需要的是一个Worksheet_SelectionChange()事件-该事件放置在Sheet模块中(而不是Module1中)。

这是如何工作的一个最小示例:

Private Sub Worksheet_SelectionChange(ByVal Target As Range)

    If Target.Value = "John Doe1" Then
        ActiveSheet.Pictures("John Doe1").Visible = True
        ActiveSheet.Pictures("John Doe2").Visible = False
        ActiveSheet.Pictures("John Doe3").Visible = False
    ElseIf Target.Value = "John Doe2" Then
        ActiveSheet.Pictures("John Doe1").Visible = False
        ActiveSheet.Pictures("John Doe2").Visible = True
        ActiveSheet.Pictures("John Doe3").Visible = False
    ElseIf Target.Value = "John Doe3" Then
        ActiveSheet.Pictures("John Doe1").Visible = False
        ActiveSheet.Pictures("John Doe2").Visible = False
        ActiveSheet.Pictures("John Doe3").Visible = True
    Else
        ActiveSheet.Pictures("John Doe1").Visible = False
        ActiveSheet.Pictures("John Doe2").Visible = False
        ActiveSheet.Pictures("John Doe3").Visible = False
    End If

End Sub

@ScottCraner的替代解决方案:

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    ActiveSheet.Pictures("John Doe1").Visible = Target.Value = "John Doe1"
    ActiveSheet.Pictures("John Doe2").Visible = Target.Value = "John Doe2"
    ActiveSheet.Pictures("John Doe3").Visible = Target.Value = "John Doe3"
End Sub