![]() |
Creating matrices and subsequently prosess those in a loop
Hello FellowDevelopers,
I want to define a set of arrays with the same structure but with the diferent contents. The content of each array has to be processed. The arrays vary in count, sometimes 2, sometimes it's 6. So I want to repeat processing all arrays in a loop. By naming all arrays subsequently (Arr1, Arr2 etc) with a counter I can see what array I have to process. I wrote this code with two subroutines. The problem is in the second part, (CopyOne), where I cannot use the name of the array. Any ideas? Rob Option Base 1 Public Arr1 As Variant Public Arr2 As Variant Sub CopyAll() ' Needed counters Dim intArrNr As Integer Dim intArrCount As Integer intArrCount = 2 ' Here I declare two arrays and with each array the same things have to be copied. Arr1 = Array("A", "B") Arr2 = Array("C", "D") ' Do somthing with each of the arrays For intArrNr = 1 To intArrAant CopyOne (intArrNr) Next End Sub Sub CopyOne(Nr) Dim strArrName strArrName = "Arr" & Nr ' Now, normally if I want to do something with a part of the array I do this: ' msgbox Arr1(1) and it says A ' msgbox Arr2(2) and it says D ' But now I want to do Msgbox strArrName(1) ' Tht is: take the value IN strArrName, ' which is "Arr1", and use it as the name of the array to show the first element ' How can I solve this? End Sub |
Creating matrices and subsequently prosess those in a loop
In cases like this, I fall back on the old timey practice of developing a
flow chart to see if I am really doing what I want to do. I can also figure out what my alternatives are and usually, although not always, come up with the optimal solution. You could declare your arrays again, or you could incorporate that procedure into the other one. There are probably several other approaches that would become evident if the objectives were put in flow chart form. "Rob" wrote: Hello FellowDevelopers, I want to define a set of arrays with the same structure but with the diferent contents. The content of each array has to be processed. The arrays vary in count, sometimes 2, sometimes it's 6. So I want to repeat processing all arrays in a loop. By naming all arrays subsequently (Arr1, Arr2 etc) with a counter I can see what array I have to process. I wrote this code with two subroutines. The problem is in the second part, (CopyOne), where I cannot use the name of the array. Any ideas? Rob Option Base 1 Public Arr1 As Variant Public Arr2 As Variant Sub CopyAll() ' Needed counters Dim intArrNr As Integer Dim intArrCount As Integer intArrCount = 2 ' Here I declare two arrays and with each array the same things have to be copied. Arr1 = Array("A", "B") Arr2 = Array("C", "D") ' Do somthing with each of the arrays For intArrNr = 1 To intArrAant CopyOne (intArrNr) Next End Sub Sub CopyOne(Nr) Dim strArrName strArrName = "Arr" & Nr ' Now, normally if I want to do something with a part of the array I do this: ' msgbox Arr1(1) and it says A ' msgbox Arr2(2) and it says D ' But now I want to do Msgbox strArrName(1) ' Tht is: take the value IN strArrName, ' which is "Arr1", and use it as the name of the array to show the first element ' How can I solve this? End Sub |
Creating matrices and subsequently process those in a loop
Hello,
it isn't that complicated that I have to put it in a flowchart. It works if I address the arrays individually. The question is how I can get the value in the variable to work as a NAME for the array. Ok, even more easy code Sub WorkWithArray() Dim Arr1 As Variant Dim Arr2 As Variant Dim strArrName Dim strArrNr Dim intArrCount ' Here I declare two arrays and with each array I have to do something Arr1 = Array("A", "B") Arr2 = Array("C", "D") ' Fill counters strArrNr = 1 intArrCount = 2 strArrName = "Arr" & Nr ' Nr = 1, so strArrName = "Arr1" ' Repeat doing something with each of the arrays For intArrNr = 1 To intArrCount Msgbox strArrName(1) ' But THIS doesn't work. How do I get the CONTENT in the variable work as the Array-designation - Arr1(1) - ? strArrNr = strArrNr + 1 strArrName = "Arr" & Nr Next End sub Rob "JLGWhiz" schreef in bericht ... In cases like this, I fall back on the old timey practice of developing a flow chart to see if I am really doing what I want to do. I can also figure out what my alternatives are and usually, although not always, come up with the optimal solution. You could declare your arrays again, or you could incorporate that procedure into the other one. There are probably several other approaches that would become evident if the objectives were put in flow chart form. "Rob" wrote: Hello FellowDevelopers, I want to define a set of arrays with the same structure but with the diferent contents. The content of each array has to be processed. The arrays vary in count, sometimes 2, sometimes it's 6. So I want to repeat processing all arrays in a loop. By naming all arrays subsequently (Arr1, Arr2 etc) with a counter I can see what array I have to process. I wrote this code with two subroutines. The problem is in the second part, (CopyOne), where I cannot use the name of the array. Any ideas? Rob Option Base 1 Public Arr1 As Variant Public Arr2 As Variant Sub CopyAll() ' Needed counters Dim intArrNr As Integer Dim intArrCount As Integer intArrCount = 2 ' Here I declare two arrays and with each array the same things have to be copied. Arr1 = Array("A", "B") Arr2 = Array("C", "D") End Sub Sub CopyOne(Nr) Dim strArrName strArrName = "Arr" & Nr ' Now, normally if I want to do something with a part of the array I do this: ' msgbox Arr1(1) and it says A ' msgbox Arr2(2) and it says D ' But now I want to do Msgbox strArrName(1) ' Tht is: take the value IN strArrName, ' which is "Arr1", and use it as the name of the array to show the first element ' How can I solve this? End Sub |
All times are GMT +1. The time now is 10:56 PM. |
Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com