I have a need to display an image, according to a text value in a cell.
This solution was found on Stack Overflow, but it is for Excel.
Sub Sample()
Select Case Range("G11").Value
Case "Picture 1": ShowPicture ("Picture 1")
Case "Picture 2": ShowPicture ("Picture 2")
Case "Picture 3": ShowPicture ("Picture 3")
Case "Picture 4": ShowPicture ("Picture 4")
End Select
End Sub
Sub ShowPicture(picname As String)
'~~> The reason why I am using OERN is because it is much simpler
'~~> than looping all shapes and then deleting them. There could be
'~~> charts, command buttons and other shapes. I will have to write
'~~> extra validation code so that those shapes are not deleted.
On Error Resume Next
Sheets("Sheet1").Shapes("Picture 1").Delete
Sheets("Sheet1").Shapes("Picture 2").Delete
Sheets("Sheet1").Shapes("Picture 3").Delete
Sheets("Sheet1").Shapes("Picture 4").Delete
On Error GoTo 0
Sheets("Temp").Shapes(picname).Copy
'<~~ Alternative to the below line. You may re-position the image
'<~~ after you paste as per your requirement
Sheets("Sheet1").Range("G15").Select
Sheets("Sheet1").Paste
End Sub
The code appears to be easily modified.
This is useful because I have over 50 named images to be selected.
Here is my test spreadsheet:
Display-Pic-By-Cell-Value.ods (335.0 KB)
This contains the macro:
Sub DisplayPic()
Select Case Range("Sheet1.A2").Value
Case "a": ShowPicture ("Image 1")
Case "b": ShowPicture ("Image 2")
Case "c": ShowPicture ("Image 3")
Case "d": ShowPicture ("Image 4")
End Select
End Sub
Sub ShowPicture(picname As String)
'~~> The reason why I am using OERN is because it is much simpler
'~~> than looping all shapes and then deleting them. There could be
'~~> charts, command buttons and other shapes. I will have to write
'~~> extra validation code so that those shapes are not deleted.
On Error Resume Next
Sheets("Sheet1").Shapes("Image 1").Delete
Sheets("Sheet1").Shapes("Image 2").Delete
Sheets("Sheet1").Shapes("Image 3").Delete
Sheets("Sheet1").Shapes("Image 4").Delete
On Error GoTo 0
Sheets("Temp").Shapes(picname).Copy
'<~~ Alternative to the below line. You may re-position the image
'<~~ after you paste as per your requirement
Sheets("Sheet1").Range("B2").Select
Sheets("Sheet1").Paste
End Sub
I noted that Calc names images âImage 1, Image 2 etcâ.
Consequently, I changed ShowPicture value âPicture 1â to âImage 1â etc.
and the Case values to a, b, c, d
When I run the macro
[Select Case Range(âSheet1.A2â).Value] is not defined.
Neither is [Sub ShowPicture(picname As String)]
[Select Case Range(âSheet1.A2â).Value] seems to be logical, but it fails.
[Sub ShowPicture(picname As String)] - I donât know how in excel, the macro knows that âpicnameâ refers to the image name.
Does anyone know how to modify this code to work in Libre Office Basic?