Consulting

Results 1 to 10 of 10

Thread: storing processed data

  1. #1

    storing processed data

    Hi

    assuming that we have below sub that generate a 2d array.

    sub test()
         dim arr(1 to10, 1 to 10)
    
         for i =1 to 10
              arr(i,1)=i: arr(i,2)=2*i
         next i
    
    end sub
    is it possible to store the arr() in the memory (something like data dump) and use it in other subs without calculating it again?

    thanks
    Last edited by NIMANIMA50; 09-13-2022 at 10:56 PM.

  2. #2
    make your array variable Public

    public arr(1 to 10, 1 to 10) as Long

    sub test()
    ...
    ...

  3. #3
    Knowledge Base Approver VBAX Wizard
    Joined
    Apr 2012
    Posts
    5,641
    Avoid public variables; use

    Dim arr(9,9)
    or

    Private arr(9,9)

  4. #4
    Quote Originally Posted by arnelgp View Post
    make your array variable Public

    public arr(1 to 10, 1 to 10) as Long

    sub test()
    ...
    ...

    Thank you it works.Here is what i did :

    Public arr(1 To 10, 1 To 10) As LongSub test()
         For i = 1 To 10
              arr(i, 1) = i: arr(i, 2) = 2 * i
         Next i
    End Sub

    I was able to read the values of Arr from other subs.

  5. #5
    Quote Originally Posted by snb View Post
    Avoid public variables; use

    Dim arr(9,9)
    or

    Private arr(9,9)
    i tried this :
    Private arr(1 To 10, 1 To 10) As Long
    Sub test()
         For i = 1 To 10
              arr(i, 1) = i: arr(i, 2) = 2 * i
         Next i
    End Sub
    but did not work, I can not read the values of Arr from other subs.
    may I know why you recommended to avoid using Public variables?

  6. #6
    Knowledge Base Approver VBAX Wizard
    Joined
    Apr 2012
    Posts
    5,641
    You can't use the same variable name in another Macromodule.

    Dim sn(9, 0)
    
    Sub M_snb()
      For j = 0 To 9
        sn(j, 0) = j
      Next
      
      M_snb_002
    End Sub
       
    Sub M_snb_002()
       MsgBox sn(4, 0)
    End Sub

  7. #7
    Quote Originally Posted by snb View Post
    You can't use the same variable name in another Macromodule.

    Dim sn(9, 0)
    
    Sub M_snb()
      For j = 0 To 9
        sn(j, 0) = j
      Next
      
      M_snb_002
    End Sub
       
    Sub M_snb_002()
       MsgBox sn(4, 0)
    End Sub
    in your method i can only use the array while the main module is running (in your example "sub M_snb()".
    i prefer @arnelgp method as it will give me the possibility of using the array even if the main module is done running.

  8. #8
    Quote Originally Posted by snb View Post
    You don't understand the structure of a VBA project.
    Please master VBA fundamentals first.
    i agree that i dont know many things about VBA.That is the reason Im posting my questions here so people with good understanding about VBA enlighten me.
    but your suggestions to my question ,as a person that understand structure of a VBA project, does not solve my problem.
    if you think i am not applying your method properly, please let me know how it should be done. Thanks

  9. #9
    Moderator VBAX Master georgiboy's Avatar
    Joined
    Mar 2008
    Location
    Kent, England
    Posts
    1,190
    Location
    Quote Originally Posted by snb View Post
    You don't understand the structure of a VBA project.
    Please master VBA fundamentals first.
    Your attitude towards people trying to learn VBA stinks

    Not sure if anything is lost in translation but it needs to be remembered that people come here to learn (as I did), your messages quite often come across as someone who is on a high horse.

    You could also argue that your explanations are sub standard.

    Moderators can delete my account if this is a problem, I have had enough of the clique here that support such people.
    Last edited by georgiboy; 09-15-2022 at 05:05 AM.
    Click here for a guide on how to add code tags
    Click here for a guide on how to mark a thread as solved

    Excel 365, Version 2403, Build 17425.20146

  10. #10
    Knowledge Base Approver VBAX Wizard
    Joined
    Apr 2012
    Posts
    5,641
    in your method i can only use the array while the main module is running (in your example "sub M_snb()".
    This is not a question, but a statement.
    That statement can only based on .....
    The best advice to give I gave you.

Posting Permissions

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