点击后,excel vba userform图片更改

时间:2014-11-18 12:37:27

标签: excel image vba excel-vba userform

我创建了一个10 * 4个图片空间的用户表单(" set11"," set12",...)。一开始他们是空的。当我点击其中一张图片时,应该通过随机图片重置(函数getRandomPath)。

子"点击"如果我使用按钮(开始)单击,则有效。如果我点击图片没有任何反应。当我再次单击按钮(开始)时,之前点击的图片不再发生变化。

以下是相关代码:

Private Sub set11_Click() 
    Call clicked("1", "1") '*doesn't work*
End Sub 

Private Sub set12_Click() 
    Call clicked("1", "2") '*doesn't work*
End Sub 

Private Sub set13_Click() 
    Call clicked("1", "3") '*doesn't work*
End Sub 

Private Sub set14_Click() 
    Call clicked("1", "4") '*doesn't work*
End Sub 

Private Sub clicked(row As String, column As String) 
    Controls("set" & row & column).Picture = LoadPicture(getRandomPfad()) 
End Sub

Private Sub start_Click() 
    Call clearpictures 
    set11.Picture = LoadPicture(getRandomPfad()) '*works*
    Controls("set12").Picture = LoadPicture(getRandomPfad()) '*works*
    Call clicked("1", "3") '*works*
End Sub

Private Function getRandomPfad()
    Dim random As Integer
    random = Int(6 * Rnd + 1)
    Select Case random
        Case Is = 1
            getRandomPfad = "U:\MMpic\Green.jpg"
        Case Is = 2
            getRandomPfad = "U:\MMpic\Blue.jpg"
        Case Is = 3
            getRandomPfad = "U:\MMpic\Yellow.jpg"
        Case Is = 4
            getRandomPfad = "U:\MMpic\Pink.jpg"
        Case Is = 5
            getRandomPfad = "U:\MMpic\Orange.jpg"
        Case Is = 6
            getRandomPfad = "U:\MMpic\Red.jpg"
    End Select
End Function

我希望你能帮助我。

2 个答案:

答案 0 :(得分:0)

您的代码必须存在于userform中。如果代码在模块中,它将无法工作 enter image description here

另外,请确保您已将图像控件命名为与代码中指定的名称相匹配:“set11”

enter image description here

答案 1 :(得分:0)

我找到了解决方案,我应该重新粉刷:

Private Sub clicked(row As String, column As String) 
    Controls("set" & row & column).Picture = LoadPicture(getRandomPfad())
    Repaint
End Sub  

但是谢谢你的帮助!