vba to show images in comment box causing Out of Memory

Armlegga

New Member
Joined
Sep 4, 2018
Messages
2
Hi,

I have some VBA to take a path from the 2nd column and show an image for the cell that has been clicked using the comment box to display the image
It looks great, hopefully others can use it if this error gets cleared up

Example sheet

ML053

<tbody>
</tbody>
http://media.4rgos.it/i/Argos/9016710_R_Z001A?$Web$&$DefaultPDP570$
ML089https://image.freepik.com/free-vector/smile-background-in-yellow-tones_23-2147637286.jpg
ML100https://www.free-funny-jokes.com/funny-pictures/what-do-you-call-a-sheep-covered-in-chocolate-joke.jpg

<tbody>
</tbody>


<tbody>
</tbody>


I have it working and looks great but after around 7 clicks I get RUN TIME ERROR 7 'Out of Memory'
I'm using a tiny sheet as a test only put around 12 lines into it so the size is 20KB

This is my code and the out of memory stops on .Comment.Shape.Fill.UserPicture TheFile
How can I keep the memory down, is it the code or an excel bug?

Just paste into the sheet you are using as it activates on the cell being selected
Code:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Dim listWS As Worksheet
Dim targetCol, targetRow As Long
Dim TheFile As String


If Target.Column = 2 Then
    If Target.Cells.Count = 1 Then
        If IsEmpty(Target.Cells) = False Then
            If Cells(Target.Row, 2).Comment Is Nothing Then
               
           ' Set listWS = Application.ThisWorkbook.ActiveSheet
            
            targetCol = Target.Column
            targetRow = Target.Row
           ' TheFile = listWS.Cells(targetRow, targetCol).Value
            TheFile = Cells(targetRow, targetCol).Value
            'With listWS.Range(listWS.Cells(targetRow, 4), listWS.Cells(targetRow, 4))
            With Range(Cells(targetRow, 2), Cells(targetRow, 2))
                .AddComment
                .Comment.Visible = True
                .Comment.Shape.Fill.UserPicture TheFile
                .Comment.Shape.ScaleWidth 0.8, msoFalse, msoScaleFromTopLeft
                .Comment.Shape.ScaleHeight 1.5, msoFalse, msoScaleFromTopLeft
               
            End With
           ' Set listWS = Nothing
            Else
            Cells(Target.Row, 2).Comment.Delete
            
            End If
        End If
    End If
End If
End Sub



Thanks for your help
 
Last edited by a moderator:

Excel Facts

How to show all formulas in Excel?
Press Ctrl+` to show all formulas. Press it again to toggle back to numbers. The grave accent is often under the tilde on US keyboards.

Forum statistics

Threads
1,214,606
Messages
6,120,487
Members
448,967
Latest member
visheshkotha

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