Consulting

Results 1 to 10 of 10

Thread: Enter image based on cell value

  1. #1
    VBAX Newbie
    Joined
    Apr 2009
    Posts
    1
    Location

    Enter image based on cell value

    Hi,
    I need a vb script to display a image in column A based on the cell value of column B.
    For example, if the value of column B is Carrera, column A should display carrera.bmp.

    The images reside on c:\images

    Thanks in advance.

  2. #2
    VBAX Contributor
    Joined
    Jul 2004
    Location
    Gurgaon, India
    Posts
    148
    Location
    Hi,

    Try this UDF.

    [vba]Function ShowPicD(PicFile As String) As Boolean
    'By Damon Ostrander
    'http://www.mrexcel.com/forum/showthread.php?t=100737&
    Dim AC As Range
    Static P As Shape
    On Error GoTo Done
    Set AC = Application.Caller
    If PicExists(P) Then
    P.Delete
    Else
    'look for a picture already over cell
    For Each P In ActiveSheet.Shapes
    If P.Type = msoLinkedPicture Then
    If P.Left >= AC.Left And P.Left < AC.Left + AC.Width Then
    If P.Top >= AC.Top And P.Top < AC.Top + AC.Height Then
    P.Delete
    Exit For
    End If
    End If
    End If
    Next P
    End If
    Set P = ActiveSheet.Shapes.AddPicture(PicFile, True, True, AC.Left, AC.Top, 200, 200)
    ShowPicD = True
    Exit Function
    Done:
    ShowPicD = False
    End Function
    Function PicExists(P As Shape) As Boolean
    'Return true if P references an existing shape
    Dim ShapeName As String
    On Error GoTo NoPic
    If P Is Nothing Then GoTo NoPic
    ShapeName = P.Name
    PicExists = True
    NoPic:
    PicExists = False
    End Function[/vba]
    and try

    =showpicd("c:\images\"&b2&".bmp")

    where b2 houses the image name

    HTH

  3. #3
    Hi,
    i like this idea and try to use it but it doesnt works for me.
    what i did:
    - create folder images on C: and insert there a picture 1.jpg
    - copy this code to modules of sheet and in workbook too
    - insert formula [vba]=showpicd("C:\images\"&B2&".jpg")[/vba] in A2 and in B2 i write number 1 (this is name of my image)

    can you help me?
    thx

    result: error:
    #NAME?

  4. #4
    VBAX Contributor
    Joined
    Jul 2004
    Location
    Gurgaon, India
    Posts
    148
    Location
    Copy the code into a standard module rather than sheet/workbook module.

  5. #5
    i did it, but it didnt help.
    i copy the code to my personal modul ant the same error

  6. #6
    VBAX Regular JONvdHeyden's Avatar
    Joined
    Mar 2009
    Location
    Hampshire, UK
    Posts
    75
    Location
    Hello

    I usually tend to add pictures to comments. Debra has a nice piece for sample code of how to add pictures in a list to comments in the adjacent cells. Link here: http://www.contextures.com/xlcomment...ml#PictureFile

    HTH
    Regards,
    Jon von der Heyden (Excel 2003, OS XP Pro)

  7. #7
    this look good, but i need something, what will works like:
    if this condition 1, show this pic, if = 2 this pic2 ...
    but this code with comments i save for me too...maybe i will use it next time..

  8. #8
    VBAX Contributor
    Joined
    Jul 2004
    Location
    Gurgaon, India
    Posts
    148
    Location
    Quote Originally Posted by danovkos
    i did it, but it didnt help.
    i copy the code to my personal modul ant the same error
    See the attached.

    HTH

  9. #9
    yes, this works great, thank you.

    and how can i do, that the source of picture will be picture, inserted in the same workbook, but other sheet? I mean, that source of picture will not from any path, but from the same workbook.

  10. #10
    hi all,
    can anyone tell me, how can i do, that the picture, which is inserted will be not locked?

    i try use this example in my DB, but i need to have protected workbook. But if i do this, the code doesnt works good. It will appear the picture, but dont wont to dissaper when i have my wb protected.
    If wb is not protected, it works good.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •