+ Reply to Thread
Results 1 to 8 of 8

Accessing Variables In Modules

Hybrid View

  1. #1
    Registered User
    Join Date
    10-07-2009
    Location
    Brisbane Australia
    MS-Off Ver
    Excel 2003
    Posts
    13

    Accessing Variables In Modules

    I have variables declared in the code for the Sheet1 (mLength,mWidth,mHeight) that once they have their values have been set I want to access it by the code for the command button in the Module. But for life of me I can't work our why the button isnt doing what the test code is set up for.

    I have gone through page after web page looking for solutions and havent found anything that works. I thought declaring the variables as Public would have made them accessible through all parts of the spreadsheets code.
    Attached Files Attached Files

  2. #2
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2010, 2019
    Posts
    40,689

    Re: Accessing Variables In Modules

    Public variables must be declared in a code module, not an object (sheet, thisworkbook, form, class) module.
    Entia non sunt multiplicanda sine necessitate

  3. #3
    Valued Forum Contributor
    Join Date
    07-21-2008
    Location
    London, UK
    Posts
    326

    Re: Accessing Variables In Modules

    Im running Excel 2003 so can not get the XLSM ???

    Can you post the Codes, sounds like your not set up quote right

    Cheeers

  4. #4
    Valued Forum Contributor
    Join Date
    07-21-2008
    Location
    London, UK
    Posts
    326

    Re: Accessing Variables In Modules

    Your module 1 code can not pull the variable set to public in the sheet1 area

    Not sure why your using code in the sheet modules

    Cheers,

  5. #5
    Valued Forum Contributor
    Join Date
    07-21-2008
    Location
    London, UK
    Posts
    326

    Re: Accessing Variables In Modules

    see if this helps

    Copy your workbook, delets all code and copy this code into the module one are and run...

    i cant test as do not have excel 2007, worth a try thou

    Public mLength As Double
    Public mWidth As Double
    Public mHeight As Double
    
    '// jack in the UK
    
    Private Sub cboUnit1_Change()
    '//
      If Sheet1.Range("D9").Value <= 0# Then
        MsgBox ("Invalid Dimension, Must Be Value Greater Than 0.")
        Sheet1.cboUnit1.Value = ""
      Else
        Select Case Sheet1.cboUnit1.Value
          Case "": MsgBox ("Please Select A Unit Of Measurement.")
          Case "in": mLength = Sheet1.Range("D9").Value * Sheet1.Range("BB2").Value
          Case "ft": mLength = Sheet1.Range("D9").Value * Sheet1.Range("BB3").Value
          Case "mm": mLength = Sheet1.Range("D9").Value * Sheet1.Range("BB4").Value
          Case "cm": mLength = Sheet1.Range("D9").Value * Sheet1.Range("BB5").Value
          Case "m": mLength = Sheet1.Range("D9").Value * Sheet1.Range("BB6").Value
        End Select
      End If
    End Sub
    
    Private Sub cboUnit2_Change()
    
      If Sheet1.Range("D11").Value <= 0# Then
        MsgBox ("Invalid Dimension, Must Be Value Greater Than 0.")
        Sheet1.cboUnit2.Value = ""
      Else
        Select Case Sheet1.cboUnit2.Value
          Case "": MsgBox ("Please Select A Unit Of Measurement.")
          Case "in": mLength = Sheet1.Range("D11").Value * Sheet1.Range("BB2").Value
          Case "ft": mLength = Sheet1.Range("D11").Value * Sheet1.Range("BB3").Value
          Case "mm": mLength = Sheet1.Range("D11").Value * Sheet1.Range("BB4").Value
          Case "cm": mLength = Sheet1.Range("D11").Value * Sheet1.Range("BB5").Value
          Case "m": mLength = Sheet1.Range("D11").Value * Sheet1.Range("BB6").Value
        End Select
      End If
    End Sub
    
    Private Sub cboUnit3_Change()
    Call mySheet1
      If Sheet1.Range("D13").Value <= 0# Then
        MsgBox ("Invalid Dimension, Must Be Value Greater Than 0.")
        Sheet1.cboUnit3.Value = ""
      Else
        Select Case Sheet1.cboUnit3.Value
          Case "": MsgBox ("Please Select A Unit Of Measurement.")
          Case "in": mLength = Sheet1.Range("D13").Value * Sheet1.Range("BB2").Value
          Case "ft": mLength = Sheet1.Range("D13").Value * Sheet1.Range("BB3").Value
          Case "mm": mLength = Sheet1.Range("D13").Value * Sheet1.Range("BB4").Value
          Case "cm": mLength = Sheet1.Range("D13").Value * Sheet1.Range("BB5").Value
          Case "m": mLength = Sheet1.Range("D13").Value * Sheet1.Range("BB6").Value
        End Select
      End If
    End Sub
    
    Sub cmdCalculateCubic()
      Sheet1.Range("D15").Value = (Sheet1.mLength * Sheet1.mWidth * Sheet1.mHeight) * 250#
      MsgBox (CDbl(Sheet1.Range("D15").Value))
    End Sub
    
    Sub cmdFindDeterminingWeight()
    '// Jack in the UK
    End Sub

  6. #6
    Forum Expert romperstomper's Avatar
    Join Date
    08-13-2008
    Location
    England
    MS-Off Ver
    365, varying versions/builds
    Posts
    21,997

    Re: Accessing Variables In Modules

    Your code should work fine other than the fact that the change events for all three comboboxes only set the mLength variable, so your result will always be 0.
    And of course the variables only have values after you change the comboboxes so if you simply open the workbook and press calculate, or don't change one of the comboboxes, you will get 0.
    Everyone who confuses correlation and causation ends up dead.

  7. #7
    Registered User
    Join Date
    10-07-2009
    Location
    Brisbane Australia
    MS-Off Ver
    Excel 2003
    Posts
    13

    Re: Accessing Variables In Modules

    Other than the fact I forgot to change the code for mWidth and mHeight when I copied and pasted. The other thing I wanted to know is the fact when I right-clicked on the command button and went through the process to create the code, and it put it into a seperate module to the combo box code that as long as the variables as Public I can access them anywhere? Even in the modules?

  8. #8
    Forum Expert romperstomper's Avatar
    Join Date
    08-13-2008
    Location
    England
    MS-Off Ver
    365, varying versions/builds
    Posts
    21,997

    Re: Accessing Variables In Modules

    You can access public variables from anywhere within the same project. If you declare them in a class module (as you did), you have to qualify every reference to them with the object to which they belong, as you did by using Sheet1.mLength

+ 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