Allow user to change instered picture on protected sheet

Sven62

Active Member
Joined
Feb 21, 2012
Messages
485
Is there a way to allow users to place a new picture in place of an existing picture in a protected workbook/worksheet? I don't want them to be able to edit any other part of the sheet.
 

Excel Facts

Will the fill handle fill 1, 2, 3?
Yes! Type 1 in a cell. Hold down Ctrl while you drag the fill handle.
Yeah except there are many other objects on the sheet. Command buttons. Form buttons. Etc...

But Thanks for trying!
 
Upvote 0
If you have command buttons on the sheet, then I would assume you have macros attached to them...

So if you are familiar with VBA, you could add a command button with a macro that swaps the picture. In which case, I would use VBA to protect the sheet and set the UserInterfaceOnly option of the Sheet.Protect method to True. This would allow your macro to swap the picture to function uninhibited on an otherwise completely protected worksheet.
 
Upvote 0
Thank you!

I know enough VBA to be dangerous. UserInterfaceOnly is set to true. But I don't know the code to create a button that lets me swap an existing photo on the worksheet for another photo on the computer. Can you help me with that?
 
Upvote 0
I'm gonna assume it's just OK to swap the picture and use the TopLeftCell property of the existing picture to place the new picture.

This code is actually pretty cool! I think I'm gonna keep it. :cool:

Code:
Sub SwapPic()
   

    Dim PicFileName As String
    With Application.FileDialog(msoFileDialogFilePicker)
        .Show
        PicFileName = .SelectedItems(1)
    End With

    With ActiveSheet.Shapes(Application.Caller)
        .TopLeftCell.Select
        .Delete
    End With
       

    With ActiveSheet.Pictures.Insert(PicFileName)
        .Name = "UserPic"
        .OnAction = "SwapPic"
    End With
    

End Sub

1) Copy/paste the above code, exactly as-is, into a module code pane.
2) Right-click your picture that you want to make "swappable" and select "Assign Macro" (choose SwapPic of course!)
3) Click the picture to run the macro

Cool part is, it swaps out the picture and automagically reassigns the SwapPic macro to the new picture!
Now all the users have to do is click the pic to swap it out. Pretty nifty!

Notice that it does set the swapped picture name to the constant "UserPic" shape object name. If you want to assign this macro to multiple pics on the same sheet you would have to comment-out or delete that line of code so the default "Picture1", "Picture2", "Picture..." filename would be allowed to increment. I just set it as a constant in the event that you may want to refer to the picture in your code.
 
Last edited:
Upvote 0
One adjustment, need to add a line of code to exit the subroutine if the user clicked the image by mistake and exits the file picker window without making a selection...

Code:
 Sub SwapPic()

  
    Dim PicFileName As String
    With Application.FileDialog(msoFileDialogFilePicker)
        .Show
        On Error Resume Next
            PicFileName = .SelectedItems(1)
        On Error GoTo 0
    End With
    If PicFileName = "" Then Exit Sub

    With ActiveSheet.Shapes(Application.Caller)
        .TopLeftCell.Select
        .Delete
    End With
       
    With ActiveSheet.Pictures.Insert(PicFileName)
        .Name = "UserPic"
        .OnAction = "SwapPic"
    End With
    
End Sub

...and this assumes of course that you've already protected the worksheet and set the UserInterfaceOnly property to True.
 
Last edited:
Upvote 0
That is awesome! Is there any way to password protect that specific macro and not the others on the page?
 
Upvote 0

Forum statistics

Threads
1,223,249
Messages
6,171,031
Members
452,374
Latest member
keccles

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