fill array with 5 items for each occurrence

User avatar
sal21
PlatinumLounger
Posts: 4355
Joined: 26 Apr 2010, 17:36

fill array with 5 items for each occurrence

Post by sal21 »

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)

User avatar
HansV
Administrator
Posts: 78488
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: fill array with 5 items for each occurrence

Post by HansV »

You could use

Dim Customer(1 To 5)
Customer(1) = ID_Cust
Customer(2) = TIPO
Customer(3) = AB
Customer(4) = CAB
Customer(5) = NR
Best wishes,
Hans

User avatar
sal21
PlatinumLounger
Posts: 4355
Joined: 26 Apr 2010, 17:36

Re: fill array with 5 items for each occurrence

Post by sal21 »

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
but when i process the next line, this code store the new id customer? or i need to use the redim preserve statement?

User avatar
HansV
Administrator
Posts: 78488
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: fill array with 5 items for each occurrence

Post by HansV »

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.
Best wishes,
Hans

User avatar
sal21
PlatinumLounger
Posts: 4355
Joined: 26 Apr 2010, 17:36

Re: fill array with 5 items for each occurrence

Post by sal21 »

HansV 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.
ok, tks for expalin

in this case i need the two-dimensional array, how to?

User avatar
HansV
Administrator
Posts: 78488
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: fill array with 5 items for each occurrence

Post by HansV »

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
Best wishes,
Hans

User avatar
sal21
PlatinumLounger
Posts: 4355
Joined: 26 Apr 2010, 17:36

Re: fill array with 5 items for each occurrence

Post by sal21 »

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
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? :scratch:

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

User avatar
HansV
Administrator
Posts: 78488
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: fill array with 5 items for each occurrence

Post by HansV »

You do need a line

Dim Customer()

at the beginning of your macro.
Best wishes,
Hans

User avatar
sal21
PlatinumLounger
Posts: 4355
Joined: 26 Apr 2010, 17:36

Re: fill array with 5 items for each occurrence

Post by sal21 »

HansV wrote:You do need a line

Dim Customer()

at the beginning of your macro.
OPS! during you post solution i have modified my last post....se the last line
in other case i test the array loop with, but dont work :scratch:


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

User avatar
HansV
Administrator
Posts: 78488
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: fill array with 5 items for each occurrence

Post by HansV »

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

User avatar
sal21
PlatinumLounger
Posts: 4355
Joined: 26 Apr 2010, 17:36

Re: fill array with 5 items for each occurrence

Post by sal21 »

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...
yes, i loop after populating the array.
when i loop to view each items?

User avatar
HansV
Administrator
Posts: 78488
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: fill array with 5 items for each occurrence

Post by HansV »

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)
Best wishes,
Hans

User avatar
sal21
PlatinumLounger
Posts: 4355
Joined: 26 Apr 2010, 17:36

Re: fill array with 5 items for each occurrence

Post by sal21 »

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)
Peraphs solved with:

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
...
but when I loop the array and have only one series i have:
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?

User avatar
HansV
Administrator
Posts: 78488
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: fill array with 5 items for each occurrence

Post by HansV »

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

User avatar
sal21
PlatinumLounger
Posts: 4355
Joined: 26 Apr 2010, 17:36

Re: fill array with 5 items for each occurrence

Post by sal21 »

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
i LO** YOU! :laugh: :laugh: :laugh: :clapping: :thankyou:

Bologna( :whisper:) wait me :grin: :cheers: :cheers: :cheers: and wine naturally!

User avatar
sal21
PlatinumLounger
Posts: 4355
Joined: 26 Apr 2010, 17:36

Re: fill array with 5 items for each occurrence

Post by sal21 »

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
Still with array...
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

User avatar
HansV
Administrator
Posts: 78488
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: fill array with 5 items for each occurrence

Post by HansV »

Can you explain in more detail what you want to do?
Best wishes,
Hans

User avatar
sal21
PlatinumLounger
Posts: 4355
Joined: 26 Apr 2010, 17:36

Re: fill array with 5 items for each occurrence

Post by sal21 »

HansV wrote:Can you explain in more detail what you want to do?
the lines i have posted are a lines in txt file.

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.....

User avatar
HansV
Administrator
Posts: 78488
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: fill array with 5 items for each occurrence

Post by HansV »

What happens with the array when we find "AGG"?
Best wishes,
Hans

User avatar
sal21
PlatinumLounger
Posts: 4355
Joined: 26 Apr 2010, 17:36

Re: fill array with 5 items for each occurrence

Post by sal21 »

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(!?)