Reputation: 1
In form controls we can use { controls("Textbox"&1) }
as for loops.
My question is I have already defined the String as D1,D2,D3. I want to use like D as common and suffix as variable
sub abcd ()
dim i, k as integer
dim D1 as string
dim D2 as string
k="abcd1"
for i = 1 to 2
if k<> "" then 'like controls("textbox" & i ) for loop
"D"&i = true
Else "D" & i+1
end sub
It shows a messagebox with the error:
expected : line number or label or statement or end of statement
Upvotes: 0
Views: 44
Reputation: 57683
This code has several (actually a lot) of issues:
Dim i, k As Integer
declares k As Integer
but i As Variant
in VBA you need to specify a type for every variable.D1
as String
but True
is a Boolean
.If you declare Dim D1 As String
you cannot access the variabele with "D" & i = True
. This is no valid syntax. Therefore you would need to use an array:
Dim MyArray(1 To 2) As Boolean
So you can access it like
MyArray(i) = True 'where i can be 1 or 2
If you declare k As Integer
that means k
repersents a number (without decimals). So you cannot put text in there k = "abcd1"
, this will produce a type missmatch. Also comparing k
against a string (text) ""
like If k <> "" Then
will missmatch since k
is a number.
Also have a look at data type summary to study which data type to use for which kind of data.
Your For i = 1 To 2
loop is missing a Next i
in the end of the loop.
If
statement is missing a End If
in the end.So in general I can only recommend you to study some tutorials or books about the basic syntax of VBA, you are lacking a lot of basic things.
So the following example might help you:
Option Explicit
Public Sub Test()
Dim i As Long
Dim MyArray(1 To 3) As Boolean
For i = 1 To 3
MyArray(i) = True
Next i
End Sub
Upvotes: 1