首页 > 解决方案 > 单击单元格,通过链接图片显示数据范围

问题描述

第一次发帖,希望这一切都有意义。我在 excel 中创建了一个显示 88 行1的图形。在另一个工作表中,有几行数据记录了每行的内容2当在图形上单击一行时,我希望弹出另一个工作表中的相应数据3

我已经通过在每行上制作一个透明按钮/形状来隐藏和显示链接图像4来管理它。但是,我不得不为每行创建一个单独的按钮和宏,每行有 100 多行,这似乎效率很低。这是我使用的代码:

Sub LINE1A1()

    With ActiveSheet.Shapes("Rectangle 9").TextFrame2.TextRange.Characters
        If .Text = "Hide" Then
            .Text = "Show"
            ActiveSheet.Shapes("Picture 3").Visible = False
        Else
            .Text = "Hide"
            With ActiveSheet.Shapes("Rectangle 9")
                ActiveSheet.Shapes("Picture 3").Left = .Left + .Width
                ActiveSheet.Shapes("Picture 3").Top = .Top + .Height
                ActiveSheet.Shapes("Picture 3").Visible = True
            End With
        End If
    End With
End Sub

实现这一目标的更好方法是什么?不是我使用链接图像作为数据和数据范围会发生变化,因为每行可能有超过 1 行数据。

标签: excelvba

解决方案


这是一个简单的例子,一个普通的 Sub 用来Application.Caller确定是哪个形状触发了它。

Sub Tester()

    Dim s As Shape, c, s2 As Shape

    c = Application.Caller        '<< name of your clicked shape
    Debug.Print c

    Set s = ActiveSheet.Shapes(c) '<< the clicked shape

    With s.TextFrame2.TextRange.Characters

        'here you need some way to transform the name of the clicked
        '  shape to get the name of the other shape to be shown/hidden/moved
        Set s2 = ActiveSheet.Shapes(c & "_linked")

        s2.Visible = (.Text = "Show")
        .Text = IIf(.Text = "Show", "Hide", "Show") 'toggle text

    End With

End Sub

推荐阅读