+ Reply to Thread
Results 1 to 2 of 2

Excel 2010 - Use VBA to display only relevant images

Hybrid View

Duluth06ChE Excel 2010 - Use VBA to... 07-13-2012, 10:12 AM
Duluth06ChE Re: Excel 2010 - New Images... 07-20-2012, 02:40 PM
  1. #1
    Registered User
    Join Date
    07-13-2012
    Location
    Minneapolis, MN
    MS-Off Ver
    Excel 2010
    Posts
    3

    Excel 2010 - Use VBA to display only relevant images

    I'm a youth hockey coach and I'm trying to make a workbook to help planning practices easier for everyone in the association. I'd like the user to be able to select a drill from a drop down menu and have a corresponding image display next to it that shows how the drill is run. One one sheet, I have created a lookup table that includes the drill name, a description and a name for the image I want displayed when that option is selected. On a second sheet, I created the drop down menu using the data validation function and a dynamic formula so if the lookup table is added on to, the options in the drop down are automatically updated.

    The workbook should work by the user selecting an option from the drop down and the corresponding image name appearing in cell D3 (this works by using a simple VLOOKUP formula). I then have the following VBA code that runs whenever the sheet recalculates (i.e. drop down is changed).

    Private Sub Worksheet_Calculate()
        Dim oPic As Picture
        Me.Pictures.Visible = False
        With Me.Range("D3")
            For Each oPic In Me.Pictures
                If oPic.Name = .Text Then
                    oPic.Visible = True
                    oPic.Top = .Top
                    oPic.Left = .Left
                    Exit For
                End If
            Next oPic
        End With
    End Sub
    When I first made the sheet I had items in the table and 4 pictures pasted into the sheet with the drop down and everything worked fine. I saved the workbook as a .xlsm. Now when I add more items to the lookup table and paste and name the pictures in the sheet with the drop down menu, the sheet will not show the new images when that drill is selected. The 4 original images work just fine, but not any of the new ones. The really strange thing is that when the following line of code runs at the start of the macro...
    Me.Pictures.Visible = False
    ...all the picture disappear, even the new ones. So Excel knows they are in the Me.Pictures collection, but for some reason can't find their names.

    Thank you in advance for your help!
    Last edited by Duluth06ChE; 07-20-2012 at 02:45 PM.

  2. #2
    Registered User
    Join Date
    07-13-2012
    Location
    Minneapolis, MN
    MS-Off Ver
    Excel 2010
    Posts
    3

    Re: Excel 2010 - New Images Added to Worksheet Do Not Display with Code

    Solution,

    Rather than referencing the Me.Pictures collection, I instead referenced Me.Shapes. The code worked perfectly and I added some stuff to size the images and add a border. With the changes, the working code is as follows:

        Dim oShape As Shape
        Dim intRow As Integer, intCol As Integer
        Me.Shapes.Visible = False
        With Me.Cells(3, 4)
            For Each oShape In Me.Shapes
                If oShape.Name = .Text Then
                    oShape.Visible = True
                    oShape.Top = .Top
                    oShape.Left = .Left
                    oShape.Width = 250
                    oShape.Height = 160
                    oShape.Line.Visible = msoTrue
                    oShape.Line.Weight = 3
                    Exit For
                End If
            Next oShape
        End With

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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