LOOPING variable

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

LOOPING variable

Post by sal21 »

I have as string this var in a project:

COLA="1"
COLB="gg"
COLC=ttt"
...

COLBJ="yy"

The sequence of var is COL+character (A, B, C...BJ)

possible to create a loop with this variable?

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

Re: LOOPING variable

Post by HansV »

No, VB6 doesn't support that.
You can use an array instead, or a Collection object.
Best wishes,
Hans

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

Re: LOOPING variable

Post by sal21 »

HansV wrote:
04 Feb 2021, 20:22
No, VB6 doesn't support that.
You can use an array instead, or a Collection object.
EXAMPLE...

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

Re: LOOPING variable

Post by sal21 »

in effect i fill the var with a loop of .txt with a freefile procedure:

...
While Not (EOF(nFreeFile))
Line Input #nFreeFile, sAux

COLA=MID(ECC)
COLB=LEFT(ECC)
...
COLBJ=MId(ECC)

Wend

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

Re: LOOPING variable

Post by HansV »

For example

Code: Select all

Sub Test()
    Dim COL As New Collection
    COL.Add Item:="1", Key:="COLA"
    COL.Add Item:="gg", Key:="COLB"
    COL.Add Item:="ttt", Key:="COLC"
    '...
    COL.Add Item:="yy", Key:="COLZ"

    Dim L As Long
    For L = 1 To 26
        MsgBox COL("COL" & Chr(64 + L))
    Next L
End Sub
(This won't work beyond COLZ)

Or:

Code: Select all

Sub Test()
    Dim ARR(1 To 62) As Variant
    ARR(1) = "1"
    ARR(2) = "gg"
    ARR(3) = "ttt"
    '...
    ARR(62) = "yy"

    Dim L As Long
    For L = 1 To 62
        MsgBox ARR(L)
    Next L
End Sub
Best wishes,
Hans

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

Re: LOOPING variable

Post by sal21 »

HansV wrote:
04 Feb 2021, 21:25
For example

Code: Select all

Sub Test()
    Dim COL As New Collection
    COL.Add Item:="1", Key:="COLA"
    COL.Add Item:="gg", Key:="COLB"
    COL.Add Item:="ttt", Key:="COLC"
    '...
    COL.Add Item:="yy", Key:="COLZ"

    Dim L As Long
    For L = 1 To 26
        MsgBox COL("COL" & Chr(64 + L))
    Next L
End Sub
(This won't work beyond COLZ)

Or:

Code: Select all

Sub Test()
    Dim ARR(1 To 62) As Variant
    ARR(1) = "1"
    ARR(2) = "gg"
    ARR(3) = "ttt"
    '...
    ARR(62) = "yy"

    Dim L As Long
    For L = 1 To 62
        MsgBox ARR(L)
    Next L
End Sub
i use the arry...
but the value of 62 is variable... idea?

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

Re: LOOPING variable

Post by HansV »

You can use ReDim:

Code: Select all

    Dim ARR() As Variant
    Dim Size As Long
    '...
    Size = 37
    Redim ARR(1 to Size)
Best wishes,
Hans

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

Re: LOOPING variable

Post by sal21 »

HansV wrote:
04 Feb 2021, 21:25
For example

Code: Select all

Sub Test()
    Dim COL As New Collection
    COL.Add Item:="1", Key:="COLA"
    COL.Add Item:="gg", Key:="COLB"
    COL.Add Item:="ttt", Key:="COLC"
    '...
    COL.Add Item:="yy", Key:="COLZ"

    Dim L As Long
    For L = 1 To 26
        MsgBox COL("COL" & Chr(64 + L))
    Next L
End Sub
(This won't work beyond COLZ)

Or:

Code: Select all

Sub Test()
    Dim ARR(1 To 62) As Variant
    ARR(1) = "1"
    ARR(2) = "gg"
    ARR(3) = "ttt"
    '...
    ARR(62) = "yy"

    Dim L As Long
    For L = 1 To 62
        MsgBox ARR(L)
    Next L
End Sub
HUMMM...

But i loop the txt i have approx 23.xxx lines...

to the end of code the array loop only the first sequence, in....

Dim L As Long
For L = 1 To 62
MsgBox ARR(L)
Next L

peraphs i need a 2 dimensinal array?

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

Re: LOOPING variable

Post by HansV »

Try something like this:

Code: Select all

Sub Test()
    Dim f As Integer
    Dim strLines As String
    Dim arrLines() As String
    Dim i As Long
    f = FreeFile
    ' Open the text file
    Open "C:\MyFiles\TextFile.txt" For Input As #f
    ' Read it into a string variable
    strLines = Input(LOF(f), f)
    ' Close the file
    Close #f
    ' Split the string variable into lines
    arrLines = Split(strLines, vbCrLf)
    ' Clear the string variable to conserve memory
    strLines = ""
    ' Create a two-dimensional array with as many columns as you need
    ReDim arrParts(LBound(arrLines) To UBound(arrLines), 0 To 30) As String
    ' Loop through the lines
    For i = LBound(arrLines) To UBound(arrLines)
        ' Populate the two-dimensional array
        arrParts(i, 0) = Mid(arrLines(i), 1, 5)
        arrParts(i, 1) = Mid(arrLines(i), 10, 4)
        ' ...
        arrParts(i, 30) = Mid(arrLines(i), 120, 10)
    Next i
    ' Code to use arrParts goes here
    ' ...
End Sub
Best wishes,
Hans