excel - 根据单元格值返回图像(100 张图像和可变单元格)

标签 excel vba

我正在尝试为我制作一个 TFT(团战战术)表,并希望使其看起来更好。为此,我想添加游戏中冠军的图像。当我输入名称时,图像应该出现在下面。我找到了一种将所有图像插入 Excel 工作表(~100)的方法,并且还成功制作了一个动态图像:

enter image description here

=插入IMG:

=INDEX(PIC!$B$1:$B$55;MATCH(Sheet1!B4;PIC!$A$1:$A$55;0))

我尝试设置 Sheet1!B4 部分变量,但它对单元格 D5 不起作用。现在对我来说唯一的解决方案是为每个“插槽”创建一个名称范围,但这将花费大量时间。有没有办法让excel只需输入名称就可以在下面插入图像?

最佳答案

可以使用 Worksheet_Change 事件实现您想要的效果。

出于演示目的,我将使用 3 个单元 B4C4D4

enter image description here

假设我们的图像表(我们称之为 PIC)如下所示。

enter image description here

如果您注意到的话,我在第二行插入了一个空白形状。如果用户在 B4C4D4 中按删除键,我们将使用此形状。如果没有找到匹配项,我们也将使用此图像。

现在让我们准备我们的主要工作表。请按照以下步骤操作

  1. PIC 工作表中选择单元格 B2(而不是形状),然后按 CRTL + C
  2. 右键单击主工作表中的单元格 B5,然后单击选择性粘贴-->链接图片,如下所示。 enter image description here
  3. 对单元格 C5D5 重复此操作。您的工作表现在看起来像这样。 enter image description here
  4. 我们现在已准备好基本设置。打开VBE并将以下代码粘贴到工作表代码区域中,我们就完成了!

代码:

Option Explicit

'More about Worksheet_Change at the below link
'https://stackoverflow.com/questions/13860894/why-ms-excel-crashes-and-closes-during-worksheet-change-sub-procedure/13861640#13861640

Private Sub Worksheet_Change(ByVal Target As Range)
    '~~> Check if multiple cells were changed
    If Target.Cells.CountLarge > 1 Then Exit Sub

    On Error GoTo Whoa

    Application.EnableEvents = False

    If Not Intersect(Target, Range("B4:D4")) Is Nothing Then
        Dim wsPic As Worksheet
        Dim pic As Shape, txtShp As Shape, shp As Shape
        Dim addr As String
        Dim aCell As Range

        '~~> Identify the shape below the changed cell
        For Each shp In ActiveSheet.Shapes
            If shp.TopLeftCell.Address = Target.Offset(1).Address Then
                Set txtShp = shp
                Exit For
            End If
        Next shp

        Set wsPic = ThisWorkbook.Sheets("PIC")

        '~~> Find the text in the PIC sheet
        Set aCell = wsPic.Columns(1).Find(What:=Target.Value2, LookIn:=xlValues, _
        LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
        MatchCase:=False, SearchFormat:=False)

        '~~> Identify the shape
        If Not aCell Is Nothing Then
            For Each shp In wsPic.Shapes
                If shp.TopLeftCell.Address = aCell.Offset(, 1).Address Then
                    Set pic = shp
                    addr = aCell.Offset(, 1).Address
                    Exit For
                End If
            Next shp
        End If

        '~~> Add the formula to show the image
        If Not pic Is Nothing And Not txtShp Is Nothing Then
            txtShp.Select '<~~ Required to insert the formula
            Selection.Formula = "=PIC!" & addr
        Else
            txtShp.Select
            Selection.Formula = "=PIC!$B$2"
        End If
        Target.Select '<~~ Remove focus from the shape
    End If

Letscontinue:
    Application.EnableEvents = True
    Exit Sub
Whoa:
    MsgBox Err.Description
    Resume Letscontinue
End Sub

实际行动

enter image description here

示例文件

您可以从Here下载示例文件

关于excel - 根据单元格值返回图像(100 张图像和可变单元格),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57370553/

相关文章:

excel - 如何获取当月的星期一(完整日期)?

excel - 如何重新编写我的 VBA 以将复制的行粘贴到具有匹配值的单独工作表中的行上?

excel - VBA - 从 XML 代码循环特定的子节点

C: int 到 _bstr_t

excel - 在除 Sheet1 和 Sheet2 之外的每个工作表上运行宏

java - Apache POI - 在不覆盖现有单元格格式的情况下写入 Excel 模板

vba - Excel根据当前日期自动填充日期

vba - 在 Access 中隐藏和重新显示自定义弹出总机?

excel - 选择范围内的形状。奇怪的看似随机的结果?

excel - 无法更改 Excel VBA IP 列表 ping 中的 ping 超时