fill array with 5 items for each occurrence
-
- PlatinumLounger
- Posts: 4362
- Joined: 26 Apr 2010, 17:36
fill array with 5 items for each occurrence
I have five var one of ths is ID_cust.
The other for var contain a notice of Customer: TIPO, AB , CAB, NR.
Actually i use:
...
Dim CUSTOMER() As Variant
dim x as long
...
X = X + 1
ReDim Preserve CUSTOMER(X)
CUSTOMER(X) = ID_cust
i need to add to the arry the oterh for var
...
How to?
Note:
I get info from a loop file txt with the tipical free file open ="c:\..." and loop all lines ecc...
I get info from a string single line. One line contain all info for customer.
in this csast ID_cust is just filled
i get the info other 4 var with:
TIPO = Mid$(sNextLine, 7, 2)
ABI = Mid$(sNextLine, 15, 5)
CAB = Mid$(sNextLine, 26, 5)
NR = Format$(Mid$(sNextLine, 41, 12))
In effect for each line i need for one item of array:
array(ID_cust,TIPO,ABI....eccc)
The other for var contain a notice of Customer: TIPO, AB , CAB, NR.
Actually i use:
...
Dim CUSTOMER() As Variant
dim x as long
...
X = X + 1
ReDim Preserve CUSTOMER(X)
CUSTOMER(X) = ID_cust
i need to add to the arry the oterh for var
...
How to?
Note:
I get info from a loop file txt with the tipical free file open ="c:\..." and loop all lines ecc...
I get info from a string single line. One line contain all info for customer.
in this csast ID_cust is just filled
i get the info other 4 var with:
TIPO = Mid$(sNextLine, 7, 2)
ABI = Mid$(sNextLine, 15, 5)
CAB = Mid$(sNextLine, 26, 5)
NR = Format$(Mid$(sNextLine, 41, 12))
In effect for each line i need for one item of array:
array(ID_cust,TIPO,ABI....eccc)
-
- Administrator
- Posts: 78536
- Joined: 16 Jan 2010, 00:14
- Status: Microsoft MVP
- Location: Wageningen, The Netherlands
Re: fill array with 5 items for each occurrence
You could use
Dim Customer(1 To 5)
Customer(1) = ID_Cust
Customer(2) = TIPO
Customer(3) = AB
Customer(4) = CAB
Customer(5) = NR
Dim Customer(1 To 5)
Customer(1) = ID_Cust
Customer(2) = TIPO
Customer(3) = AB
Customer(4) = CAB
Customer(5) = NR
Best wishes,
Hans
Hans
-
- PlatinumLounger
- Posts: 4362
- Joined: 26 Apr 2010, 17:36
Re: fill array with 5 items for each occurrence
but when i process the next line, this code store the new id customer? or i need to use the redim preserve statement?HansV wrote:You could use
Dim Customer(1 To 5)
Customer(1) = ID_Cust
Customer(2) = TIPO
Customer(3) = AB
Customer(4) = CAB
Customer(5) = NR
-
- Administrator
- Posts: 78536
- Joined: 16 Jan 2010, 00:14
- Status: Microsoft MVP
- Location: Wageningen, The Netherlands
Re: fill array with 5 items for each occurrence
You only need Dim Customer(1 To 5) once, at the beginning of your macro.
Within the loop, you'll overwrite the values from the previous line. If you want to keep those values, you'll need a two-dimensional array instead of a one-dimensional one.
Within the loop, you'll overwrite the values from the previous line. If you want to keep those values, you'll need a two-dimensional array instead of a one-dimensional one.
Best wishes,
Hans
Hans
-
- PlatinumLounger
- Posts: 4362
- Joined: 26 Apr 2010, 17:36
Re: fill array with 5 items for each occurrence
ok, tks for expalinHansV wrote:You only need Dim Customer(1 To 5) once, at the beginning of your macro.
Within the loop, you'll overwrite the values from the previous line. If you want to keep those values, you'll need a two-dimensional array instead of a one-dimensional one.
in this case i need the two-dimensional array, how to?
-
- Administrator
- Posts: 78536
- Joined: 16 Jan 2010, 00:14
- Status: Microsoft MVP
- Location: Wageningen, The Netherlands
Re: fill array with 5 items for each occurrence
Use somethin like this:
x = x + 1
ReDim Preserve Customer(1 To 5, 1 To x)
Customer(1, x) = ID_Cust
Customer(2, x) = TIPO
Customer(3, x) = AB
Customer(4, x) = CAB
Customer(5, x) = NR
x = x + 1
ReDim Preserve Customer(1 To 5, 1 To x)
Customer(1, x) = ID_Cust
Customer(2, x) = TIPO
Customer(3, x) = AB
Customer(4, x) = CAB
Customer(5, x) = NR
Best wishes,
Hans
Hans
-
- PlatinumLounger
- Posts: 4362
- Joined: 26 Apr 2010, 17:36
Re: fill array with 5 items for each occurrence
now... in this case if i dont remember wron(based your old suggestion) i dont need to Dim the array because the redim preserve just dim the array, or not?HansV wrote:Use somethin like this:
x = x + 1
ReDim Preserve Customer(1 To 5, 1 To x)
Customer(1, x) = ID_Cust
Customer(2, x) = TIPO
Customer(3, x) = AB
Customer(4, x) = CAB
Customer(5, x) = NR
when i loop the array have a blank value!
I use this:
For X = 0 To UBound(Customer)
Debug.Print Customer(1, X)
Next X
-
- Administrator
- Posts: 78536
- Joined: 16 Jan 2010, 00:14
- Status: Microsoft MVP
- Location: Wageningen, The Netherlands
Re: fill array with 5 items for each occurrence
You do need a line
Dim Customer()
at the beginning of your macro.
Dim Customer()
at the beginning of your macro.
Best wishes,
Hans
Hans
-
- PlatinumLounger
- Posts: 4362
- Joined: 26 Apr 2010, 17:36
Re: fill array with 5 items for each occurrence
OPS! during you post solution i have modified my last post....se the last lineHansV wrote:You do need a line
Dim Customer()
at the beginning of your macro.
in other case i test the array loop with, but dont work
Dim M As Long, N As Long
For M = LBound(Customer, 1) To UBound(Customer, 1)
For N = LBound(Customer, 2) To UBound(Customer, 2)
Debug.Print Customer(M, N)
Next N
Next M
-
- Administrator
- Posts: 78536
- Joined: 16 Jan 2010, 00:14
- Status: Microsoft MVP
- Location: Wageningen, The Netherlands
Re: fill array with 5 items for each occurrence
Do you call that code after populating the array? If so, the variables ID_cust, TIPO, AB , CAB, and NR must have been blank...
Best wishes,
Hans
Hans
-
- PlatinumLounger
- Posts: 4362
- Joined: 26 Apr 2010, 17:36
Re: fill array with 5 items for each occurrence
yes, i loop after populating the array.HansV wrote:Do you call that code after populating the array? If so, the variables ID_cust, TIPO, AB , CAB, and NR must have been blank...
when i loop to view each items?
-
- Administrator
- Posts: 78536
- Joined: 16 Jan 2010, 00:14
- Status: Microsoft MVP
- Location: Wageningen, The Netherlands
Re: fill array with 5 items for each occurrence
Try single-stepping through the code to try to find out what is happening.
(It's impossible for me to know that from a distance)
(It's impossible for me to know that from a distance)
Best wishes,
Hans
Hans
-
- PlatinumLounger
- Posts: 4362
- Joined: 26 Apr 2010, 17:36
Re: fill array with 5 items for each occurrence
Peraphs solved with:HansV wrote:Try single-stepping through the code to try to find out what is happening.
(It's impossible for me to know that from a distance)
Code: Select all
...
dim myarray() As Variant
...
X = X + 1
ReDim Preserve myarray(1 To 5, 1 To X)
myarray(1, X) = IMPORTO
myarray(2, X) = TIPO
myarray(3, X) = ABI
myarray(4, X) = CAB
myarray(5, X) = NR
....
and i loop with:
...
Dim M As Long, N As Long
For M = LBound(myarray, 1) To UBound(myarray, 1)
For N = LBound(myarray, 2) To UBound(myarray, 2)
Debug.Print myarray(M, N)
Next N
Next M
...
1440
P7
01005
03298
3087878787837298852
taths is ok.
but if the series is more of one have(two series in this case):
1440
1420
P7
P1
01005
01005
03298
03992
3087878787837298852
3056566512372988529
but i need always a separate series, similar this:
1440
P7
01005
03298
3087878787837298852
next series:
1420
P1
01005
03992
3056566512372988529
how to modify the code?
-
- Administrator
- Posts: 78536
- Joined: 16 Jan 2010, 00:14
- Status: Microsoft MVP
- Location: Wageningen, The Netherlands
Re: fill array with 5 items for each occurrence
Exchange the loops for M and N:
Code: Select all
For N = LBound(myarray, 2) To UBound(myarray, 2)
For M = LBound(myarray, 1) To UBound(myarray, 1)
Debug.Print myarray(M, N)
Next M
' Optional: insert blank row
Debug.Print
Next N
Best wishes,
Hans
Hans
-
- PlatinumLounger
- Posts: 4362
- Joined: 26 Apr 2010, 17:36
Re: fill array with 5 items for each occurrence
i LO** YOU!HansV wrote:Exchange the loops for M and N:
Code: Select all
For N = LBound(myarray, 2) To UBound(myarray, 2) For M = LBound(myarray, 1) To UBound(myarray, 1) Debug.Print myarray(M, N) Next M ' Optional: insert blank row Debug.Print Next N
Bologna( ) wait me and wine naturally!
-
- PlatinumLounger
- Posts: 4362
- Joined: 26 Apr 2010, 17:36
Re: fill array with 5 items for each occurrence
Still with array...HansV wrote:Exchange the loops for M and N:
Code: Select all
For N = LBound(myarray, 2) To UBound(myarray, 2) For M = LBound(myarray, 1) To UBound(myarray, 1) Debug.Print myarray(M, N) Next M ' Optional: insert blank row Debug.Print Next N
during a line line by in text file i can have:
d1 d2 d3 d4 d5 year t euro s nr
0690 001 990007280 01 03 2008 C 719,00 1 88000001825
0690 001 990007280 01 04 2008 C 720,00 1 88000001825
0690 002 770007280 01 04 2008 C 720,00 1 88000001825
4) AGG
d1 d2 d3 d4 d5 year t euro s nr
0690 002 886001659 01 02 2008 C 568,00 1 260000520095
4) AGG
i need to fill array until code found AGG, how to?
and loop it after code found AGG
-
- Administrator
- Posts: 78536
- Joined: 16 Jan 2010, 00:14
- Status: Microsoft MVP
- Location: Wageningen, The Netherlands
Re: fill array with 5 items for each occurrence
Can you explain in more detail what you want to do?
Best wishes,
Hans
Hans
-
- PlatinumLounger
- Posts: 4362
- Joined: 26 Apr 2010, 17:36
Re: fill array with 5 items for each occurrence
the lines i have posted are a lines in txt file.HansV wrote:Can you explain in more detail what you want to do?
i need to fill array with the nine value of lines until code to read line by line found AGG.
in first block is an array with 3 series of nine value
the second block is an array with 1 series of nine value
note:
i just have a vars filled for the nine value, simply var1, var2,var3....var9:
var1 for 0690
var2 for 001
...
var9 for 88000001825
ecc.....
-
- Administrator
- Posts: 78536
- Joined: 16 Jan 2010, 00:14
- Status: Microsoft MVP
- Location: Wageningen, The Netherlands
Re: fill array with 5 items for each occurrence
What happens with the array when we find "AGG"?
Best wishes,
Hans
Hans
-
- PlatinumLounger
- Posts: 4362
- Joined: 26 Apr 2010, 17:36
Re: fill array with 5 items for each occurrence
HansV wrote:What happens with the array when we find "AGG"?
a little progress:
...
X = X + 1
ReDim myarray(1 To 9, 1 To X)
myarray(1, X) = var1
myarray(2, X) = var2
myarray(3, X) = var3
myarray(4, X) = var4
myarray(5, X) = var5
myarray(6, X) = var6
myarray(7, X) = var7
myarray(8, X) = var8
myarray(9, X) = var9
...
If Mid$(TextOfLine, 1, 3) = "AGG " Then
AGG ="OK"
End If
...
If AGG = "OK" Then
For N = LBound(myarray, 2) To UBound(myarray, 2)
For M = LBound(myarray, 1) To UBound(myarray, 1)
Debug.Print myarray(M, N)
Next M
Next N
Erase myarray()
AGG = ""
X = 0
End If
but when UBound(myarray, 2) is >1 the code loop with a blank value(!?)