Results 1 to 28 of 28

Thread: Can't declare Public Constant in a macro in Excel?

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Jan 2006
    Posts
    20

    Can't declare Public Constant in a macro in Excel?

    Ok...I have a macro I wrote which calls on other subs. I wanted to declare some global constants so I could change the variable easily if I had to change it later on. So this is what I have:

    Sub MyMacro()

    Public Const MATRIX As String = "Tab 1"
    Public Const MATRIX2 As String = "Tab 2"

    Call OtherSub
    Call OtherSub2

    End Sub

    So basically I want to pass the constants MATRIX and MATRIX2 to the other subs. But everytime I try to run the debugger, I get the error:

    Compile error:
    Invalid attribute in Sub or Function

    I am using Microsoft Excel 2003 SP1 and VB 6.3 Version 9972. Any ideas or tips would be greatly appreciated!

  2. #2
    Frenzied Member DKenny's Avatar
    Join Date
    Sep 2005
    Location
    on the good ship oblivion..
    Posts
    1,171

    Re: Can't declare Public Constant in a macro in Excel?

    Move your constant declaration outside the procedure. Put them at the top of the module.
    Declan

    Don't forget to mark your Thread as resolved.
    Take a moment to rate posts that you think are helpful

  3. #3

    Thread Starter
    Junior Member
    Join Date
    Jan 2006
    Posts
    20

    Re: Can't declare Public Constant in a macro in Excel?

    okay, so now I have it like this:

    Public Const MATRIX As String = "Tab 1"
    Public Const MATRIX2 As String = "Tab 2"

    Sub MyMacro()

    Call OtherSub
    Call OtherSub2

    End Sub

    And it worked the first time I ran it (actually, with different combinations and movement of code, it will work for the first time and then won't work the next time...) but then when I call OtherSub and that sub calls for the Const MATRIX, I get the error:

    Compile error:
    Ambigous name detected: MATRIX

    But then again, if I just call one of the constants in the first Sub MyMacro(), it will work.

    Very stumped...
    Last edited by Sacto95827; Feb 17th, 2006 at 12:30 AM.

  4. #4
    Frenzied Member DKenny's Avatar
    Join Date
    Sep 2005
    Location
    on the good ship oblivion..
    Posts
    1,171

    Re: Can't declare Public Constant in a macro in Excel?

    Have you another declaration of MATRIX in OtherSub?
    Declan

    Don't forget to mark your Thread as resolved.
    Take a moment to rate posts that you think are helpful

  5. #5

    Thread Starter
    Junior Member
    Join Date
    Jan 2006
    Posts
    20

    Re: Can't declare Public Constant in a macro in Excel?

    No I do not.

  6. #6
    Addicted Member
    Join Date
    Jan 2006
    Location
    Montreal, Canada
    Posts
    152

    Re: Can't declare Public Constant in a macro in Excel?

    then we will need the code of othersub and othersub2 to find where this error is coming from.

  7. #7

    Thread Starter
    Junior Member
    Join Date
    Jan 2006
    Posts
    20

    Re: Can't declare Public Constant in a macro in Excel?

    Haha...I think I figured it out...can you pass variables between modules?

  8. #8
    Frenzied Member DKenny's Avatar
    Join Date
    Sep 2005
    Location
    on the good ship oblivion..
    Posts
    1,171

    Re: Can't declare Public Constant in a macro in Excel?

    Yes you can. I still think you should post your code.
    Declan

    Don't forget to mark your Thread as resolved.
    Take a moment to rate posts that you think are helpful

  9. #9

    Thread Starter
    Junior Member
    Join Date
    Jan 2006
    Posts
    20

    Re: Can't declare Public Constant in a macro in Excel?

    Okay...so here's my code:

    MODULE 1:
    VB Code:
    1. Public Const MATRIX As String = "Tab 1"
    2. Public Const MATRIX2 As String = "Tab 2"
    3.  
    4. Sub MyMacro()
    5.  
    6. Sheets(MATRIX).Select
    7. Call OtherSub
    8. Call OtherSub2
    9.  
    10. End Sub

    MODULE 2:
    VB Code:
    1. Sub OtherSub()
    2.  
    3. Sheets(MATRIX2).Select
    4.  
    5. End Sub

    So basically, when I'm just trying to bring up another sheet in a workbook. The first Constant works fine in Module 1, but when I call the OtherSub() in Module 2, I get the ambigious error. But if I move the OtherSub() from Module 2 to Module 1, it works. But I would much rather have my subs in another module...looks a lot cleaner.

  10. #10
    Frenzied Member DKenny's Avatar
    Join Date
    Sep 2005
    Location
    on the good ship oblivion..
    Posts
    1,171

    Re: Can't declare Public Constant in a macro in Excel?

    Is this your complete code for both modules?
    Declan

    Don't forget to mark your Thread as resolved.
    Take a moment to rate posts that you think are helpful

  11. #11

    Thread Starter
    Junior Member
    Join Date
    Jan 2006
    Posts
    20

    Re: Can't declare Public Constant in a macro in Excel?

    No...it's not my complete code. But I don't think it makes a difference because if I comment out all of my other code in Module 2 and just leave that one select statement...I get the error...right when the code switches to that sub...

  12. #12
    Frenzied Member DKenny's Avatar
    Join Date
    Sep 2005
    Location
    on the good ship oblivion..
    Posts
    1,171

    Re: Can't declare Public Constant in a macro in Excel?

    Well, I copied this code and don't get an error, so there has to be something in the rest of you code that is causing the issue.
    Declan

    Don't forget to mark your Thread as resolved.
    Take a moment to rate posts that you think are helpful

  13. #13
    Addicted Member
    Join Date
    Jan 2006
    Location
    Montreal, Canada
    Posts
    152

    Re: Can't declare Public Constant in a macro in Excel?

    try this

    VB Code:
    1. Sheets([MATRIX]).Select
    2. Sheets([MATRIX2]).Select


    give me some news, because me too I Copied & Pasted your code and everything went fine
    Last edited by billhuard; Feb 17th, 2006 at 01:22 PM.

  14. #14
    Frenzied Member
    Join Date
    May 2004
    Location
    Carlisle, PA
    Posts
    1,045

    Re: Can't declare Public Constant in a macro in Excel?

    Something doesn't make sense here ... your code runs fine on my machine.

    How is VB6 involved in this operation?

    You state:
    Compile error:
    Ambigous name detected: MATRIX
    but your code says:
    Code:
    Sheets(MATRIX2).Select
    Where is the complier finding "MATRIX" (as opposed to "MATRIX2"). You don't actually have a SPACE, as in "Matrix 2", do you?
    Blessings in abundance,
    All the Best,
    & ENJOY!

    Art . . . . Carlisle, PA . . USA

  15. #15

    Thread Starter
    Junior Member
    Join Date
    Jan 2006
    Posts
    20

    Re: Can't declare Public Constant in a macro in Excel?

    I think I might of ran into one of my problems. So when I copied and pasted all of my code from one module to the main module to make it work, I erased the old module. So now I just added a module and pasted the subs into the module and my main sub can't reference the other subs. Do I need to do something to have the modules reference each other? I think this might have been my initial problem all this time.

  16. #16

    Thread Starter
    Junior Member
    Join Date
    Jan 2006
    Posts
    20

    Re: Can't declare Public Constant in a macro in Excel?

    Quote Originally Posted by Webtest
    Something doesn't make sense here ... your code runs fine on my machine.

    How is VB6 involved in this operation?

    You state: but your code says:
    Code:
    Sheets(MATRIX2).Select
    Where is the complier finding "MATRIX" (as opposed to "MATRIX2"). You don't actually have a SPACE, as in "Matrix 2", do you?
    sorry...should be MATRIX2

  17. #17
    Addicted Member
    Join Date
    Jan 2006
    Location
    Montreal, Canada
    Posts
    152

    Re: Can't declare Public Constant in a macro in Excel?

    you don't have to do anything to add reference between module,
    Only by compiling you're project it will.

    Do you have a sheet named MATRIX2????

  18. #18
    Frenzied Member
    Join Date
    May 2004
    Location
    Carlisle, PA
    Posts
    1,045

    Re: Can't declare Public Constant in a macro in Excel?

    It should take you less than 5 minutes to prove the following ... open a clean workbook, Menu Bar: Insert > Worksheet. This adds "Sheet2" to the workbook. Record a dummy macro and then edit it with the following (in Module 1):
    Code:
    Option Explicit
    Public Const MATRIX As String = "Sheet2"
    Sub Macro1()
        Call junk
    End Sub
    In the VBA Project View right-click on "Modules" and select Insert > Module. In this new Module2, insert the following code:
    Code:
    Option Explicit
    Sub junk()
        Sheets(MATRIX).Select
    End Sub
    Now when you select Sheet1 and then run Macro1, Sheet2 will be selected. Does this work for you?
    Blessings in abundance,
    All the Best,
    & ENJOY!

    Art . . . . Carlisle, PA . . USA

  19. #19
    Frenzied Member
    Join Date
    May 2004
    Location
    Carlisle, PA
    Posts
    1,045

    Re: Can't declare Public Constant in a macro in Excel?

    I'll bet you have something loaded in your References that has a keyword "Matrix" in its namespace.
    Blessings in abundance,
    All the Best,
    & ENJOY!

    Art . . . . Carlisle, PA . . USA

  20. #20
    Frenzied Member DKenny's Avatar
    Join Date
    Sep 2005
    Location
    on the good ship oblivion..
    Posts
    1,171

    Re: Can't declare Public Constant in a macro in Excel?

    Quote Originally Posted by Webtest
    I'll bet you have something loaded in your References that has a keyword "Matrix" in its namespace.
    Nice idae, but he stated that the constant worked in one sub, but not in another. If it was a keyword issue it wouldn't work at all.
    5 will get you 10 that its double declared.
    Declan

    Don't forget to mark your Thread as resolved.
    Take a moment to rate posts that you think are helpful

  21. #21

    Thread Starter
    Junior Member
    Join Date
    Jan 2006
    Posts
    20

    Re: Can't declare Public Constant in a macro in Excel?

    I don't think it's double declared...I think it's something else. Check it out. So I initially had two modules. Module 1 had the macro I wanted to run. So it basically had:

    MODULE 1:

    VB Code:
    1. Public Const MATRIX As String = "Tab 1"
    2. Public Const MATRIX2 As String = "Tab 2"
    3.  
    4. Sub MyMacro()
    5.  
    6. Sheets(MATRIX).Select
    7. Call OtherSub
    8. Call OtherSub2
    9.  
    10. End Sub
    Module 2 had all of my other subs...
    MODULE 2:
    VB Code:
    1. Sub OtherSub()
    2.  
    3. Sheets(MATRIX2).Select
    4.  
    5. End Sub
    Running from top-to-bottom, when Module 1 would call Sub OtherSub() and the pointer got to Module 2 Sub OtherSub(), I would right away get the error:

    Compile error:
    Ambigous name detected: MATRIX2

    So I basically copied all of my code from Module 2 to Module 1 making it look like this:

    MODULE 1:
    VB Code:
    1. Public Const MATRIX As String = "Tab 1"
    2. Public Const MATRIX2 As String = "Tab 2"
    3.  
    4. Sub MyMacro()
    5.  
    6. Sheets(MATRIX).Select
    7. Call OtherSub
    8. Call OtherSub2
    9.  
    10. End Sub
    11.  
    12. Sub OtherSub()
    13.  
    14. Sheets(MATRIX2).Select
    15.  
    16. End Sub
    I then deleted Module 2 and this worked...but it's not really pretty or the best coding practice.

    So billhuard suggested to try:
    VB Code:
    1. Sheets([MATRIX2])Select
    I thought all I would have to do is insert another module, cut the code from Module 1 to the new Module and viola. But now I am getting:

    Compile error:
    Ambigous name detected: OtherSub

    So for some reason my modules aren't associating with each other. And because of this, I think that this might of been my initial problem. Is this a stupid error that can be solved easily...I bet it is...

  22. #22
    Fanatic Member Comintern's Avatar
    Join Date
    Nov 2004
    Location
    Lincoln, NE
    Posts
    826

    Re: Can't declare Public Constant in a macro in Excel?

    Quote Originally Posted by Sacto95827
    So for some reason my modules aren't associating with each other. And because of this, I think that this might of been my initial problem. Is this a stupid error that can be solved easily...I bet it is...
    Do you have Option Explicit on? If not, turn it on. Then, do a global search for MATRIX to make sure it isn't double declared. Also, make sure you don't have the constants declared in more than one place. If they are, it will work fine from any module it is declared in, as it defaults to it's own namespace. You can duplicate this behaviour by doing the following: Create 3 modules. In the first one put this code:
    VB Code:
    1. Private Sub Verify()
    2.  
    3.     Debug.Print TEST_IT
    4.  
    5. End Sub
    In the second one put this code:
    VB Code:
    1. Public Const TEST_IT As String = "Module2"
    2.  
    3. Private Sub Verify()
    4.  
    5.     Debug.Print TEST_IT
    6.  
    7. End Sub
    And in the third, put this code:
    VB Code:
    1. Public Const TEST_IT As String = "Module3"
    2.  
    3. Private Sub Verify()
    4.  
    5.     Debug.Print TEST_IT
    6.  
    7. End Sub
    The Verify sub will run fine in any module with the Const declaration in it (modules 2 and 3), but will give your error in module 1. After you have done that, if you are still having problems, you can explicitly reference namespaces of modules by using syntax like this:
    VB Code:
    1. Sub OtherSub()
    2.  
    3. Sheets(Module1.MATRIX2).Select
    4.  
    5. End Sub
    [EDIT] BTW, same thing applies to your Subs. I would also get into the habit of explicitly declaring them Public or Private. By default, they are Public.

  23. #23

    Thread Starter
    Junior Member
    Join Date
    Jan 2006
    Posts
    20

    Re: Can't declare Public Constant in a macro in Excel?

    I tried everything and I'm still stuck. Here's my code pretty much in it's entirety:

    Module 1:
    VB Code:
    1. Option Explicit
    2.  
    3. Public Const MATRIX As String = "SHEET 1"
    4. Public Const PIVOTS As String = "PIVOTS"
    5. Public Const MATRIX2 As String = "SHEET 2"
    6.  
    7. Sub RefreshLOR1()
    8.     Sheets("Raw").Select
    9.     ActiveWorkbook.RefreshAll
    10.     Sheets(MATRIX).Select
    11.     Range("A1").Select
    12.     Call ClearScreen
    13.     Call GetNames
    14.     Range("B1").Select
    15.    
    16. End Sub

    Module 2:
    VB Code:
    1. Option Explicit
    2.  
    3. Sub ClearScreen()
    4.     Range("C6:C30").Select
    5.     Application.CutCopyMode = False
    6.     Selection.ClearContents
    7.     Range("P6:Q30").Select
    8.     Selection.ClearContents
    9.     Range("P34:P85").Select
    10.     Selection.Cut
    11.     Range("Q34").Select
    12.     ActiveSheet.Paste
    13. End Sub
    14.  
    15. Sub GetNames()
    16.     Range("P34").Select
    17.     ChDir "Y:\Directory\Pro\Indicators"
    18.     Workbooks.Open Filename:= _
    19.         "Y:\Directory\Pro\Latest Matrix.xls"
    20.     Sheets(MATRIX2).Select
    21.     Call GetSection
    22. Do While IsEmpty(ActiveCell.Offset(1, 1)) = False
    23.     Sheets(MATRIX2).Select
    24.     Selection.Copy
    25.     ActiveCell.Offset(1, 0).Select
    26.     Windows("Schedule_Audit_Template.xls").Activate
    27.     ActiveSheet.Paste
    28.     ActiveCell.Offset(1, 0).Select
    29.     Windows("Latest Matrix.xls").Activate
    30. Loop
    31.     Windows("Schedule_Audit_Template.xls").Activate
    32.     Range("P36:P85").Select
    33.     Selection.Sort Key1:=Range("P36"), Order1:=xlAscending, Header:=xlGuess, _
    34.         OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom, _
    35.         DataOption1:=xlSortNormal
    36. End Sub
    37.  
    38. Sub GetSection()
    39.     Dim str As String
    40.     Dim fcell As Range
    41.     Dim r As Range
    42.  
    43.     Set r = Worksheets(MATRIX2).Range("A1:A300")
    44.     Range("A1").Select
    45.     Do While fcell Is Nothing
    46.     str = InputBox("Pick Section")
    47.     Set fcell = r.Find(what:="Section-Done-" + str, _
    48.     After:=r.Cells(r.Cells.Count), _
    49.     LookIn:=xlValues, LookAt:=xlPart, _
    50.     SearchOrder:=xlByRows, SearchDirection:=xlNext, _
    51.     MatchCase:=False)
    52.     If fcell Is Nothing Then
    53.         MsgBox "Section Not Found"
    54.     End If
    55.     Loop
    56.     If Len(str) = 0 Then
    57.         Range("U1").Select
    58.     ElseIf StrPtr(str) = 0 Then
    59.         Range("U1").Select
    60.     Else
    61.         fcell.Activate
    62.     End If
    63. End Sub

    Like I said, when I'm in Module 1, the first constant MATRIX works fine. Then it calls the ClearScreen sub and it works fine. Then when I go into the GetNames sub, I get the error:

    Compile error:

    Ambigious name detected: MATRIX2

    It's like the constant doesn't get passed between the modules because if I just copy and paste all of my code from Module 2 to Module 1, it works. Man I'm stuck.

  24. #24
    Fanatic Member Comintern's Avatar
    Join Date
    Nov 2004
    Location
    Lincoln, NE
    Posts
    826

    Re: Can't declare Public Constant in a macro in Excel?

    Your problem has nothing to do with the constants. It is here:
    VB Code:
    1. Sub GetNames()
    2.     Range("P34").Select
    3.     ChDir "Y:\Directory\Pro\Indicators"
    4.     Workbooks.Open Filename:= _
    5.         "Y:\Directory\Pro\Latest Matrix.xls"
    6.     Sheets(MATRIX2).Select        'Error on this line.
    You have more than one sheet open with the same name, so the worksheet is the ambiguous name, not the constant. You need to find some way to directly reference it's object without an implicit activate. I'd do it like this:
    VB Code:
    1. Sub GetNames()
    2.    
    3.     Dim oBook As Workbook
    4.    
    5.     Range("P34").Select
    6.     ChDir "Y:\Directory\Pro\Indicators"
    7.     Set oBook = Application.Workbooks.Open("Y:\Directory\Pro\Latest Matrix.xls")
    8.     oBook.Sheets(MATRIX2).Select
    9.     Call GetSection
    10.     '....
    It's generally better to always keep specific references to the objects you are working with in VBA (it also shows why you should post all your code). This is a perfect example.

  25. #25

    Thread Starter
    Junior Member
    Join Date
    Jan 2006
    Posts
    20

    Re: Can't declare Public Constant in a macro in Excel?

    Quote Originally Posted by Comintern
    You have more than one sheet open with the same name, so the worksheet is the ambiguous name, not the constant.
    What are you refering to as the same name? The sheets all have different sheet names:
    SHEET 1
    SHEET 2
    PIVOTS

    Quote Originally Posted by Comintern
    You need to find some way to directly reference it's object without an implicit activate. I'd do it like this:
    VB Code:
    1. Sub GetNames()
    2.    
    3.     Dim oBook As Workbook
    4.    
    5.     Range("P34").Select
    6.     ChDir "Y:\Directory\Pro\Indicators"
    7.     Set oBook = Application.Workbooks.Open("Y:\Directory\Pro\Latest Matrix.xls")
    8.     oBook.Sheets(MATRIX2).Select
    9.     Call GetSection
    10.     '....
    It's generally better to always keep specific references to the objects you are working with in VBA (it also shows why you should post all your code). This is a perfect example.
    So I think I see what you are saying...declare an object that is specific to that sub?

    But then I did try your suggestion, but to no avail...

  26. #26
    Frenzied Member DKenny's Avatar
    Join Date
    Sep 2005
    Location
    on the good ship oblivion..
    Posts
    1,171

    Re: Can't declare Public Constant in a macro in Excel?

    Sacto
    Comintern is saying that you may have multiple workbooks open and that there may be a sheet called "SHEET 2" in more than one of those workbooks, hence the Ambigious name detected - VBA doesn't know which "sheet 2" you are referring to.

    Try changing your GetSection procedure, such that it require the name of the workbook to be passed to it.
    Now when setting the range 'r', you can qualify the workbook as well as the sheet name.
    VB Code:
    1. Sub GetSection(ByVal BookName As String)
    2.     Dim str As String
    3.     Dim fcell As Range
    4.     Dim r As Range
    5.  
    6.     Set r = Workbooks(BookName).Worksheets(MATRIX2).Range("A1:A300")
    7.     Range("A1").Select
    8.     Do While fcell Is Nothing
    9.     str = InputBox("Pick Section")
    10.     Set fcell = r.Find(what:="Section-Done-" + str, _
    11.     After:=r.Cells(r.Cells.Count), _
    12.     LookIn:=xlValues, LookAt:=xlPart, _
    13.     SearchOrder:=xlByRows, SearchDirection:=xlNext, _
    14.     MatchCase:=False)
    15.     If fcell Is Nothing Then
    16.         MsgBox "Section Not Found"
    17.     End If
    18.     Loop
    19.     If Len(str) = 0 Then
    20.         Range("U1").Select
    21.     ElseIf StrPtr(str) = 0 Then
    22.         Range("U1").Select
    23.     Else
    24.         fcell.Activate
    25.     End If
    26. End Sub


    Now when calling it from GetNames, you can pass it the name of the workbook in question.

    VB Code:
    1. Sub GetNames()
    2.    
    3.     Dim oBook As Workbook
    4.    
    5.     Range("P34").Select
    6.     ChDir "Y:\Directory\Pro\Indicators"
    7.     Set oBook = Application.Workbooks.Open("Y:\Directory\Pro\Latest Matrix.xls")
    8.     oBook.Sheets(MATRIX2).Select
    9.     GetSection oBook.Name
    10.     '......
    Declan

    Don't forget to mark your Thread as resolved.
    Take a moment to rate posts that you think are helpful

  27. #27

    Thread Starter
    Junior Member
    Join Date
    Jan 2006
    Posts
    20

    Re: Can't declare Public Constant in a macro in Excel?

    Quote Originally Posted by DKenny
    Sacto
    Comintern is saying that you may have multiple workbooks open and that there may be a sheet called "SHEET 2" in more than one of those workbooks, hence the Ambigious name detected - VBA doesn't know which "sheet 2" you are referring to.
    I don't think that can be the case because I made sure that both the workbooks that I have open have different sheet/tab names. I just used SHEET 2 as a generic name since I'm at work

    But I am going to try your suggestion by passing the workbook, also.

  28. #28
    Frenzied Member
    Join Date
    May 2004
    Location
    Carlisle, PA
    Posts
    1,045

    Re: Can't declare Public Constant in a macro in Excel?

    I know a lot of people HATE globals, but if I am working back and forth with a lot of manipulations on the same 2 or 3 sheets I do the following (for 2 sheets):
    Code:
    Option Explicit
    
    'Define the Global Source Sheet
    Public G_srcSheet As Worksheet
    'Define the Global Destination Sheet
    Public G_dstSheet As Worksheet
    
    Sub Macro1()
    
    'Sheet Handle Initialization
    Set G_srcSheet = Workbooks("mySourceWorkbookName").Sheets("mySourceSheetName")
    Set G_dstSheet = Workbooks("myDestWorkbookName").Sheets("myDestSheetName")
    
    'Go on your merry way using just G_srcSheet and G_dstSheet anywhere in your code!!!
    'They will never get confused.
    
    End Sub
    You can also initialize the Sheet Handle right after you open a workbook if that doesn't happen until later in the code. It is handy to know that opening a workbook immediately forces that workbook to be the ActiveWorkbook.
    Last edited by Webtest; Feb 23rd, 2006 at 04:05 PM.
    Blessings in abundance,
    All the Best,
    & ENJOY!

    Art . . . . Carlisle, PA . . USA

Posting Permissions

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



Click Here to Expand Forum to Full Width