+ Reply to Thread
Results 1 to 10 of 10

Global Variables

  1. #1
    Francis Brown
    Guest

    Global Variables

    Could some one explain how to set up some global variables.

    I Have code that uses application.username to test who is using my
    application.

    The code then accesses a userfile.xls sheet to find what management group
    the person belongs too and detects there manager etc. This is then used to
    setup where the users data is saved etc.

    I can get all of the above to work but the variables are only good for the
    module.

    How to I say make a variable good for all code in the project.

    eg if the users team is retrived from my code to a variable called Team. how
    to I keep that variable valid after the module closes.
    --
    Regards and Thanks for any assistance.

    Francis Brown.

  2. #2
    Joe Fish
    Guest

    Re: Global Variables

    Francis,
    You need to declare the variables as Public outside of a procedure. For
    example:

    Public rng As Range

    Sub MooCow ()
    'blah blah blah
    End Sub

    Hae fun,
    Fish


  3. #3
    Joe Fish
    Guest

    Re: Global Variables

    Now that I'm thinking of it, I'm pretty certain it has to be before the
    first procedure in a module. So actually, it's more like:

    Public rng As Range

    Set rng = Range("A1")

    Sub MooCow ()
    'blah blah blah
    End Sub


  4. #4
    JE McGimpsey
    Guest

    Re: Global Variables

    Try it!

    You'll find that your Set statement is invalid outside a procedure.

    Instead, use

    Public rng As Range

    Public Sub MooCow()
    'blah blah blah
    Set rng = Range("A1")
    End Sub

    In article <[email protected]>,
    "Joe Fish" <[email protected]> wrote:

    > Now that I'm thinking of it, I'm pretty certain it has to be before the
    > first procedure in a module. So actually, it's more like:
    >
    > Public rng As Range
    >
    > Set rng = Range("A1")
    >
    > Sub MooCow ()
    > 'blah blah blah
    > End Sub


  5. #5
    Gary''s Student
    Guest

    Re: Global Variables

    Joe is correct. If you just DIM them outside the procedures of a module,
    then they are by default private and only the procedures within that module
    can "see" them.

    By Declaring the PUBLIC, all procedures in all modules can see them.
    --
    Gary''s Student


    "Joe Fish" wrote:

    > Now that I'm thinking of it, I'm pretty certain it has to be before the
    > first procedure in a module. So actually, it's more like:
    >
    > Public rng As Range
    >
    > Set rng = Range("A1")
    >
    > Sub MooCow ()
    > 'blah blah blah
    > End Sub
    >
    >


  6. #6
    Joe Fish
    Guest

    Re: Global Variables

    Francis,
    He's right, the Set statement needs to be inside a module, but the
    Public declaration needs to be before the first statement in a module.
    Have Fun,
    Fish


  7. #7
    Francis Brown
    Guest

    Re: Global Variables

    I'M still having a little trouble. Here is my code

    Public name As String
    Public team As String
    Public TeamLeader As String
    Public CommandN As String



    Public Sub FindUser()
    currentuser = Application.UserName
    Workbooks.Open "Z:/Systemdown/Userfile.xls"
    lastrow =
    Workbooks("Userfile.xls").Sheets("UserData").Range("A65536").End(xlUp).Row

    For Each person In Workbooks("Userfile.xls").Sheets("UserData").Range("A2:A"
    & lastrow)
    If person.Value = currentuser Then
    Set name = person.Offset(0, 1).Value
    Set team = person.Offset(0, 2).Value

    lastrow2 =
    Workbooks("Userfile.xls").Sheets("UserData").Range("A65536").End(xlUp).Row
    For Each Teamgroup In
    Workbooks("Userfile.xls").Sheets("Command").Range("A2:A" & lastrow2)
    If Teamgroup.Value = team Then
    Set TeamLeader = Teamgroup.Offset(0, 1).Value

    Set CommandN = Teamgroup.Offset(0, 2).Value
    Else
    End If
    Next Teamgroup
    Else
    End If
    Next person
    Workbooks("Userfile.xls").Close

    End Sub

    The user file has two sheets.

    Userdata with colums ID, Name and Team
    Command with Team, Team Leader and Command.

    The Macro is meant to use application.UserName to find who is currently
    runing the program. It is then meant to look up from the tables there name,
    Team, Team Leader and Command. These are then going to used to set up the
    save directory on a shared drive for the user. That's why I need the
    variables to go Global as I need them in other modules.

    When I run the code as above I get mismatch errors when trying to set the
    data to the variable.

    Can someone suggest where i'm going wrong.


    --
    Regards and Thanks for any assistance.

    Francis Brown.


    "Gary''s Student" wrote:

    > Joe is correct. If you just DIM them outside the procedures of a module,
    > then they are by default private and only the procedures within that module
    > can "see" them.
    >
    > By Declaring the PUBLIC, all procedures in all modules can see them.
    > --
    > Gary''s Student
    >
    >
    > "Joe Fish" wrote:
    >
    > > Now that I'm thinking of it, I'm pretty certain it has to be before the
    > > first procedure in a module. So actually, it's more like:
    > >
    > > Public rng As Range
    > >
    > > Set rng = Range("A1")
    > >
    > > Sub MooCow ()
    > > 'blah blah blah
    > > End Sub
    > >
    > >


  8. #8
    Chip Pearson
    Guest

    Re: Global Variables

    Get rid of the 'Set' keyword wherever you have it. 'Set' is used
    only for object type variables, not simple variables.


    --
    Cordially,
    Chip Pearson
    Microsoft MVP - Excel
    Pearson Software Consulting, LLC
    www.cpearson.com

    "Francis Brown" <[email protected]> wrote in
    message
    news:[email protected]...
    > I'M still having a little trouble. Here is my code
    >
    > Public name As String
    > Public team As String
    > Public TeamLeader As String
    > Public CommandN As String
    >
    >
    >
    > Public Sub FindUser()
    > currentuser = Application.UserName
    > Workbooks.Open "Z:/Systemdown/Userfile.xls"
    > lastrow =
    > Workbooks("Userfile.xls").Sheets("UserData").Range("A65536").End(xlUp).Row
    >
    > For Each person In
    > Workbooks("Userfile.xls").Sheets("UserData").Range("A2:A"
    > & lastrow)
    > If person.Value = currentuser Then
    > Set name = person.Offset(0, 1).Value
    > Set team = person.Offset(0, 2).Value
    >
    > lastrow2 =
    > Workbooks("Userfile.xls").Sheets("UserData").Range("A65536").End(xlUp).Row
    > For Each Teamgroup In
    > Workbooks("Userfile.xls").Sheets("Command").Range("A2:A" &
    > lastrow2)
    > If Teamgroup.Value = team Then
    > Set TeamLeader = Teamgroup.Offset(0, 1).Value
    >
    > Set CommandN = Teamgroup.Offset(0, 2).Value
    > Else
    > End If
    > Next Teamgroup
    > Else
    > End If
    > Next person
    > Workbooks("Userfile.xls").Close
    >
    > End Sub
    >
    > The user file has two sheets.
    >
    > Userdata with colums ID, Name and Team
    > Command with Team, Team Leader and Command.
    >
    > The Macro is meant to use application.UserName to find who is
    > currently
    > runing the program. It is then meant to look up from the tables
    > there name,
    > Team, Team Leader and Command. These are then going to used to
    > set up the
    > save directory on a shared drive for the user. That's why I
    > need the
    > variables to go Global as I need them in other modules.
    >
    > When I run the code as above I get mismatch errors when trying
    > to set the
    > data to the variable.
    >
    > Can someone suggest where i'm going wrong.
    >
    >
    > --
    > Regards and Thanks for any assistance.
    >
    > Francis Brown.
    >
    >
    > "Gary''s Student" wrote:
    >
    >> Joe is correct. If you just DIM them outside the procedures
    >> of a module,
    >> then they are by default private and only the procedures
    >> within that module
    >> can "see" them.
    >>
    >> By Declaring the PUBLIC, all procedures in all modules can see
    >> them.
    >> --
    >> Gary''s Student
    >>
    >>
    >> "Joe Fish" wrote:
    >>
    >> > Now that I'm thinking of it, I'm pretty certain it has to be
    >> > before the
    >> > first procedure in a module. So actually, it's more like:
    >> >
    >> > Public rng As Range
    >> >
    >> > Set rng = Range("A1")
    >> >
    >> > Sub MooCow ()
    >> > 'blah blah blah
    >> > End Sub
    >> >
    >> >




  9. #9
    Francis Brown
    Guest

    Re: Global Variables

    Done this but now have other problem. Please see newer thread with name
    Global Variable.
    --
    Regards and Thanks for any assistance.

    Francis Brown.


    "Chip Pearson" wrote:

    > Get rid of the 'Set' keyword wherever you have it. 'Set' is used
    > only for object type variables, not simple variables.
    >
    >
    > --
    > Cordially,
    > Chip Pearson
    > Microsoft MVP - Excel
    > Pearson Software Consulting, LLC
    > www.cpearson.com
    >
    > "Francis Brown" <[email protected]> wrote in
    > message
    > news:[email protected]...
    > > I'M still having a little trouble. Here is my code
    > >
    > > Public name As String
    > > Public team As String
    > > Public TeamLeader As String
    > > Public CommandN As String
    > >
    > >
    > >
    > > Public Sub FindUser()
    > > currentuser = Application.UserName
    > > Workbooks.Open "Z:/Systemdown/Userfile.xls"
    > > lastrow =
    > > Workbooks("Userfile.xls").Sheets("UserData").Range("A65536").End(xlUp).Row
    > >
    > > For Each person In
    > > Workbooks("Userfile.xls").Sheets("UserData").Range("A2:A"
    > > & lastrow)
    > > If person.Value = currentuser Then
    > > Set name = person.Offset(0, 1).Value
    > > Set team = person.Offset(0, 2).Value
    > >
    > > lastrow2 =
    > > Workbooks("Userfile.xls").Sheets("UserData").Range("A65536").End(xlUp).Row
    > > For Each Teamgroup In
    > > Workbooks("Userfile.xls").Sheets("Command").Range("A2:A" &
    > > lastrow2)
    > > If Teamgroup.Value = team Then
    > > Set TeamLeader = Teamgroup.Offset(0, 1).Value
    > >
    > > Set CommandN = Teamgroup.Offset(0, 2).Value
    > > Else
    > > End If
    > > Next Teamgroup
    > > Else
    > > End If
    > > Next person
    > > Workbooks("Userfile.xls").Close
    > >
    > > End Sub
    > >
    > > The user file has two sheets.
    > >
    > > Userdata with colums ID, Name and Team
    > > Command with Team, Team Leader and Command.
    > >
    > > The Macro is meant to use application.UserName to find who is
    > > currently
    > > runing the program. It is then meant to look up from the tables
    > > there name,
    > > Team, Team Leader and Command. These are then going to used to
    > > set up the
    > > save directory on a shared drive for the user. That's why I
    > > need the
    > > variables to go Global as I need them in other modules.
    > >
    > > When I run the code as above I get mismatch errors when trying
    > > to set the
    > > data to the variable.
    > >
    > > Can someone suggest where i'm going wrong.
    > >
    > >
    > > --
    > > Regards and Thanks for any assistance.
    > >
    > > Francis Brown.
    > >
    > >
    > > "Gary''s Student" wrote:
    > >
    > >> Joe is correct. If you just DIM them outside the procedures
    > >> of a module,
    > >> then they are by default private and only the procedures
    > >> within that module
    > >> can "see" them.
    > >>
    > >> By Declaring the PUBLIC, all procedures in all modules can see
    > >> them.
    > >> --
    > >> Gary''s Student
    > >>
    > >>
    > >> "Joe Fish" wrote:
    > >>
    > >> > Now that I'm thinking of it, I'm pretty certain it has to be
    > >> > before the
    > >> > first procedure in a module. So actually, it's more like:
    > >> >
    > >> > Public rng As Range
    > >> >
    > >> > Set rng = Range("A1")
    > >> >
    > >> > Sub MooCow ()
    > >> > 'blah blah blah
    > >> > End Sub
    > >> >
    > >> >

    >
    >
    >


  10. #10
    David McRitchie
    Guest

    Re: Global Variables

    Hi Francis,
    You should continue in same thread.

    For those looking in the Google archives, you can beam on over to the other thread, at
    http://groups.google.com/groups?thre...TNGP09.phx.gbl

    making multiple posts (/threads) makes it hard to follow in archives, and
    usually means at least one person will waste time answering a question, and
    many people had to read the same question twice.


    "Francis Brown" <[email protected]> wrote...
    > Done this but now have other problem. Please see newer thread with name
    > Global Variable.




+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1