Consulting

Results 1 to 11 of 11

Thread: Public Variables don't work

  1. #1

    Public Variables don't work

    I'm using MS Office 2003 in an Windows XP Pro environment.

    I'm developing an inventory set of macros. I have it working in a manner that requires manual intervention and want to integrate the modules together to automate the process. Now, in the process of integrating the modules I've declared variables that exist in more than one module as Public. I did so in the Main (master) module. However, when I go to use one of the variables in another module I get ambiguous name errors. Does Public delcaration really work, shouldn't I be able to reference the Public variable in any module within the same project? If I declare a variable once again as Public in the respective module(s) I don't get the error. Must I declare Public variables in every module? Thanks in advance for your assistance.

    Public icnt as integer
    Sub ABC()

    icnt = icnt + 20
    Call XYZ

    end sub

    Sub XYZ()

    if icnt > 20 then
    ' do something
    end if

    end sub
    Steve

  2. #2
    Moderator VBAX Wizard lucas's Avatar
    Joined
    Jun 2004
    Location
    Tulsa, Oklahoma
    Posts
    7,323
    Location
    Im guessing there is somthing else wrong. This works:
    [VBA]Option Explicit
    Public icnt As Integer
    Sub ABC()
    icnt = 10
    icnt = icnt + 20
    MsgBox icnt
    End Sub
    Sub XYZ()
    icnt = 21
    If icnt > 20 Then
    MsgBox "over"
    End If
    End Sub[/VBA]
    Steve
    "Nearly all men can stand adversity, but if you want to test a man's character, give him power."
    -Abraham Lincoln

  3. #3
    Moderator VBAX Wizard lucas's Avatar
    Joined
    Jun 2004
    Location
    Tulsa, Oklahoma
    Posts
    7,323
    Location
    this works:
    [vba]Option Explicit
    Public icnt As Integer
    Sub ABC()
    icnt = 10
    icnt = icnt + 20
    Call XYZ
    End Sub
    Sub XYZ()
    If icnt > 20 Then
    MsgBox "over"
    End If
    End Sub
    [/vba]
    Steve
    "Nearly all men can stand adversity, but if you want to test a man's character, give him power."
    -Abraham Lincoln

  4. #4
    Administrator
    VP-Knowledge Base
    VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    You need to run ABC twice to get a result > 20
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  5. #5
    You are correct. My mistake for not providing a more accurate example. Lets say that Sub ABC is in module TestABC while Sub XYZ is in module TestXYZ. Both of which are in Project Test. I don't have access to the PC where the code is located which is why I'm using this example. In this scenario when XYZ is executed it gets an ambiguous name error.

    ProjectTest.xls:

    Module TestABC:

    Option Explicit
    Public icnt AsInteger
    Sub ABC()
    icnt = 10
    icnt = icnt + 20
    Call XYZ
    End Sub

    Module TestXYZ:


    Option Explicit
    Sub XYZ()
    If icnt > 20 Then
    MsgBox "over"
    EndIf
    End Sub

  6. #6
    Administrator
    VP-Knowledge Base VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    Try this
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  7. #7
    MDMack,Yep that's the way I envisioned it should work, but for some reason I'm getting the ambiguous name error. I wonder if I need to comment out all Public statements in the subroutine that is being called from the Main. However, it does appear that the error is associated with a particular variable that is only declared as Public in the Main subroutine. By the way, I like how you setup the Run ABC button. How is that done? Also, thank you very much for taking the time to help in this matter I truly appreciate your time and efforts. Steve

  8. #8
    Moderator VBAX Wizard lucas's Avatar
    Joined
    Jun 2004
    Location
    Tulsa, Oklahoma
    Posts
    7,323
    Location
    ambiguous name error usually means you have two subs with the same name.
    Steve
    "Nearly all men can stand adversity, but if you want to test a man's character, give him power."
    -Abraham Lincoln

  9. #9
    Administrator
    VP-Knowledge Base VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    To add a button, Open the Forms toolbar, Click the button tool and draw a button. You'll be prompted to assign a macro
    Also see here
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  10. #10
    Lucas, definitely do not have two subs named the same.

    MDMack, thanks, pretty nifty.

  11. #11
    Lucas/MDMack,

    Just wanted to let you know that I got the public statements to work. I moved all the Public statements to the very first subroutine and did not remove all other Public statements from the various subroutines that were called. Once I did everything seem to function as designed. Took some time to get it to work. THANK YOU both for YOUR assistance.

    Steve

Posting Permissions

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