What I have done is created a macro that deletes any images in a certain range of cells if an image is present Range("D199:U205"
and then copies an image named "JB Sig" at the bottom of the sheet, and pastes it to the now blank location in the sheet.
I am getting "Run-time error '1004': Application-defined or object-defined error" when I use the macro. When this error occurs and I debug, the line that is causing problems is If Not Intersect(Range("D199:U205"), Range(Pic.TopLeftCell, Pic.BottomRightCell)) Is Nothing Then Pic.Delete
I have rewritten this in numerous ways given examples online but the error is still causing me problems.The weird thing is, sometimes I get the error, other times I don't, and even a minimal cosmetic change in the VBA code can fix it (temporarily). So at the moment the macro is working perfectly on my computer. However, my colleague who is accessing the same file (in a different office) is still getting the run-time error.
I am very inexperienced with code and any VBA I write is usually copied and pasted from somewhere, as opposed to me understanding what I am doing. So please be patient with my stupidity. Thanks
I've tried unprotecting the VBAProject by getting rid of a password I had to view it. This didn't work. I've tried changing the code for deleting the images to various different suggestions online but I am still running into the same error at times. There appears to be many different ways of getting the code to work but they are all susceptible to run-time errors.
Sub Approval_JB()
'
' Approval_JB Macro
'
'
Dim ws As Worksheet
Set ws = Sheets("Issue Letters")
ws.Unprotect Password:="john123"
Dim Pic As Object
For Each Pic In ws.Shapes
Dim R As Range
Debug.Print Pic.Name
Debug.Print Pic.TopLeftCell.Address
Debug.Print Pic.BottomRightCell.Address
Set R = Range(Pic.TopLeftCell, Pic.BottomRightCell)
If Not Intersect(Range("D199:U205"), R) Is Nothing Then Pic.Delete
Next Pic
Range("D199:U205").ClearContents
ws.Shapes.Range(Array("JB Sig")).Select
Selection.Copy
ws.Range("D201").Select
With ws.Range("D199")
.Value = "Yours Faithfully"
End With
With ws.Range("D204")
.Value = "Joe Bloggs"
End With
With ws.Range("D205")
.Value = "Engineer"
End With
Range("B192").Select
ws.Protect Password:="john123"
End Sub
It works sometimes, as described above. However other times when I run the macro, this error appears.
Run-time error '1004': Application-defined or object-defined error