我在命令按钮上绑定了一个宏,该按钮允许用户插入图像。
Sub Insert_Image()
ActiveSheet.Unprotect
With Application.FileDialog(msoFileDialogFilePicker)
.AllowMultiSelect = False
.ButtonName = "Submit"
.Title = "Select an image file"
.Filters.Clear
.Filters.Add "JPG", "*.JPG"
.Filters.Add "JPEG File Interchange Format", "*.JPEG"
.Filters.Add "Graphics Interchange Format", "*.GIF"
.Filters.Add "Portable Network Graphics", "*.PNG"
.Filters.Add "Tag Image File Format", "*.TIFF"
.Filters.Add "All Pictures", "*.*"
If .Show = -1 Then
Dim img As Object
Set img = ActiveSheet.Pictures.Insert(.SelectedItems(1))
'Position Image
img.Left = 598
img.Top = 70
mbResult = MsgBox("Did you select the correct image?", vbYesNo)
Select Case mbResult
Case vbNo:
img.Delete
End Select
End If
End With
ThisWorkbook.Activate
ActiveSheet.Protect
End Sub
---------------------------------------------
Sub Delete_Image()
img.Delete
End Sub
我正在尝试创建一个单独的命令按钮,如果用户插入了错误的按钮,该按钮将允许用户删除该图像。
当我将Dim img As Object
更改为Public img As Object
时,我在Sub或Function Error中得到了无效的属性,如果我将其保留为Dim
img.delete
为空。
我不确定如何将变量跨多个子对象。
先谢谢了。
答案 0 :(得分:2)
将其放在模块顶部的Sub外部:
Option Explicit
Public img As Object
Public Sub CreateImg()
Set img = ActiveSheet.Pictures.Insert(.SelectedItems(1))
End Sub
Public Sub DeleteImg()
If Not (img Is Nothing) Then
img.Delete
Set img = Nothing
End If
End Sub