点击单元格,通过链接图片显示数据范围

问题描述 投票:0回答:2

第一次发帖,希望这一切都有意义。我在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行数据。

excel vba
2个回答
0
投票

您可以使用宏创建这些按钮或形状(如您所说,例如100个按钮),并使用宏移动它们,使它们位于正确的位置。他们将使用相同的宏。根据按钮的位置或根据其名称可能更好,它会做一个不同的事情。

有一个很好的教程。 https://www.youtube.com/watch?v=bF28JhlC7yc&index=18&list=PLpOAvcoMay5SE3XTp2YN2v6NcJuXKM9KX


0
投票

这是一个简单的例子,其中一个公共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
© www.soinside.com 2019 - 2024. All rights reserved.