使用Excel中的“名称管理器”动态更改图像

时间:2018-03-01 09:34:07

标签: excel excel-formula

我有一个动态行数的表。有一个特定的列可以根据旁边单元格的值更改图像。

我尝试过使用本指南:

https://www.youtube.com/watch?v=yHBQ9Qxli3M

但它只能用于特定行而不能动态使用。 我尝试过使用:

=INDIRECT("RC[-1]",0)

以我的图片名称,但似乎没有根据目标细胞进行更改。

指向我的Excel文件的链接:

https://drive.google.com/open?id=1v1RXhdPYU4j2CqxgNEWUtJ8gHbZV33xN

1 个答案:

答案 0 :(得分:0)

还有另一种方法可以通过VBA执行此操作。您可以将此代码放在' Sheet1'下。它会检测何时对列M进行更改,然后从查找表中复制并粘贴图像。

注意:

  • 仅当值1插入单元格时才有效
  • 删除' 1'值删除图像
  • 向下拖动1个值将无效(尽管可以调整宏)。最好进入' 1'一次一个值;)
    Private Sub Worksheet_Change(ByVal Target As Range)
    Dim KeyCells, SelectedCells As Range, s As Shape
    Set KeyCells = Range("M13:M1000")
    Set SelectedCells = Range(Target.Address)

    If Not Application.Intersect(KeyCells, SelectedCells) Is Nothing Then
    'Place a 1 in the cell to insert an image
    If Application.Sum(Range(Target.Address)) = 1 Then        
      Sheets("Shapes").Shapes("Shape" & Range(Target.Address).Offset(0, -2).Value).Copy
      ActiveSheet.Paste
      Selection.Left = SelectedCells.Left
      Selection.Top = SelectedCells.Top
      SelectedCells.Select    
    Else 'Deletes the pictures if a 1 is not present
      For Each s In ActiveSheet.Shapes
        If Not Intersect(SelectedCells, s.TopLeftCell) Is Nothing Then
            s.Delete
        End If
      Next s 
    End If
    End If
    End Sub