vba to show images in comment box causing Out of Memory


New Member
Sep 4, 2018

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





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
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))
                .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
            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:

Some videos you may like

Excel Facts

What is the shortcut key for Format Selection?
Ctrl+1 (the number one) will open the Format dialog for whatever is selected.


New Member
Sep 4, 2018
Does anyone know how to prevent the images destroying the memory?

Forum statistics

Latest member

This Week's Hot Topics

  • help please
    SORRY NOT ANY GOOD AT EXCEL SO HELP WOULD BE MUCH APPRECIATED this formula is in a sheet called ignore...
  • two formulas needed
    Hello, I'll try my best to explain this: First formula needed in Sheet1 cell A2: If Sheet1 cell B2 = Sheet2 cell B2 then return a 1. If not then...
  • Dynamic Counts
    Good afternoon, we are tidying up some data & the data seems to be growing quicker than we are tidying it up! What we confirm (by reviewing it...
  • Help Excel formula eliminate duplicate values and keep only 2 identical rows.
    as picture below column A has a duplicate value. but the values are not the same as the rule. sometimes 4 rows, sometimes 10 rows or 7 or 9...
  • Macro Compile Error Sub or Function not defined
    Hello, I am trying to run macros from a validation list, all macros have been created and run perfectly on there own but I'm getting a compile...
  • Last row combined with Current Region VBA
    I'm generally happy finding the last row of data through something like Lastrow = Cells(Rows.Count, "D").End(xlUp) but I don't always receive data...