Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 121
Default pass a variable to a public sub

I have this sub routine to get the value of fname:
Public Sub lbVendor_Click()
Dim fname As String, spath As String
Dim wb As Workbook, ws As Worksheet
Dim found As Boolean

With lbVendor
fname = .List(.ListIndex) 'capture the value of the list index#
If obHouse Then
spath = "C:\Pricing\House\"
ElseIf obDan Then
spath = "C:\Pricing\Dan\"
ElseIf obEdwin Then
spath = "C:\Pricing\Edwin\"
ElseIf obJeff Then
spath = "C:\Pricing\Jeff\"
ElseIf obJohn Then
spath = "C:\Pricing\John\"
End If
End With

Workbooks.Open spath & fname
End Sub

Now I need to get the value of fname into this sub routine so that I can
cycle thru a different wb to find the matching ws name and open and copy
it so that I can paste it as a new ws in the wb in the above routine:
Public Sub GetMstrWS()
Dim fname As String
Workbook.Open "C:\Pricing\Outlook Master Pricing.xls"

found = False
For Each ws In Worksheets
If ws.Name = fname Then
found = True
Exit For
End If
Next ws
If found = True Then
Worksheet.Copy 'copy the entire worksheet
End If
End Sub

BTW, all of the above coding is either thru the generous help of the
mvps in these forums or from reading thru the forums, for which I am
very grateful.

