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?
LOOPING variable
-
- Administrator
- Posts: 78415
- Joined: 16 Jan 2010, 00:14
- Status: Microsoft MVP
- Location: Wageningen, The Netherlands
Re: LOOPING variable
No, VB6 doesn't support that.
You can use an array instead, or a Collection object.
You can use an array instead, or a Collection object.
Best wishes,
Hans
Hans
-
- PlatinumLounger
- Posts: 4353
- Joined: 26 Apr 2010, 17:36
-
- PlatinumLounger
- Posts: 4353
- Joined: 26 Apr 2010, 17:36
Re: LOOPING variable
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
...
While Not (EOF(nFreeFile))
Line Input #nFreeFile, sAux
COLA=MID(ECC)
COLB=LEFT(ECC)
...
COLBJ=MId(ECC)
Wend
-
- Administrator
- Posts: 78415
- Joined: 16 Jan 2010, 00:14
- Status: Microsoft MVP
- Location: Wageningen, The Netherlands
Re: LOOPING variable
For example
(This won't work beyond COLZ)
Or:
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
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
Hans
-
- PlatinumLounger
- Posts: 4353
- Joined: 26 Apr 2010, 17:36
Re: LOOPING variable
i use the arry...HansV wrote: ↑04 Feb 2021, 21:25For example
(This won't work beyond COLZ)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
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
but the value of 62 is variable... idea?
-
- Administrator
- Posts: 78415
- Joined: 16 Jan 2010, 00:14
- Status: Microsoft MVP
- Location: Wageningen, The Netherlands
Re: LOOPING variable
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
Hans
-
- PlatinumLounger
- Posts: 4353
- Joined: 26 Apr 2010, 17:36
Re: LOOPING variable
HUMMM...HansV wrote: ↑04 Feb 2021, 21:25For example
(This won't work beyond COLZ)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
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
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?
-
- Administrator
- Posts: 78415
- Joined: 16 Jan 2010, 00:14
- Status: Microsoft MVP
- Location: Wageningen, The Netherlands
Re: LOOPING variable
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
Hans