Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
DiscussionsAccessExcelInfoPathOutlookPowerPointPublisherWord
DirectoryUser Groups
Related Topics
Outlook ExpressInternet ExplorerWindowsMS Server ProductsMore Topics ...

MS Office Forum / Word / Programming / July 2007

Tip: Looking for answers? Try searching our database.

Variable from sub to sub

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
brownti - 10 Jul 2007 15:28 GMT
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?
Jay Freedman - 11 Jul 2007 00:57 GMT
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        FAQ: http://word.mvps.org
Email cannot be acknowledged; please post all follow-ups to the
newsgroup so all may benefit.

>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:
[quoted text clipped - 27 lines]
>commanbutton1 it is supposed to insert the picture, although it has trouble.
>Any ideas?
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.