1

I am writing a code where I have a for loop in which I give a variable (named VType ) some value. For loop goes for a range of i variables. Now I want to make a new variable by concatenating names of both variables. for example if i = 1 then I want to make variable VType1. Here is my piece of code.

nrec = Split(Split(ie.document.body.innerHTML, "Found <strong>")(1), "</strong> records")(0)
If nrec = 1 Then
    lnk.Click
Else
    For j = 1 To nrec
        link.Click

        Do While ie.readyState <> 4: Wait 5: Loop

        Application.Wait (Now + TimeValue("0:00:01"))
        'VType , j = GetType
        'Application.Wait (Now + TimeValue("0:00:01"))
        IMO , j = GetValue("IMO:")
        'MMSI = GetValue("MMSI:")
        YBuilt , j = GetValue("Year Built:")
        Flag , j = GetValue("Flag:")
        DWT , j = GetValue("Deadweight:")
    Next j

    num = "1 - " & IMO1
    For i = 2 To nrec
        num = num & vbCrLf & i & "abc"
    Next I
    fin = InputBox(num, nrec & " records found for a. please select right one.")

    Exit For
End If
pgSystemTester
  • 8,979
  • 2
  • 23
  • 49
Ashish Baboo
  • 154
  • 1
  • 1
  • 13
  • 3
    I've heard of something called *arrays*... – A.S.H Jul 23 '17 at 16:30
  • 2
    Possible duplicate of [How to create dynamic variable names VBA](https://stackoverflow.com/questions/38254337/how-to-create-dynamic-variable-names-vba) – trincot Jul 23 '17 at 16:47

2 Answers2

0

There is not a way to directly do what you're specifically requesting. However, you can use arrays to get a similar outcome. Arrays are a not a topic that can be explained in a single posted answer, but if you do a little research you can probably figure out how the below might be useful...

Dim VTtyp(0 to i) as string


'while Looping... 
Vtype(i) = "Whatever you want stored in this round of i"

When your code completes, you'll have all fields saved as variables that can be called from this array. An example is if you wanted to call the one that was tied to the number "2" you could type: Vtype(2) and it would call the text from the 2 iteration.

Again this example is extremely simplified and there are things to consider such as dim size, changing the dim, preserving the array, etc. and that is something you'll have to research further. However bottom line is, "there is not a way to do what you're specifically trying to do."

pgSystemTester
  • 8,979
  • 2
  • 23
  • 49
0

You can achieve this using Dictionary objects concept. Go through the below link to know more about dictionary objects.

https://www.tutorialspoint.com/vbscript/vbscript_dictionary_objects.htm

Arun Raj
  • 47
  • 6