Results 1 to 12 of 12

Thread: Scope and visibility in vba

  1. #1

    Thread Starter
    Member
    Join Date
    Oct 2010
    Location
    Bristol
    Posts
    36

    Scope and visibility in vba

    I am having trouble with a Sub not seeing arrays which are in the Declarations section of the same module. I have used Dim to specify these arrays and I understand that this makes them Private as a default BUT they should still be seen by the Subs in that Module. The strange thing is that several other Subs see the arrays OK. I judge that they are (or are not) seen by whether, if I deliberately spell them wrongly the vba corrects the spelling. Another way I check is to right click on the arrays and choose Definition. Sometimes this highlights the array in declarations and sometimes not. Is it possible that I have too many arrays - if so should there not be a warning? I did notice that replacing the arrays which were originally in one line by individual Dims. seemed to ease the situation but not cure it. I did try replacing Dim by Public but this made no difference. I cannot seem to find the limitations of vba; ie total no. of arrays, total cumulative size of arrays, etc. by clicking on help.
    I have found a "work around" but I am very uncomfortable having a problem that I don't understand. Although I have produced a very cut-down .bas file, I haven't got a clue how to put it on here! I assume that a .bas file does not go against the rules? I would be very grateful for any help you can give.
    Last edited by maurice_whittaker; Oct 29th, 2010 at 10:33 AM.

  2. #2
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: Scope and visibility in vba

    Moved From The CodeBank (which is for sharing code rather than asking questions )

  3. #3
    Superbly Moderated NeedSomeAnswers's Avatar
    Join Date
    Jun 2002
    Location
    Manchester uk
    Posts
    2,660

    Re: Scope and visibility in vba

    can you post your code ?

    and i dont mean attached the .bas file but literaly copy and paste your code inside [code] tags in a post window ?

    Also point out (maybe with comments) which arrays do not seem to be working correctly.
    Please Mark your Thread "Resolved", if the query is solved & Rate those who have helped you



  4. #4
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: Scope and visibility in vba

    Although I have produced a very cut-down .bas file, I haven't got a clue how to put it on here! I assume that a .bas file does not go against the rules? I would be very grateful for any help you can give.
    manage attachments in advanced editor, zip any files types that are not in the list, like xls

    there are limitations on how many modules and how many lines of code you can have in a workbook
    as far as number of arrays i believe that would be depending on amount of memory etc, and content of arrays at any given time

    imho module level variables should be used sparingly, as far as possible variables should be declared at procedure level, and where required passed to other procedures from the caller
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  5. #5

    Thread Starter
    Member
    Join Date
    Oct 2010
    Location
    Bristol
    Posts
    36

    Re: Scope and visibility in vba

    [QUOTE
    and i dont mean attached the .bas file but literaly copy and paste your code inside [code] tags in a post window ?.[/QUOTE]

    Thanks very much for this, very new to internet forums and am sorry but don't understand what you mean by code tags. Could you please explain?

  6. #6
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: Scope and visibility in vba

    Quote Originally Posted by maurice_whittaker View Post
    very new to internet forums and am sorry but don't understand what you mean by code tags. Could you please explain?
    He means if you are going to post code, use [code]your code goes here[/code] tags. They make is to much easier to read what has been submitted.

    Example:

    With [code]your code goes here[/code] tags
    Code:
    Private Sub Command1_Click()
    'Remove Duplicates
    Dim i As Long
    Dim j As Long
        With List1
            For i = 0 To .ListCount - 1
                For j = .ListCount To (i + 1) Step -1
                    If .List(j) = .List(i) Then
                        .RemoveItem j
                    End If
                Next
            Next
        End With
    
    End Sub
    The exact same code without the use of code tags

    Private Sub Command1_Click()
    'Remove Duplicates
    Dim i As Long
    Dim j As Long
    With List1
    For i = 0 To .ListCount - 1
    For j = .ListCount To (i + 1) Step -1
    If .List(j) = .List(i) Then
    .RemoveItem j
    End If
    Next
    Next
    End With

    End Sub

  7. #7

    Thread Starter
    Member
    Join Date
    Oct 2010
    Location
    Bristol
    Posts
    36

    Re: Scope and visibility in vba

    Thank you NeedSomeAnswers, Westconn1 and Hack - I have learned quite a lot already. I'm afraid the problem was all my fault because I was dealing with arrays which had lots of zeros in them. The Sub which failed had the zeroes and the ones that worked had numbers because they were using the arrays in reverse. I jumped to the conclusion that It was a Scope problem. My daughter was helping me and I said to her 'I don't even know what "imho module level variables" are'. She explained very gently to me what imho meant! Thanks again everyone, no doubt I will be back again!

  8. #8

    Thread Starter
    Member
    Join Date
    Oct 2010
    Location
    Bristol
    Posts
    36

    Re: Scope and visibility in vba

    Re: westconn1 reply, is it possible to call a Sub with Arrays as arguments? I have not yet found a way to do this unless one has to use paramarrays somehow? Am I right that this would make it impossible to use more than one array in the argument list because the paramarray has to be the last one? I am anxious not to slow the code down much and I am not sure what effect this would have. In order to stop me asking "simple" questions, is there a way in VBA Help to ask "compound" questions like "Using arrays in argument lists?"

  9. #9
    Superbly Moderated NeedSomeAnswers's Avatar
    Join Date
    Jun 2002
    Location
    Manchester uk
    Posts
    2,660

    Re: Scope and visibility in vba

    Yes you can, like this !

    vb Code:
    1. Private Sub Test (strData() As String)
    2.  
    3. End Sub
    Please Mark your Thread "Resolved", if the query is solved & Rate those who have helped you



  10. #10

    Thread Starter
    Member
    Join Date
    Oct 2010
    Location
    Bristol
    Posts
    36

    Re: Scope and visibility in vba

    Thank you, I will give that a try. I assume that only string arrays can be passed, and if I want to do any others I would have to convert to string.

  11. #11
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: Scope and visibility in vba

    any type of array can be passed
    the type passed must be the type expected by the procedure
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  12. #12

    Thread Starter
    Member
    Join Date
    Oct 2010
    Location
    Bristol
    Posts
    36

    Re: Scope and visibility in vba

    Thanks westconn1, it works very well. I originally thought that I would have to pass the size of the array as well but the system knows this. I accidentlally put integers and real numbers in the trial (without the "" marks) and to my surprise it worked, automatically converting the values. Lack of this knowledge was the cause of my plethora of Module-wide arrays. I have to find a way to mark this thread resolved but I have forgotten how to do it! Dumbo! Thanks again.

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