Skip Blanks Inserting Pictures

dimples_21188

New Member
Joined
Aug 21, 2017
Messages
12
Hello everyone
I would truly appreciate some help. The formula below works beautifully until two different reasons:

1. The cell is blank or,
2. There isn't an picture in the file that matches the name in the cell

Could someone help me find a way to ignore these errors and allow the code to continue.

THANK YOU

Sub Worksheet_change(ByVal Target As Range)

Dim animal_pic As Picture
Dim pic_location As String
Dim animal_name As String
ActiveSheet.Pictures.Delete
For i = 7 To 50
animal_name = Worksheets("Strains").Cells(i, 3).Value
pic_location = "C:\Users\dimpl\Desktop\Strain Tiles\" & Worksheets("Strains").Cells(i, 3).Value & ".png"

With Worksheets("Strains").Cells(i, 1)
Set animal_pic = ActiveSheet.Pictures.Insert(pic_location)

animal_pic.Top = .Top
animal_pic.Left = .Left
animal_pic.ShapeRange.LockAspectRatio = msoFalse
animal_pic.Placement = xlMoveAndSize
animal_pic.ShapeRange.Width = 75
animal_pic.ShapeRange.Height = 75


End With
Next i


End Sub
 

Excel Facts

What is the shortcut key for Format Selection?
Ctrl+1 (the number one) will open the Format dialog for whatever is selected.
Hi!

See this link I like for proper error handling in VBA. However, I think what you have is simple enough we can just handle it with some if statements.

To check if a cell is blank, check if its value equals vbnullstring (which is the same as ""):
VBA Code:
    If ThisWorkbook.Worksheets("Strains").Cells(i, 3).Value = vbNullString Then
        '...
    End If
To check if file exists, make use of the Dir function with the file path and evaluate its result. If the result is vbnullstring then the file does not exist:
VBA Code:
    If Dir("C:\Users\dimpl\Desktop\Strain Tiles\" & Worksheets("Strains").Cells(i, 3).Value & ".png") = vbNullString Then
        '...
    End If


So, I might combine those two together and only do the picture operations if both results are not blank, i.e.,
VBA Code:
If cellValue <> vbNullString And Dir(imagePath) <> vbNullString Then
        'Image insert and manipulations here
    End If
This of course assumes that if there is an error you just want to do nothing and skip to the next loop iteration as you mentioned.
 
Upvote 0
Hi!

See this link I like for proper error handling in VBA. However, I think what you have is simple enough we can just handle it with some if statements.

To check if a cell is blank, check if its value equals vbnullstring (which is the same as ""):
VBA Code:
    If ThisWorkbook.Worksheets("Strains").Cells(i, 3).Value = vbNullString Then
        '...
    End If
To check if file exists, make use of the Dir function with the file path and evaluate its result. If the result is vbnullstring then the file does not exist:
VBA Code:
    If Dir("C:\Users\dimpl\Desktop\Strain Tiles\" & Worksheets("Strains").Cells(i, 3).Value & ".png") = vbNullString Then
        '...
    End If


So, I might combine those two together and only do the picture operations if both results are not blank, i.e.,
VBA Code:
If cellValue <> vbNullString And Dir(imagePath) <> vbNullString Then
        'Image insert and manipulations here
    End If
This of course assumes that if there is an error you just want to do nothing and skip to the next loop iteration as you mentioned.
thank you ill give that a go. really thanks, this has been puzzling me for sometime
 
Upvote 0

Forum statistics

Threads
1,214,516
Messages
6,119,978
Members
448,934
Latest member
audette89

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top