Could someone please help me get fname from Public Sub lbVendor_Click
into Public Sub GetMstrWS? I was thinking maybe these subs need to be
functions because I thought I read somewhere in the forums that you need
a function if you want to share variables, but I tried it and it didn't
work at all - (could be my coding - I'm not real good at it)

Thanks for your time and consideration
Joanne
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 6,582
Default pass a variable to a public sub

It depends where you're calling GetMstrWS from. Generally you set up your
sub like this

Public Sub GetMstrWS(sName As String)
' code here which uses sName
End Sub

and you call it like this:

GetMstrWS sVariable

or

GetMstrWS "Name here"

- Jon
-------
Jon Peltier, Microsoft Excel MVP
Tutorials and Custom Solutions
Peltier Technical Services, Inc. - http://PeltierTech.com
_______



"Joanne" wrote in message
...
I have this sub routine to get the value of fname:
Public Sub lbVendor_Click()
Dim fname As String, spath As String
Dim wb As Workbook, ws As Worksheet
Dim found As Boolean

With lbVendor
fname = .List(.ListIndex) 'capture the value of the list index#
If obHouse Then
spath = "C:\Pricing\House\"
ElseIf obDan Then
spath = "C:\Pricing\Dan\"
ElseIf obEdwin Then
spath = "C:\Pricing\Edwin\"
ElseIf obJeff Then
spath = "C:\Pricing\Jeff\"
ElseIf obJohn Then
spath = "C:\Pricing\John\"
End If
End With

Workbooks.Open spath & fname
End Sub

Now I need to get the value of fname into this sub routine so that I can
cycle thru a different wb to find the matching ws name and open and copy
it so that I can paste it as a new ws in the wb in the above routine:
Public Sub GetMstrWS()
Dim fname As String
Workbook.Open "C:\Pricing\Outlook Master Pricing.xls"

found = False
For Each ws In Worksheets
If ws.Name = fname Then
found = True
Exit For
End If
Next ws
If found = True Then
Worksheet.Copy 'copy the entire worksheet
End If
End Sub

BTW, all of the above coding is either thru the generous help of the
mvps in these forums or from reading thru the forums, for which I am
very grateful.

Could someone please help me get fname from Public Sub lbVendor_Click
into Public Sub GetMstrWS? I was thinking maybe these subs need to be
functions because I thought I read somewhere in the forums that you need
a function if you want to share variables, but I tried it and it didn't
work at all - (could be my coding - I'm not real good at it)

Thanks for your time and consideration
Joanne



  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 121
Default pass a variable to a public sub

Jon
Thanks for your reply.
But I don't understand really what you are telling me.
I am running the first sub to identify 'fname' and open the
corresponding workbook. This works just fine.

In another workbook, there is a worksheet with the same name as the
value of fname along with about 60 other ws that could be the match,
depending on what the user chooses as 'fname'.

What I need to do is loop thru the ws.names on the second wb until I
find the one that matches 'fname' from the first wb. But I don't know
how to tell the loop what the value of fname is since it is in a
different sub-routine.

Both of these sub-routines are running on the userform. I tried putting
the value of fname into the () in the name of the looping (second wb)
routine, but that gave me a global method error.
I don't know where to go from here.
Jon Peltier wrote:

It depends where you're calling GetMstrWS from. Generally you set up your
sub like this

Public Sub GetMstrWS(sName As String)
' code here which uses sName
End Sub

and you call it like this:

GetMstrWS sVariable

or

GetMstrWS "Name here"

- Jon
-------
Jon Peltier, Microsoft Excel MVP
Tutorials and Custom Solutions
Peltier Technical Services, Inc. - http://PeltierTech.com
_______



"Joanne" wrote in message
...
I have this sub routine to get the value of fname:
Public Sub lbVendor_Click()
Dim fname As String, spath As String
Dim wb As Workbook, ws As Worksheet
Dim found As Boolean

With lbVendor
fname = .List(.ListIndex) 'capture the value of the list index#
If obHouse Then
spath = "C:\Pricing\House\"
ElseIf obDan Then
spath = "C:\Pricing\Dan\"
ElseIf obEdwin Then
spath = "C:\Pricing\Edwin\"
ElseIf obJeff Then
spath = "C:\Pricing\Jeff\"
ElseIf obJohn Then
spath = "C:\Pricing\John\"
End If
End With

Workbooks.Open spath & fname
End Sub

Now I need to get the value of fname into this sub routine so that I can
cycle thru a different wb to find the matching ws name and open and copy
it so that I can paste it as a new ws in the wb in the above routine:
Public Sub GetMstrWS()
Dim fname As String
Workbook.Open "C:\Pricing\Outlook Master Pricing.xls"

found = False
For Each ws In Worksheets
If ws.Name = fname Then
found = True
Exit For
End If
Next ws
If found = True Then
Worksheet.Copy 'copy the entire worksheet
End If
End Sub

BTW, all of the above coding is either thru the generous help of the
mvps in these forums or from reading thru the forums, for which I am
very grateful.

Could someone please help me get fname from Public Sub lbVendor_Click
into Public Sub GetMstrWS? I was thinking maybe these subs need to be
functions because I thought I read somewhere in the forums that you need
a function if you want to share variables, but I tried it and it didn't
work at all - (could be my coding - I'm not real good at it)

Thanks for your time and consideration
Joanne




  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 6,582
Default pass a variable to a public sub

If you are keeping the subs separate, then simply call the second sub from
the first:

Public Sub lbVendor_Click()
Dim fname As String, spath As String
Dim wb As Workbook, ws As Worksheet
Dim found As Boolean

With lbVendor
fname = .List(.ListIndex) 'capture the value of the list index#
If obHouse Then
spath = "C:\Pricing\House\"
ElseIf obDan Then
spath = "C:\Pricing\Dan\"
ElseIf obEdwin Then
spath = "C:\Pricing\Edwin\"
ElseIf obJeff Then
spath = "C:\Pricing\Jeff\"
ElseIf obJohn Then
spath = "C:\Pricing\John\"
End If
End With

Workbooks.Open spath & fname

GetMstrWS fName
End Sub


- Jon
-------
Jon Peltier, Microsoft Excel MVP
Tutorials and Custom Solutions
Peltier Technical Services, Inc. - http://PeltierTech.com
_______


"Joanne" wrote in message
...
Jon
Thanks for your reply.
But I don't understand really what you are telling me.
I am running the first sub to identify 'fname' and open the
corresponding workbook. This works just fine.

In another workbook, there is a worksheet with the same name as the
value of fname along with about 60 other ws that could be the match,
depending on what the user chooses as 'fname'.

What I need to do is loop thru the ws.names on the second wb until I
find the one that matches 'fname' from the first wb. But I don't know
how to tell the loop what the value of fname is since it is in a
different sub-routine.

Both of these sub-routines are running on the userform. I tried putting
the value of fname into the () in the name of the looping (second wb)
routine, but that gave me a global method error.
I don't know where to go from here.
Jon Peltier wrote:

It depends where you're calling GetMstrWS from. Generally you set up your
sub like this

Public Sub GetMstrWS(sName As String)
' code here which uses sName
End Sub

and you call it like this:

GetMstrWS sVariable

or

GetMstrWS "Name here"

- Jon
-------
Jon Peltier, Microsoft Excel MVP
Tutorials and Custom Solutions
Peltier Technical Services, Inc. - http://PeltierTech.com
_______



"Joanne" wrote in message
...
I have this sub routine to get the value of fname:
Public Sub lbVendor_Click()
Dim fname As String, spath As String
Dim wb As Workbook, ws As Worksheet
Dim found As Boolean

With lbVendor
fname = .List(.ListIndex) 'capture the value of the list index#
If obHouse Then
spath = "C:\Pricing\House\"
ElseIf obDan Then
spath = "C:\Pricing\Dan\"
ElseIf obEdwin Then
spath = "C:\Pricing\Edwin\"
ElseIf obJeff Then
spath = "C:\Pricing\Jeff\"
ElseIf obJohn Then
spath = "C:\Pricing\John\"
End If
End With

Workbooks.Open spath & fname
End Sub

Now I need to get the value of fname into this sub routine so that I
can
cycle thru a different wb to find the matching ws name and open and
copy
it so that I can paste it as a new ws in the wb in the above routine:
Public Sub GetMstrWS()
Dim fname As String
Workbook.Open "C:\Pricing\Outlook Master Pricing.xls"

found = False
For Each ws In Worksheets
If ws.Name = fname Then
found = True
Exit For
End If
Next ws
If found = True Then
Worksheet.Copy 'copy the entire worksheet
End If
End Sub

BTW, all of the above coding is either thru the generous help of the
mvps in these forums or from reading thru the forums, for which I am
very grateful.

Could someone please help me get fname from Public Sub lbVendor_Click
into Public Sub GetMstrWS? I was thinking maybe these subs need to be
functions because I thought I read somewhere in the forums that you
need
a function if you want to share variables, but I tried it and it didn't
work at all - (could be my coding - I'm not real good at it)

Thanks for your time and consideration
Joanne






  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 121
Default pass a variable to a public sub

Thank you
Jon Peltier wrote:

If you are keeping the subs separate, then simply call the second sub from
the first:

Public Sub lbVendor_Click()
Dim fname As String, spath As String
Dim wb As Workbook, ws As Worksheet
Dim found As Boolean

With lbVendor
fname = .List(.ListIndex) 'capture the value of the list index#
If obHouse Then
spath = "C:\Pricing\House\"
ElseIf obDan Then
spath = "C:\Pricing\Dan\"
ElseIf obEdwin Then
spath = "C:\Pricing\Edwin\"
ElseIf obJeff Then
spath = "C:\Pricing\Jeff\"
ElseIf obJohn Then
spath = "C:\Pricing\John\"
End If
End With

Workbooks.Open spath & fname

GetMstrWS fName
End Sub


- Jon
-------
Jon Peltier, Microsoft Excel MVP
Tutorials and Custom Solutions
Peltier Technical Services, Inc. - http://PeltierTech.com
_______


"Joanne" wrote in message
...
Jon
Thanks for your reply.
But I don't understand really what you are telling me.
I am running the first sub to identify 'fname' and open the
corresponding workbook. This works just fine.

In another workbook, there is a worksheet with the same name as the
value of fname along with about 60 other ws that could be the match,
depending on what the user chooses as 'fname'.

What I need to do is loop thru the ws.names on the second wb until I
find the one that matches 'fname' from the first wb. But I don't know
how to tell the loop what the value of fname is since it is in a
different sub-routine.

Both of these sub-routines are running on the userform. I tried putting
the value of fname into the () in the name of the looping (second wb)
routine, but that gave me a global method error.
I don't know where to go from here.
Jon Peltier wrote:

It depends where you're calling GetMstrWS from. Generally you set up your
sub like this

Public Sub GetMstrWS(sName As String)
' code here which uses sName
End Sub

and you call it like this:

GetMstrWS sVariable

or

GetMstrWS "Name here"

- Jon
-------
Jon Peltier, Microsoft Excel MVP
Tutorials and Custom Solutions
Peltier Technical Services, Inc. - http://PeltierTech.com
_______



"Joanne" wrote in message
...
I have this sub routine to get the value of fname:
Public Sub lbVendor_Click()
Dim fname As String, spath As String
Dim wb As Workbook, ws As Worksheet
Dim found As Boolean

With lbVendor
fname = .List(.ListIndex) 'capture the value of the list index#
If obHouse Then
spath = "C:\Pricing\House\"
ElseIf obDan Then
spath = "C:\Pricing\Dan\"
ElseIf obEdwin Then
spath = "C:\Pricing\Edwin\"
ElseIf obJeff Then
spath = "C:\Pricing\Jeff\"
ElseIf obJohn Then
spath = "C:\Pricing\John\"
End If
End With

Workbooks.Open spath & fname
End Sub

Now I need to get the value of fname into this sub routine so that I
can
cycle thru a different wb to find the matching ws name and open and
copy
it so that I can paste it as a new ws in the wb in the above routine:
Public Sub GetMstrWS()
Dim fname As String
Workbook.Open "C:\Pricing\Outlook Master Pricing.xls"

found = False
For Each ws In Worksheets
If ws.Name = fname Then
found = True
Exit For
End If
Next ws
If found = True Then
Worksheet.Copy 'copy the entire worksheet
End If
End Sub

BTW, all of the above coding is either thru the generous help of the
mvps in these forums or from reading thru the forums, for which I am
very grateful.

Could someone please help me get fname from Public Sub lbVendor_Click
into Public Sub GetMstrWS? I was thinking maybe these subs need to be
functions because I thought I read somewhere in the forums that you
need
a function if you want to share variables, but I tried it and it didn't
work at all - (could be my coding - I'm not real good at it)

Thanks for your time and consideration
Joanne








Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Public Variable Marvin Excel Programming 1 March 29th 07 09:50 PM
Public variable Jack New Users to Excel 4 March 18th 06 09:35 PM
Pass public variable from one userform to a second... Mike Dunworth Excel Programming 2 September 4th 05 12:30 AM
Using Public to Pass Variable D.Parker Excel Programming 8 March 24th 05 10:39 PM
public variable marwan hefnawy Excel Programming 1 September 5th 03 08:54 AM


All times are GMT +1. The time now is 04:15 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"