PDA

View Full Version : Solved: Looping Images



Kindly_Kaela
01-04-2007, 03:05 PM
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)...


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



Thanks!
Kaela

Bob Phillips
01-04-2007, 04:10 PM
Image1.Visible = variable = 1
Image2.Visible = variable = 2
Image3.Visible = variable = 3

Bob Phillips
01-04-2007, 04:14 PM
Here is another way, haven't tested it, got a bit of a problem at the moment



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

XLGibbs
01-04-2007, 04:34 PM
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..

Kindly_Kaela
01-05-2007, 07:31 AM
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

moa
01-05-2007, 07:55 AM
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 shp.Visible =to be the result of that test so, for example, if your variable is 1 and the image is "Image2" then... Right(shp.Name, Len(shp.Name) - 5) = variable
equates to false.

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

HTH

Kindly_Kaela
01-05-2007, 08:04 AM
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.

:beerchug:

moa
01-05-2007, 08:15 AM
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.