Consulting

Results 1 to 10 of 10

Thread: Images copying to another cell

  1. #1

    Question Images copying to another cell

    I have an image in A1. Basically a Rugby team logo. I have another one in B1.

    In A2 I have the team name and in B2 I have the other team name.

    If team A2 wins their match, I want to copy the picture to D1. I will be manually inputting the team name into D2. Just wanted to find out if there was a way that depending on the team name in D2 it would copy the corresponding picture into D1.

    I hope this is kind of streamlined code because I have about 32 teams I am tracking for a tournament.

    Any and all help would be greatly appreciated.

  2. #2
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    You don't have images in a cell, it may look like it, but images are not embedded in cells, they are a separate layer.

    What you need to do is assign names to the images that is related to the team, such as imgWasps, and then derive that from the winning team name.

  3. #3
    I realize this. I am just not sure how to code that. I tried doing it in the excel environment but it does not recognize the name I gave the picture. So I am thinking it must be done via VBA.

    If anyone can give me the heads up even on the example I have given, I would be extremely appreciative!

  4. #4
    Hi Zendog

    You might want to take a look at this post (and thread)

    http://vbaexpress.com/forum/showpost...67&postcount=2

    The attached workbook simulates a Blackjack game. The code includes a part that copies pictures into certain cells:

    [vba] Serial = Round(Rnd(1) * 9) + 866
    ActiveSheet.Shapes("Picture " & CStr(Serial)).Copy
    Cells(1, Counter + 3).Select
    ActiveSheet.Paste
    [/vba] You refer to a picture by its name. In this particular example, pictures' names were "Picture 866", "Picture 867" etc., and the serial number was generated randomly when dealing a new card.

    This might be a good start. If not, then could you upload your workbook?


    Jimmy
    -------------------------------------------------
    The more details you give, the easier it is to understand your question. Don't save the effort, tell us twice rather than not at all. The amount of info you give strongly influences the quality of answer, and also how fast you get it.

  5. #5

    Well I tried

    I tried playing around with it a bit but still having problems with getting the pics to work out right. I have attached the sample workbook for your reference as previously asked for. Everything I am trying to accomplish is stated in the workbook.

    I hope someone can help me out here. I am fairly new to VBA but learning fast. I am stumped and now my stubborn side is tell me to find out how this is done.

    Thanks in advance for all your help!

  6. #6
    Administrator
    VP-Knowledge Base
    VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    Here's one method to try. Needs a bit more work!


    [vba]Option Explicit

    Private Sub Worksheet_Change(ByVal Target As Range)
    Dim TeamPic As Range, Pic

    If Target.Column = 2 Then
    Set TeamPic = Columns("P").Find(Target.Value).Offset(, 2)

    For Each Pic In ActiveSheet.Pictures
    If Pic.TopLeftCell.Address = Target.Offset(, -1).Address Then
    Pic.Delete
    GoTo NewPic
    End If
    Next

    NewPic:

    For Each Pic In ActiveSheet.Pictures
    If Pic.TopLeftCell.Address = TeamPic.Address Then
    Pic.Copy
    Target.Offset(, -1).Activate
    ActiveSheet.Paste
    Target.Activate
    Exit Sub
    End If
    Next
    End If
    End Sub
    [/vba]
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  7. #7
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    .

  8. #8
    That worked exactly how I wanted it. I do have a question however. When you set the TeamPic = Columns("P")... can I create a 'Data' sheet to store this information and hide it from the user then call it up that way? Something like:

    Set TeamPic = Worksheet("Data") Columns("P").Find(Target.Value).Offset(,2)

    Would something like this work or do the images have to reside on the sheet inwhich I intend to show them?

    Quote Originally Posted by mdmackillop
    Here's one method to try. Needs a bit more work!


    [vba]Option Explicit

    Private Sub Worksheet_Change(ByVal Target As Range)
    Dim TeamPic As Range, Pic

    If Target.Column = 2 Then
    Set TeamPic = Columns("P").Find(Target.Value).Offset(, 2)

    For Each Pic In ActiveSheet.Pictures
    If Pic.TopLeftCell.Address = Target.Offset(, -1).Address Then
    Pic.Delete
    GoTo NewPic
    End If
    Next

    NewPic:

    For Each Pic In ActiveSheet.Pictures
    If Pic.TopLeftCell.Address = TeamPic.Address Then
    Pic.Copy
    Target.Offset(, -1).Activate
    ActiveSheet.Paste
    Target.Activate
    Exit Sub
    End If
    Next
    End If
    End Sub
    [/vba]

  9. #9
    Administrator
    VP-Knowledge Base VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    Revised file attached
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  10. #10
    That is awesome. Thank you so much! Let the fun begin!

    Quote Originally Posted by mdmackillop
    Revised file attached

Posting Permissions

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