Is there a way to use a For loop to add to a String? VBA

424 views Asked by At

I'm trying to add numbers onto a string. I want to loop through 1 To 80 for 'a' and 1 to 40 for 's' so that those numbers are added to strings r(1) and r(2).

The result I'm trying to get is formatted like this r(1) = "PD 1" and so on; r(2) = "QR 1" and so on.

Dim a As Integer

Dim s As Integer

Dim r(1 To 2) As String

For a = 1 To 100

    r(1) = ""PD " & "a""

Next a

For s = 1 To 40

    r(2) = "QR " & s

Next s
1

There are 1 answers

10
BigBen On

If you want an array of arrays, perhaps:

Dim pd(1 To 100) As String
Dim qr(1 To 40) As String

Dim i As Long
For i = LBound(pd) To UBound(pd)
    pd(i) = "PD " & i
Next

For i = LBound(qr) To UBound(qr)
    qr(i) = "QR " & i
Next

Dim r(1 To 2)
r(1) = pd
r(2) = qr