Variable from sub to sub

  • Thread starter brownti via OfficeKB.com
  • Start date
B

brownti via OfficeKB.com

I am trying to select a variable in one sub procedure and then use that
variable in another procedure. I am using the following code:

Dim paneltype As String

Sub CommandButton1_Click()
Dim myPict As Picture
Dim myRng As Range
With ActiveSheet
Set myRng = .Range("i16:m28")
Set myPict = .Pictures.Insert(paneltype)
myPict.top = myRng.top
myPict.Width = myRng.Width
myPict.Height = myRng.Height
myPict.Left = myRng.Left
myPict.Placement = xlMoveAndSize
End With
End Sub

Sub CommandButton2_Click()
Dim paneltype As Variant
paneltype = Application.GetOpenFilename
If paneltype = False Then
Exit Sub 'user hit cancel
End If
panelimage.Picture = StdFunctions.LoadPicture(paneltype)
End Sub

When i click commandbutton2 it selects the picture and then when i click
commanbutton1 it is supposed to insert the picture, although it has trouble.
Any ideas?
 
J

Jay Freedman

Remove the line "Dim paneltype As Variant" from the second procedure.

Read the VBA help article "Understanding Scope and Visibility". After
that you may understand that the declaration of a local variable
inside CommandButton2_Click makes the global variable of the same name
invisible to any code inside that procedure. Therefore, that procedure
defines a value for the local variable but leaves the global variable
without a value.

--
Regards,
Jay Freedman
Microsoft Word MVP
Email cannot be acknowledged; please post all follow-ups to the
newsgroup so all may benefit.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Top