+ Reply to Thread
Results 1 to 3 of 3

Passing variable from userform to module error

  1. #1
    Registered User
    Join Date
    04-30-2004
    Posts
    15

    Angry Passing variable from userform to module error

    I have a workbook containing 3 modules and 2 user forms. 1 user form (named Animals) contains a combobox to allow the user to select an animal. I don’t show the initialization step where I populate the combobox (named Animal_List) but that is working fine. Below are the details that pertain to my question:

    Module1:
    Public critter as String
    _______________________

    Sub Zoo ()
    Code
    .
    .
    Animals.Show
    Sheets(“List”).Select
    Activecell = critter
    .
    .
    .
    End Sub

    Animals (userform1):

    Private Sub Animal_List_Change()
    Rownumber = Animal_List.ListIndex
    Sheets(“Database”).Select
    Range(“B7”).Activate
    Activecell.Offset(Rownumber, 0).Activate
    Module1.critter = Activecell.Text
    End Sub


    So the user chooses an animal from the combobox, and I want to copy that animal’s name to another worksheet. This worked fine yesterday, but as of this morning when I run the macro I get an error when Private Sub Animal_List_Change() is activated (after the user chooses the animal from the combobox). The error says: “Compile Error: Method or data member not found” and it highlights the word critter within the Animal_List_Change() function. I changed the name of my variable in all 3 places that it is used above to ensure I didn’t use it elsewhere, but that didn’t fix the problem. I did add a second userform that cannot be activated while the first is running (it allows you to replace an animal that was already chosen in worksheet List with another animal), but I still think that this macro was working fine after those additions. I am at a complete loss since I had everything working correctly yesterday. Any help is greatly appreciated.

  2. #2
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,259
    Hello MWorth,

    Explicitly delcare critter as a Public String in your Module and remove Module1 prefix from the reference to Critter.

    In Module 1
    Public Critter As String

    This should fix the problem,
    Leith Ross

  3. #3
    Registered User
    Join Date
    04-30-2004
    Posts
    15
    Quote Originally Posted by mworth01
    Animals (userform1):

    Private Sub Animal_List_Change()
    Rownumber = Animal_List.ListIndex
    Sheets(“Database”).Select
    Range(“B7”).Activate
    Activecell.Offset(Rownumber, 0).Activate
    Module1.critter = Activecell.Text
    End Sub
    I figured out what I did wrong. All I have to do is to remove the "Module1" in the line: Module1.critter = Activecell.Text. I thought this was required so it knew which module that I declared that variable as Public. Turns out it's not necessary. Thought I would post this in case somebody else in the future encounters the same problem. Thanks anyway for reading.

+ 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