Count of image/ Object in selected range

Negi1984

Board Regular
Joined
May 6, 2011
Messages
199
Hi All,

I have a problem statement where I need to count the no. of object/image in specific range/cell/merged cells.
For example
I have merged cell B19(covering B19 to E25). and these cells contains 2 objects(pdf/excel inserted as object).
Now I want to count the no. of objects , which are exist in this specific range.

I already have a code which I am using , but its checking only 1 object and I need to modify it to show output as count of object.

Looking forward for the valuable support.

Code:
Dim Pict As Object
   
   Application.Volatile
   Caddress = Cell.Address
   
   
   For Each Pict In Application.Caller.Parent.Pictures
      If Pict.TopLeftCell.Address = Caddress Or Pict.BottomRightCell.Address = Caddress Then
         HASpic = True
         Exit Function
      
      End If
   Next Pict
   HASpic = False
   
   
End Function

Regards,
Rajender
 
Last edited:

Excel Facts

Is there a shortcut key for strikethrough?
Ctrl+S is used for Save. Ctrl+5 is used for Strikethrough. Why Ctrl+5? When you use hashmarks to count |||| is 4, strike through to mean 5.
Try this:
Code:
Function Pics(rng As Range)
    Dim Pict As Object, pRng As Range, mRng As Range, c As Long, p As Long
    On Error Resume Next
    Application.Volatile
    Set mRng = rng.MergeArea
    With rng.Parent
        For p = 1 To .Pictures.Count
            Set Pict = .Pictures(p)
            Set pRng = .Range(Pict.TopLeftCell, Pict.BottomRightCell)
            If Not Intersect(pRng, mRng) Is Nothing Then c = c + 1
        Next p
    End With
    Pics = c
End Function


with formula
=pics(B19)
 
Upvote 0
Hi Yongle,

One more thing I want to ask, I saved this code in Personal macro list. but when I am trying to use Pic function in other workbooks ,this function is not visible.
Could you please suggest what I am missing here.

Regards,
Rajender
 
Upvote 0
https://answers.microsoft.com/en-us/msoffice/forum/msoffice_excel-msoffice_custom-mso_2010/custom-functions-in-personal-macro-and-not-showing/5b8a0d76-956b-4cca-b4a1-d9b402228e54

This is the way the Personal macro workbook (PMW) operates. The macros are available in the macros dialog box, regardless of the active workbook, but the functions are not. Try one of the following:

1. Qualify the reference in your worksheet formulas, e.g.,
=Personal.xlsb!pics(B19)

2. Create a reference to your personal macro workbook in the workbook you're using (you will have to save your workbook as xlsm type instead of xlsx). Open the visual basic editor (VBE). Create a module in your workbook, and put something in it, even if it's only a comment. On the VBE menu, select Tools/References. Check the appropriate box in the dialog that appears.

3. Save your PMW as an add-in, activate it in Excel using File/Options/Add-ins/Manage add-ins.




 
Last edited:
Upvote 0
It looks like the function provided by Yongle can return incorrect results under certain circumstances. For example, when the range contains a mix of both merged and unmerged cells. Or, when a multi-cell range contains no merged cells. Here's my attempt...

Code:
Function getNumPics(rTarget As Range) As Long    
    Dim Pic As Object
    Dim numPics As Long
       
    Application.Volatile
    
    If rTarget.MergeCells Then
        Set rTarget = rTarget.MergeArea
    End If
    
    numPics = 0
    For Each Pic In rTarget.Parent.Pictures
       If Not Application.Intersect(Range(Pic.TopLeftCell, Pic.BottomRightCell), rTarget) Is Nothing Then
          numPics = numPics + 1
       End If
    Next Pic
    
    getNumPics = numPics
   
End Function

Hope this helps!
 
Upvote 0
Hi Yongle,

Thanks a lot once again for your valuable support.
its working fine now.

Regards,
Rajender
 
Upvote 0
Hi Domenic,

Thanks a lot for your valuable support as well. This is working perfectly.
Have a nice day to you.

Regards,
Rajender
 
Upvote 0

Forum statistics

Threads
1,223,911
Messages
6,175,327
Members
452,635
Latest member
laura12345

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