从主模块和Populate imageBox具有userform调用函数

时间:2017-06-20 19:36:01

标签: excel vba userform

Public Function LoadImage() As Object

  For Each pics In sheetImages.Shapes
    If (CStr(pics.Name) = CStr(currpartImage)) Then
      Me.ImagePreview.Picture =PictureFromShape(sheetImages.Shapes(currImage))
      Me.ImagePreview.PictureSizeMode = 3
      currImageRow = sheetImages.Shapes(currpartImage).TopLeftCell.row + 1
    End If
  Next
End Function

我正在尝试让userform从我的主模块调用此函数并将其设置为形状...当此函数在userform代码中时它运行正常但我试图避免必须复制此所有我的用户形式的代码....当我把这个功能放在用户形式 - "我。" &安培; " as Object"被删除

1 个答案:

答案 0 :(得分:0)

在例程中添加参数:

Public Sub LoadImage(imageControl as Object) As Object
  For Each pics In sheetImages.Shapes
    If (CStr(pics.Name) = CStr(currpartImage)) Then
      imageControl .Picture =PictureFromShape(sheetImages.Shapes(currImage))
      imageControl .PictureSizeMode = 3
      currImageRow = sheetImages.Shapes(currpartImage).TopLeftCell.row + 1
    End If
  Next
End Sub

从您的表格致电:

LoadImage Me.ImagePreview

您还需要传递LoadImage中引用但不在用户表单范围之外的任何其他项目。