Consulting

Page 1 of 2 1 2 LastLast
Results 1 to 20 of 23

Thread: Solved: Problem of accessibility within a project

  1. #1
    VBAX Contributor
    Joined
    Jun 2006
    Posts
    135
    Location

    Solved: Problem of accessibility within a project

    I have a project divided into several subs, with some global variables. It works perfectly on 2 computers (mine and another one that I tested), but it doesn't work on a 3rd computer.

    The code looks like this:

    [vba]
    Public Sub Main ()

    Dim k As Integer

    Call Macro1

    ...


    End Sub

    Public Sub Macro1 ()

    k = ...

    End Sub
    [/vba]

    I don't declare k in Macro1 again, and the 3rd computer gives an error "Can't access to the project" when I try to access k. Why isn't it working? What have I got to change on this computer so that everything works properly

  2. #2
    Moderator VBAX Wizard lucas's Avatar
    Joined
    Jun 2004
    Location
    Tulsa, Oklahoma
    Posts
    7,323
    Location
    Discussion on global variables and scope that might be what you need:
    http://vbaexpress.com/forum/showthread.php?t=8554
    Steve
    "Nearly all men can stand adversity, but if you want to test a man's character, give him power."
    -Abraham Lincoln

  3. #3
    VBAX Contributor
    Joined
    Jun 2006
    Posts
    135
    Location
    Hum, that's crazy

    I didn't precise it, but Macro1 is in another module.

    K is declared in the main Sub, but I call Macro1 before ending Sub, so k should still exist, shouldn't it?

    What is most crazy is that everything works perfectly well on my computer, so maybe it's just a question of trust in the options somewhere. I guess I could just declare the variable as Public, but I'd like to understand.

  4. #4
    Moderator VBAX Wizard lucas's Avatar
    Joined
    Jun 2004
    Location
    Tulsa, Oklahoma
    Posts
    7,323
    Location
    A good quote from Gerry on the subject:
    Well, yes of course you can write it each procedure. But No, No, No - it will not have the same effect as declaring it as Public. The value would never be passed from procedure to procedure.

    As stated, a variable declared in a procedure only exists in that procedure. You can have 10 procedures with each one having a variable named myVar. Each myVar is absolutely independent of the others. They ONLY hold value within that procedure.
    Quote by Ken Puls on the subject:
    This is an issue of "scope."
    Scope describes the "visibility" of a variable. If you make your variable declaration inside a routine (as you have in this case) it is only available inside that routine.

    You can raise the scope to module level by making the declaration at the start of the module, outside any routines. It will now be visible to all the module's procedures and functions
    Steve
    "Nearly all men can stand adversity, but if you want to test a man's character, give him power."
    -Abraham Lincoln

  5. #5
    VBAX Contributor
    Joined
    Jun 2006
    Posts
    135
    Location
    Even when I used public names for variables, I got an error for the following line, which works perfectly fine on my computer:

    Left(Text, Len(SearchString) + 1) = SearchString & " "

    where Text and SearchString are strings. Do you need an add-in to use the Left function which might not be installed on the other computer?

  6. #6
    VBAX Mentor ALe's Avatar
    Joined
    Aug 2005
    Location
    Milan
    Posts
    383
    Location
    why don't
    [VBA]
    Public k As Integer
    Public Sub Main ()



    Call Macro1

    ...


    End Sub

    Public Sub Macro1 ()

    k = ...
    End Sub
    [/VBA]

  7. #7
    VBAX Contributor
    Joined
    Jun 2006
    Posts
    135
    Location
    Yes that's what I did but I still have another problem a bit further

    Left(Text, Len(SearchString) + 1) = SearchString & " "

    where everything is defined in the good sub, and it still doesn't work.

  8. #8
    Moderator VBAX Wizard lucas's Avatar
    Joined
    Jun 2004
    Location
    Tulsa, Oklahoma
    Posts
    7,323
    Location
    what is the error?
    Steve
    "Nearly all men can stand adversity, but if you want to test a man's character, give him power."
    -Abraham Lincoln

  9. #9
    VBAX Contributor
    Joined
    Jun 2006
    Posts
    135
    Location
    The erros is still: "Can't find project or library"

  10. #10
    Moderator VBAX Wizard lucas's Avatar
    Joined
    Jun 2004
    Location
    Tulsa, Oklahoma
    Posts
    7,323
    Location
    are they all the same version of excel?
    Sounds like a missing reference.
    Check the vbe tools-references
    Steve
    "Nearly all men can stand adversity, but if you want to test a man's character, give him power."
    -Abraham Lincoln

  11. #11
    VBAX Contributor
    Joined
    Jun 2006
    Posts
    135
    Location
    Both are Excel XP Professional with Frontpage versions, and have the same vbe tools-references.

  12. #12
    Moderator VBAX Wizard lucas's Avatar
    Joined
    Jun 2004
    Location
    Tulsa, Oklahoma
    Posts
    7,323
    Location
    you have dimmed text and searchstring globally?
    Steve
    "Nearly all men can stand adversity, but if you want to test a man's character, give him power."
    -Abraham Lincoln

  13. #13
    Administrator
    VP-Knowledge Base
    VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    Why not just pass k to the sub

    [VBA]
    Public k As Integer
    Public Sub Main()
    k = 5
    Call Macro1(k)
    End Sub

    Public Sub Macro1(k As Integer)
    MsgBox k
    End Sub

    [/VBA]
    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'

  14. #14
    VBAX Contributor
    Joined
    Jun 2006
    Posts
    135
    Location
    No, but Text is declared in this Sub and SearchString is an argument of the Sub and is already used before

  15. #15
    VBAX Contributor
    Joined
    Jun 2006
    Posts
    135
    Location
    Up

    the Left Right and Mid method cause problems. What have I got to add to enable them. On all computers I have exactly the same references:

    Visual Basic For Applications
    Microsoft Excel 10.0 Object Library
    OLE Automation
    Microsoft Office 10.0 Object Library

    Do you know what library is needed to use this functions on strings?

  16. #16
    Administrator
    VP-Knowledge Base VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    Look for any references marked "Missing" and remove the check next to them.
    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'

  17. #17
    VBAX Mentor ALe's Avatar
    Joined
    Aug 2005
    Location
    Milan
    Posts
    383
    Location
    Once I had the same problem and I'm not sure about the reason.

    It occured to me for a project with a richtextbox whose library was missing in a pc. When I removed the richtextbox the error was still there. I solved it changing Mid in Mid$ and Left in Left$ and it worked but I don't know the reason.

  18. #18
    VBAX Mentor ALe's Avatar
    Joined
    Aug 2005
    Location
    Milan
    Posts
    383
    Location
    In conclusion I agree with mdmackillop. It must be related to some libraries that your project show as missing

  19. #19
    VBAX Contributor
    Joined
    Jun 2006
    Posts
    135
    Location
    That worked (Pdfdistiller was missing), I don't know how it was related since I didn't use it but now it's working. Thanks

  20. #20
    VBAX Mentor ALe's Avatar
    Joined
    Aug 2005
    Location
    Milan
    Posts
    383
    Location
    Jungix, mark this thread as solved

Posting Permissions

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