Replace the Text in excel cells with Images

Posted on 2012-09-04
Last Modified: 2012-09-07
I have a sample attached file which has some 1500 housie tickets.

I would like to replace the numbers with the corresponding image for that number.

I have seperate image files or if required I can copy the images in a cell in excel.  But there is no find and replace command from text to image.

the total numbers will range from 1 to 90 and corresponding 90 images also are ready.
Question by:dilipsjain
    LVL 10

    Expert Comment

    The following function will choose the appropriate shape:
    Function selectShape(v) As Shape
    Dim found As Boolean
    Dim s As Shape
    Dim theRow As Variant
        Set selectShape = Nothing
        theRow = Application.VLookup(v, Sheets("Images").Range("A:A"), 1)
        If IsError(theRow) Then Exit Function
        found = False
        For Each s In ActiveSheet.Shapes
            If s.TopLeftCell.Row = theRow Then
                found = True
                Exit For
            End If
        Next s
        If found Then Set selectShape = s
    End Function

    Open in new window


    Author Comment

    Hi Tdlevis,

    Thanks for the comment...

    Actually I am very poor at programming etc.  Would it be possible for you to put this function or so live in the excel sheet which I sent to you, so that It becomes possible... What I will do afterwards is copy all the 90 shapes to images sheet with appropriate numbers.

    Would it be possible to do the necessary changes in the attached sheet...

    It will be a great help to me...
    LVL 10

    Accepted Solution

    OK... I added a subroutine to replace the active cell with the corresponding image (if one can be found). The routine also adjusts the cell height and width to match the cell where the image was found.

    Note that the file type had to change to xlsm in order to store the macro.
    Sub CopyNumberedShape()
    Dim theCell As Range
    Dim theShape As Shape
        Set theCell = Selection.Cells(1, 1)
        Set theShape = selectShape(theCell.Value)
        If Not theShape Is Nothing Then
            If theCell.ColumnWidth < theShape.TopLeftCell.ColumnWidth Then _
                theCell.ColumnWidth = theShape.TopLeftCell.ColumnWidth
            If theCell.RowHeight < theShape.TopLeftCell.RowHeight Then _
                theCell.RowHeight = theShape.TopLeftCell.RowHeight
            theCell.Value = ""
        End If
    End Sub

    Open in new window


    Author Comment

    Thanks Tdlevis...
    This is what I wanted... Just One more request... This is replacing one number at a time.  Is there any way where I could replace all the numbers together or may be looping the macro for all the numbers...

    thank you so much :)  

    I accept this as my solution.. Just a final help needed.
    LVL 10

    Expert Comment

    Writing the code to replace all the numbers is slightly complicated by all the empty cells. Looking at column A implies there are three rows, but looking at column D one would only find two rows. Blank columns make it impossible to select the "Current Region" to get everything. (For example, the "Current Region" for cell A1 is A1:I3.)

    Here are some options:
    1. Before running the macro, select the cells to be replaced.
    2. Name the region where the cells are to be replaced.
    3. Hard code the region (currently it's A1:AC3

    Are any of these options close to what you had in mind?

    Author Closing Comment

    Thank u so much....

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Maximize Your Threat Intelligence Reporting

    Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

    Sometimes we don't want to show zeros in our Excel spreadsheets. This is sometimes most evident in our charts. Look at the chart below, all the zero values are visible. I think that all will agree with the fact that zero values are not looking nice …
    Approximate matching with VLOOKUP and MATCH seems to me to be a greatly under-used technique, and one which is vital for getting good performance out of large lookups. Until recently I would always have advised using an exact match for simplicity an…
    This Micro Tutorial will demonstrate on a Mac how to change the sort order for chart legend values and decrpyt the intimidating chart menu.
    This Micro Tutorial will demonstrate in Microsoft Excel how to add style and sexy appeal to horizontal bar charts.

    779 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    15 Experts available now in Live!

    Get 1:1 Help Now