Delete picture object during import if exist (1 Viewer)

tmyers

Well-known member
Local time
Yesterday, 20:15
Joined
Sep 8, 2020
Messages
1,090
I am using the following code to format an excel file:
Code:
    Set xl = New Excel.Application
    Set wb = xl.Workbooks.Open(strfile)
    Set ws = wb.Sheets("Sheet0")
'delete header information
    Set r = ws.Range("A:K").Find("QTY", lookin:=xlValues)
   
        ws.Shapes.Range(Array("Picture 1")).Delete

        If r.Row <> 1 Then
            ws.Range("A1", r.Offset(-1)).EntireRow.Delete xlShiftUp
        End If
       
    'delete footer up to and including total
    Set r = ws.Range("B:K").Find("Total", lookin:=xlValues)
   
        With ws
            .Rows(r.Row & ":" & .Rows.Count).Delete
        End With

However, I am trying to tackle the problem if a user tries to import a file again after it has already been formatted. The main issue is the picture object would no longer exist. How can I structure the code to deal with that?

If picture 1 exist, delete. If not then move on.
 

tmyers

Well-known member
Local time
Yesterday, 20:15
Joined
Sep 8, 2020
Messages
1,090
Much to my surprise, a snippet of code I remember from excel works.

I did
Diff:
dim pic as string
    pic = ws.pictures.count
    
if pic = 1 then
    ws.shapes.range(array("Picture 1")).delete
end if
 

Gasman

Enthusiastic Amateur
Local time
Today, 01:15
Joined
Sep 21, 2011
Messages
14,260
Why are you defining a variable as a string if you expect it to hold a numeric count? :unsure:
 

tmyers

Well-known member
Local time
Yesterday, 20:15
Joined
Sep 8, 2020
Messages
1,090
Why are you defining a variable as a string if you expect it to hold a numeric count? :unsure:
Correct it.
It is a habit since I have been dealing with strings a lot :p
 

Users who are viewing this thread

Top Bottom