+ Reply to Thread
Results 1 to 4 of 4

Can't show different images on different rows that match cell contents

Hybrid View

  1. #1
    Valued Forum Contributor
    Join Date
    01-16-2012
    Location
    England
    MS-Off Ver
    MS 365 Version 2501 64-bit
    Posts
    1,468

    Can't show different images on different rows that match cell contents

    Need to set the attached up so it can show numerous images in Col H at the same time, so need something more than the "1 image placeholder" approach.

    User selects or changes a Brand from any DropList in B2 - B4, and the relevant image from Col O should appear in Col H of that row
    .
    Tried two different approaches but neither work, so hope someone can see what I'm missing

    Approach 1:
    Private Sub Worksheet_Change(ByVal Target As Range)
    
           If Not Application.Intersect(Target, Range("E2:E6")) Is Nothing And Not Target.Count > 1 Then
        
            For Each shp In Me.Shapes
               If shp.Type = msoPicture Then
               shp.Visible = msoTrue
               End If
        
           Next
       
           r = Target.Row
         
         Select Case Range("E" & r).Value
         
             Case "Brand A"
             Me.Shapes("Picture 1").Visible = msoTrue
             Range("H" & r) = Me.Shapes("Picture 1")
              
             Case "Brand B"
             Me.Shapes("Picture 2").Visible = msoTrue
             range("H" & r) = Me.Shapes("Picture 2")
             
             Case "Brand C"
             Me.Shapes("Picture 3").Visible = msoTrue
             Range("H" & r) = Me.Shapes("Picture 3")
           
             End Select
                
            End If
            
            With Cells(r, 8)
             .ShapeRange.LockAspectRatio = msoFalse
            .Left = Cells(r, 5).Left
            .Top = Cells(r, 5).Top
            .Width = Cells(r, 5).Width
            .Height = Cells(r, 5).Height
            '.Name = Left(.Cells(r, 1).Value, Len(.Cells(r 1).Value))
            End With
            
        End If
        
    
    exitHandler:
        Application.EnableEvents = True
        Exit Sub
    
    End Sub
    APPROACH 2:
    Private Sub Worksheet_Change(ByVal Target As Range)
    
           If Not Application.Intersect(Target, Range("E2:E6")) Is Nothing And Not Target.Count > 1 Then
        
    With Cells(r, 8)
             .Pictures.Insert (.Cells(r, 5) & ".jpg")
            .ShapeRange.LockAspectRatio = msoFalse
            .Left = Cells(r, 5).Left
            .Top = Cells(r, 5).Top
            .Width = Cells(r, 5).Width
            .Height = Cells(r, 5).Height
            '.Name = Left(sh2.Cells(r, 1).Value, Len(sh2.Cells(k, 1).Value))
            End With
            
        End If
        
    exitHandler:
        Application.EnableEvents = True
        Exit Sub
    
    End Sub
    Hope someone can resolve this

    Ochimus
    Attached Files Attached Files
    Last edited by Ochimus; 01-18-2021 at 09:23 PM.

  2. #2
    Forum Expert
    Join Date
    10-06-2008
    Location
    Canada
    MS-Off Ver
    2007 / 2013
    Posts
    5,693

    Re: Can't show different images on different rows that match cell contents

    You could try this.
    Change references where required.
    Private Sub Worksheet_Change(ByVal Target As Range)
    Dim rng As Range, pic As Excel.Picture, pname As String
    Set rng = Range("E2:E4")
    
    If Not Application.Intersect(rng, Range(Target.Address)) Is Nothing Then
    
        For Each pic In ActiveSheet.Pictures
            If pic.TopLeftCell.Address = Columns(14).Find(Target.Value).Offset(, 1).Address Then pname = pic.Name: Exit For
        Next pic
    
        With ActiveSheet.Shapes(pname).Duplicate
            .Left = Columns(8).Left
            .Top = Cells(Target.Row, 8).Top
        End With
    
    End If
    End Sub

  3. #3
    Forum Guru Bo_Ry's Avatar
    Join Date
    09-10-2018
    Location
    Thailand
    MS-Off Ver
    MS 365
    Posts
    7,222

    Re: Can't show different images on different rows that match cell contents

    Please try

    Private Sub Worksheet_Change(ByVal Target As Range)
        Dim Pic As Picture, rn As Range
            If Not Application.Intersect(Target, Range("E2:E6")) Is Nothing And Target.Count = 1 Then
                For Each Pic In ActiveSheet.Pictures
                   If Pic.TopLeftCell.Address = Target.Offset(, 3).Address Then Pic.Delete
                Next
                Set rn = Range("N:N").Find(Target.Value).Offset(, 1)
                 For Each Pic In ActiveSheet.Pictures
                    If Pic.TopLeftCell.Address = rn.Address Then
                        Pic.Copy
                        Target.Offset(, 3).Select
                        ActiveSheet.Pictures.Paste
                    End If
                Next
           End If
           Target.Select
    End Sub
    Attached Files Attached Files

  4. #4
    Valued Forum Contributor
    Join Date
    01-16-2012
    Location
    England
    MS-Off Ver
    MS 365 Version 2501 64-bit
    Posts
    1,468

    Re: Can't show different images on different rows that match cell contents

    Many thanks to both for the prompt responses and solutions.

    Another one you have enabled me to park as Solved!

    Ochimus

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. [SOLVED] Show row if cell contents in column A match filter selection from column B
    By SHANNON MAUGHAN in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 10-13-2017, 01:28 AM
  2. [SOLVED] Show contents of cell, number of rows away from a specific cell
    By bluecat2013 in forum Excel General
    Replies: 2
    Last Post: 10-29-2014, 12:22 PM
  3. How to show Images in a Cell based on a condition?
    By e4excel in forum Excel Programming / VBA / Macros
    Replies: 20
    Last Post: 08-15-2014, 08:00 AM
  4. Replies: 6
    Last Post: 12-07-2012, 05:38 PM
  5. [SOLVED] Show the images based on the selective cell value
    By krishnakuma6 in forum Excel General
    Replies: 1
    Last Post: 11-12-2012, 06:21 AM
  6. Display different images based on contents of a cell
    By liam_bettinson in forum Excel Formulas & Functions
    Replies: 2
    Last Post: 08-01-2012, 06:34 AM
  7. Show Images in an Excel Cell
    By Sandy Ross in forum Excel General
    Replies: 2
    Last Post: 11-24-2009, 07:44 AM

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1