PDA

View Full Version : HELP With Code



zendog1960
01-27-2007, 12:42 AM
Is there a way to get this code so it looks at the two different ranges individually? Right now it runs both ranges and with about 30 pics, it would really bog down the workbook.



Private Sub Worksheet_Calculate()
Dim oPic As Picture
Me.Pictures.Visible = False
With Range("F15")
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
With Range("F2")
For Each tPic In Me.Pictures
If tPic.Name = .Text Then
tPic.Visible = True
tPic.Top = .Top
tPic.Left = .Left
Exit For
End If
Next tPic
End With
End Sub


:banghead::dunno:bug:

Bob Phillips
01-27-2007, 04:30 AM
This is not what I meant in my previous reply. I meant that if the value was say "Red" you would call the associated image imgRed and address it directly, like this


Private Sub Worksheet_Calculate()
Dim oPic As Picture
Me.Pictures.Visible = False
With Me.Range("F2")
Me.Pictures("img" & .Text).Visible = True
Me.Pictures("img" & .Text).Top = .Top
Me.Pictures("img" & .Text).Left = .Left
End With
With Me.Range("F15")
Me.Pictures("img" & .Text).Visible = True
Me.Pictures("img" & .Text).Top = .Top
Me.Pictures("img" & .Text).Left = .Left
End With
End Sub


But again, why are you using the Calculate event, this will run the code every time the sheet recalculates, even if F2 or F15 don't c hange. Shouldn't you use the Change event.

zendog1960
01-27-2007, 08:25 PM
I tried a change event once with my code and I got errors. Let me try with this.

zendog1960
01-27-2007, 08:40 PM
the is what I get:

Run-time error '1004'
Method 'Pictures' of object_worksheet' failed

Here is the code I have


Private Sub Worksheet_Calculate()
Dim oPic As Picture
Me.Pictures.Visible = False
With Me.Range("F2")
Me.Pictures("img" & .Text).Visible = True
Me.Pictures("img" & .Text).Top = .Top
Me.Pictures("img" & .Text).Left = .Left
End With
With Me.Range("F15")
Me.Pictures("img" & .Text).Visible = True
Me.Pictures("img" & .Text).Top = .Top
Me.Pictures("img" & .Text).Left = .Left
End With
End Sub


I have the following picture names"

for F15 imgBob, imgCarol, imgTed, imgAlice

For F2 imgBrowns and imgColts

Why do I get this error?

lucas
01-27-2007, 08:47 PM
I'm going to assume that you put that code in the module for the sheet that holds the pictures.....right?

zendog1960
01-27-2007, 11:14 PM
you are correct. it is sheet 1 module

Bob Phillips
01-28-2007, 03:55 AM
Look mokay. Can you post the workbook?