Consulting

Results 1 to 8 of 8

Thread: Solved: Looping Images

  1. #1

    Solved: Looping Images

    Hi Everyone!!

    I'm wondering if it's possible to run a loop that will incrementally increase the number of an image, textbox, etc.

    Specifically, my program displays Image 1 if X = 1, Image 2 if X = 2, Image 3 if X=3, etc. I have 90 images, so I am looking for a simple way to write this.

    So far my program looks like this (only displaying 3, but it really is 90)...

    [vba]
    Sub ImageChange_Click()

    Image1.Visible = False
    Image2.Visible = False
    Image3.Visible = False

    If variable = 1 Then
    Image1.Visible = True
    Image2.Visible = False
    Image3.Visible = False
    ElseIf variable = 2 Then
    Image1.Visible = False
    Image2.Visible = True
    Image3.Visible = False
    ElseIf variable = 3 Then
    Image1.Visible = False
    Image2.Visible = False
    Image3.Visible = True
    End If

    End Sub

    [/VBA]

    Thanks!
    Kaela

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

    Image1.Visible = variable = 1
    Image2.Visible = variable = 2
    Image3.Visible = variable = 3

    [/vba]

  3. #3
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Here is another way, haven't tested it, got a bit of a problem at the moment

    [vba]

    Dim shp As Shape
    Dim i As Long

    For Each shp In ActiveSheet.Shapes
    If Left(shp.Name,5) = "Image" Then
    shp.Visible = Right(shp.Name, Len(shp.Name) - 5) = variable
    End If
    Next shp
    [/vba]

  4. #4
    VBAX Master XLGibbs's Avatar
    Joined
    Jan 2006
    Location
    state of confusion, but vacation in denial
    Posts
    1,315
    Location
    KK, you would still have to pass the "variable" # through to the procedure.

    In your original procedure, I don't see where 'variable' gets populated with a value?

    If you pass a "variable" number through to the procedure XLD wrote, it would likely work..
    If you have posted the same question at multiple forums, please read this IMPORTANT INFO.

    Please use the thread tools to mark your thread Solved


    Please review the Knowledge Base
    for samples and solutions , or to submit your own!




  5. #5
    Depending on the situation, each time the form opens, the variable would change. The variable is calculated in the Excel sheets. So when the form opens, depending on the calculation, a different picture would show up. I know how to assign the variable, and that's why I used the generic name "variable" above.

    I haven't tried the code above yet, it looks complicated. I really dislike being the new girl and being so clueless.

    Thanks,
    Kaela

  6. #6
    VBAX Contributor moa's Avatar
    Joined
    Nov 2006
    Posts
    177
    Location
    K, I've been at this for 139 years and I still feel like a newbie compared to some of these people.

    xld's code is testing wether the number in the image's name matches the variable number. He sets the visible property [vba]shp.Visible =[/vba]to be the result of that test so, for example, if your variable is 1 and the image is "Image2" then... [vba]Right(shp.Name, Len(shp.Name) - 5) = variable[/vba]
    equates to false.

    this assumes that you are actually naming your images "Image1", "Image2" etc.

    HTH
    Glen

  7. #7
    Thank you so much for walking me through it like that. I just started playing with VBA a month ago, and I obviously need a lot of help. But I am hungry to learn.


  8. #8
    VBAX Contributor moa's Avatar
    Joined
    Nov 2006
    Posts
    177
    Location
    Not a problem, it's a pretty tidy bit of code from xld too.

    Actually I'm kind of in the same boat as you. Just started a job using Excel/VBA a few months ago and have very little experience with it. This was the first and only excel forum I joined (I think) and it's a good'n.

    Anyway, good luck with your project.
    Glen

Posting Permissions

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