Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

Runtime error when running this macro

Posted on 2011-04-18
8
Medium Priority
?
337 Views
Last Modified: 2012-06-27

I received this solution to search an excel worksheet for image names then to insert the corresponding image from a specific folder on my PC.

It was doing the job great but I am now getting an error that I cannot resolve. I am not sure what has changed to lead to this error so I am hoping someone here can spot it easily.

I get a "Run-time error '13'" Type mismatch" error. The offending line is:

   If oCell.Value <> "" Then



I am on excel 2010 and windows 7.



The complete code again is:

Const sSourceFolder = "E:\My Pictures"

Private Sub InsertPictures(oRange As Range)
   Dim oCell As Range
   
   For Each oCell In oRange.Cells
      If oCell.Value <> "" Then
         ' See if this contains a picture
         InsertPicture oCell
      End If
   Next
End Sub

Private Sub InsertPicture(oCell As Range)
   Dim sPicName As String
   Dim oFSO As Object
   Dim ws As Worksheet
   Dim p As Object
   
   sPicName = oCell.Value
   If sPicName = "" Then Exit Sub
   Set oFSO = CreateObject("Scripting.FileSystemObject")
   If Not oFSO.FileExists(sSourceFolder & "\" & sPicName) Then Exit Sub ' Pic not found
   
   ' Insert picture
   Set p = ActiveSheet.Pictures.Insert(sSourceFolder & "\" & sPicName)
   
   ' Ensure placed in correct location
   p.Top = oCell.Top
   p.Left = oCell.Left
End Sub

Private Sub test()
   Dim oEndCell As Range
   
   Set oEndCell = Range("A1").SpecialCells(xlCellTypeLastCell)
   InsertPictures ActiveSheet.Range("A1", oEndCell)
End Sub
0
Comment
Question by:stokenator
  • 4
  • 3
8 Comments
 
LVL 30

Expert Comment

by:SiddharthRout
ID: 35420775
There is nothing wrong with the code as far as I can see it. Try this. Insert this line

msgbox oRange.address

before

For Each oCell In oRange.Cells

in

Private Sub InsertPictures(oRange As Range)

What do you get?

Sid
0
 
LVL 30

Expert Comment

by:SiddharthRout
ID: 35420792
Or better still if you can upload your workbook then I can debug it for you?

Sid
0
 
LVL 43

Accepted Solution

by:
Saqib Husain, Syed earned 500 total points
ID: 35421011
stokenator,

It appears that at least one of your cells has an error condition like #DIV/0!, #VALUE, #NA etc. If so then use this modified code.

Saqib
Const sSourceFolder = "E:\My Pictures"

Private Sub InsertPictures(oRange As Range)
   Dim oCell As Range
   
   For Each oCell In oRange.Cells
    If Not IsError(oCell.Value) Then
      If oCell.Value <> "" Then
         ' See if this contains a picture
         InsertPicture oCell
      End If
    End If
   Next
End Sub

Private Sub InsertPicture(oCell As Range)
   Dim sPicName As String
   Dim oFSO As Object
   Dim ws As Worksheet
   Dim p As Object
   
   sPicName = oCell.Value
   If sPicName = "" Then Exit Sub
   Set oFSO = CreateObject("Scripting.FileSystemObject")
   If Not oFSO.FileExists(sSourceFolder & "\" & sPicName) Then Exit Sub ' Pic not found
   
   ' Insert picture
   Set p = ActiveSheet.Pictures.Insert(sSourceFolder & "\" & sPicName)
   
   ' Ensure placed in correct location
   p.Top = oCell.Top
   p.Left = oCell.Left
End Sub

Private Sub test()
   Dim oEndCell As Range
   
   Set oEndCell = Range("A1").SpecialCells(xlCellTypeLastCell)
   InsertPictures ActiveSheet.Range("A1", oEndCell)
End Sub

Open in new window

0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 30

Expert Comment

by:SiddharthRout
ID: 35421047
Nice Catch saqibh Bhai ;)

Sid
0
 
LVL 43

Expert Comment

by:Saqib Husain, Syed
ID: 35421058
Good morning Siddharth,

I have faced this problem quite a few times in my programs. That is why it clicked.

Saqib
0
 
LVL 30

Expert Comment

by:SiddharthRout
ID: 35421072
Good Morning. Mein to raat bhar soya hi nahin. :)

Ab thodi der mein sone jaoonga.

Sid
0
 
LVL 43

Expert Comment

by:Saqib Husain, Syed
ID: 35421082
Mujhe andaaza tha is baat ka.

0
 

Author Comment

by:stokenator
ID: 35424062
Good morning Saqib and Sid,

Thanks both for your assistance. Yes indeed it appears that the cause of my error is that I had a couple of broken Vlookup formulas in my spreadsheet. THese were giving "#NAME?" errors.

I have used your ammended code Saqib and it has worked great.

thankyou to both of you for your help.

Regards
0

Featured Post

[Webinar On Demand] Database Backup and Recovery

Does your company store data on premises, off site, in the cloud, or a combination of these? If you answered “yes”, you need a data backup recovery plan that fits each and every platform. Watch now as as Percona teaches us how to build agile data backup recovery plan.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
How to get Spreadsheet Compare 2016 working with the 64 bit version of Office 2016
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

580 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question