excel 检查userform中的所有图像

5kgi1eie  于 2023-04-22  发布在  其他
关注(0)|答案(1)|浏览(132)

我想检查我的用户表单中的所有图像,并检查工作表中某些单元格中的值。然后根据值,将图像对象的图片更改为19(i1到i19)中的一个。
为了不检查每个图像,我想循环该过程。所有图像对象的名称从slot_1到slot_42。
我在下面的(插槽(a))是否可能?
编辑:我改变了你说的,但错误仍然发生。

Private Sub UserForm_Initialize()
name = Sheets("Player Data").Cells(2, 1).Value
inventory.Caption = name & "' s inventory"
name_label_inv.Caption = name
item_info_label.Visible = False
item_info_label.Caption = " "
Dim a
For a = 1 To 42 Step 1
If Sheets("Player Data").Cells(a + 1, 11).Value = 0 Then
   Me.Controls("slot_" & a).Picture = LoadPicture("C:\The Game\items\empty_icon.jpg")
Else
        If Sheets("Player Data").Cells(a + 1, l2).Value = 1 Then
        Me.Controls("slot_" & a).Picture = LoadPicture("C:\The Game\items\item_exp_book.jpg")
        Else
        If Sheets("Player Data").Cells(a + 1, l2).Value = 2 Then
        Me.Controls("slot_" & a).Picture = LoadPicture("C:\The Game\items\item_silver_coin.jpg")
        Else
        If Sheets("Player Data").Cells(a + 1, l2).Value = 3 Then
        Me.Controls("slot_" & a).Picture = LoadPicture("C:\The Game\items\item_gold_ingot.jpg")
        Else
        If Sheets("Player Data").Cells(a + 1, l2).Value = 4 Then
        Me.Controls("slot_" & a).Picture = LoadPicture("C:\The Game\items\item_heal_hp_1.jpg")
        Else
        If Sheets("Player Data").Cells(a + 1, l2).Value = 5 Then
        Me.Controls("slot_" & a).Picture = LoadPicture("C:\The Game\items\item_heal_mp_1.jpg")
        Else
        If Sheets("Player Data").Cells(a + 1, l2).Value = 6 Then
        Me.Controls("slot_" & a).Picture = LoadPicture("C:\The Game\items\item_heal_hp_2.jpg")
        Else
        If Sheets("Player Data").Cells(a + 1, l2).Value = 7 Then
        Me.Controls("slot_" & a).Picture = LoadPicture("C:\The Game\items\item_weapon_1.jpg")
        Else
        If Sheets("Player Data").Cells(a + 1, l2).Value = 8 Then
        Me.Controls("slot_" & a).Picture = LoadPicture("C:\The Game\items\item_weapon_2.jpg")
        Else
        If Sheets("Player Data").Cells(a + 1, l2).Value = 9 Then
        Me.Controls("slot_" & a).Picture = LoadPicture("C:\The Game\items\item_weapon_3.jpg")
        Else
        If Sheets("Player Data").Cells(a + 1, l2).Value = 10 Then
        Me.Controls("slot_" & a).Picture = LoadPicture("C:\The Game\items\item_arrow.jpg")
        Else
        If Sheets("Player Data").Cells(a + 1, l2).Value = 11 Then
        Me.Controls("slot_" & a).Picture = LoadPicture("C:\The Game\items\item_weapon_4.jpg")
        Else
        If Sheets("Player Data").Cells(a + 1, l2).Value = 12 Then
        Me.Controls("slot_" & a).Picture = LoadPicture("C:\The Game\items\item_weapon_5.jpg")
        Else
        If Sheets("Player Data").Cells(a + 1, l2).Value = 13 Then
        Me.Controls("slot_" & a).Picture = LoadPicture("C:\The Game\items\item_shield_1.jpg")
        Else
        If Sheets("Player Data").Cells(a + 1, l2).Value = 14 Then
        Me.Controls("slot_" & a).Picture = LoadPicture("C:\The Game\items\item_armor_helmet_1.jpg")
        Else
        If Sheets("Player Data").Cells(a + 1, l2).Value = 15 Then
        Me.Controls("slot_" & a).Picture = LoadPicture("C:\The Game\items\item_armor_chest_1.jpg")
        Else
        If Sheets("Player Data").Cells(a + 1, l2).Value = 16 Then
        Me.Controls("slot_" & a).Picture = LoadPicture("C:\The Game\items\item_armor_ring_1.jpg")
        Else
        If Sheets("Player Data").Cells(a + 1, l2).Value = 17 Then
        Me.Controls("slot_" & a).Picture = LoadPicture("C:\The Game\items\item_wild_1.jpg")
        Else
        If Sheets("Player Data").Cells(a + 1, l2).Value = 18 Then
        Me.Controls("slot_" & a).Picture = LoadPicture("C:\The Game\items\item_wild_2.jpg")
        Else
        If Sheets("Player Data").Cells(a + 1, l2).Value = 19 Then
        Me.Controls("slot_" & a).Picture = LoadPicture("C:\The Game\items\item_key.jpg")
        End If
        End If
        End If
        End If
        End If
        End If
        End If
        End If
        End If
        End If
        End If
        End If
        End If
        End If
        End If
        End If
        End If
        End If
        End If
        End If
Next
End Sub
yeotifhr

yeotifhr1#

在这种情况下,可以使用Me.Controls来访问控件。

slot_(a).Picture = LoadPicture(i5)

可以写成

Me.Controls("Slot_" & a).Picture = LoadPicture(i5)

这里有一个例子

Option Explicit

Private Sub CommandButton1_Click()
    Dim a As Long
    a = 1
    Me.Controls("Slot_" & a).Picture = LoadPicture("C:\Sample.bmp")
End Sub

相关问题