dynamically display pictures

gleamng

Board Regular
Joined
Oct 8, 2016
Messages
98
Office Version
  1. 365
  2. 2021
  3. 2019
  4. 2016
  5. 2013
  6. 2011
  7. 2010
  8. 2007
  9. 2003 or older
Platform
  1. Windows
  2. MacOS
  3. Mobile
  4. Web
Greetings to you all.

I have a workbook with list of clients and their details in sheet (index), while every client has a sheet each, on every sheet i want picture of every client to appear on his/her.

i got the below code online, but it only works if the sheets were un-protected, and i need to protect the sheets. pls kindly help me out, here comes the vba code

Code:
Option Explicit

Function InsertPictureInCell(rg As Range, FilePathName As String) As String
Dim ws As Worksheet
Dim sh As Shape, objPicture As Object
Dim PictureExists As Integer
Dim PictureLeftPosition As Single, PictureTopPosition As Single
Dim PictureWidth As Single, PictureHeight As Single, Aspect As Single
    
    Application.ScreenUpdating = False
    Set ws = ThisWorkbook.Worksheets(rg.Worksheet.Name)
    PictureExists = 0
    If ws.Shapes.Count > 0 Then
        For Each sh In ws.Shapes
            If sh.TopLeftCell.Column = rg.Column And sh.TopLeftCell.Row = rg.Row Then
                PictureExists = 1
            End If
        Next sh
    End If
    If PictureExists = 0 Then
        If Dir(FilePathName) <> "" Then
            If LCase(Right(FilePathName, 3)) = "jpg" Or LCase(Right(FilePathName, 3)) = "tif" Then
                Set objPicture = ws.Pictures.Insert(FilePathName)
                Aspect = objPicture.Width / objPicture.Height
                objPicture.Delete
            Else
                Set objPicture = LoadPicture(FilePathName)
                Aspect = objPicture.Width / objPicture.Height
            End If
                
            PictureTopPosition = rg.Top + 1
            PictureLeftPosition = rg.Left + 1
            PictureHeight = rg.Height + 90
            PictureWidth = PictureHeight * Aspect
            ws.Shapes.AddPicture FilePathName, msoFalse, msoTrue, PictureLeftPosition, PictureTopPosition, PictureWidth, PictureHeight
            InsertPictureInCell = ""
        Else
            InsertPictureInCell = "No file"
        End If
    Else
        InsertPictureInCell = ""
    End If
    Application.ScreenUpdating = True
End Function
 

Excel Facts

Round to nearest half hour?
Use =MROUND(A2,"0:30") to round to nearest half hour. Use =CEILING(A2,"0:30") to round to next half hour.
Simply add a line of code to unprotect your sheet before the the changes to your worksheet are made, and then another line of code to protect the your sheet afterwards, for example...

Code:
ws.Unprotect Password:="Your PassWord"
[COLOR=#008000]'Your code to make changes here
'
'[/COLOR]
ws.Protect Password:="Your PassWord"

Note that a password is optional.
 
Last edited:
Upvote 0

Forum statistics

Threads
1,223,903
Messages
6,175,289
Members
452,631
Latest member
a_potato

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