Assignment problem

  • Thread starter Thread starter utkarshm
  • Start date Start date
U

utkarshm

I would like cells A1, A2 etc to populate with "Error 1", "Error 2"
etc. However when I use this:

Sub test()

E1 = "Error 1"
E2 = "Error 2"

For i = 1 To 2
Range("a" & i) = "E" & i
Next i

End Sub

I get "E1", "E2 as a result. I know I am making a silly goof and help
would be greatly appreciated.

Thanks
Utkarsh
 
You could just do this:

Sub test()
For i = 1 To 2
Range("a" & i) = "Error " & i
Next i
End Sub

But I think you're trying to do this:

Sub test2()
Dim i As Long
Dim E(1 To 2) As String

E(1) = "Error 1"
E(2) = "Error 2"
For i = 1 To 2
Range("a" & i) = E(1)
Next i
End Sub

VBA won't allow you to build variable names on the fly (like some other
languages) like you originally wanted.
 
Thanks Dave!!
It was the second solution that I was looking for.

Also, you are right about programming languages. Switching between them
causes many a heartache!

Utkarsh
 
You probably meant
Range("a" & i) = E(1)

should be

Range("a" & i) = E(i)
 
I think this is what you need as you first intended ...
Starting with a blank worksheet run this standard code:

Sub test()
Range("E1") = "Error 1"
Range("E2") = "Error 2"
For i = 1 To 2
Range("a" & i) = Range("E" & i)
Next i
End Sub

HTH
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top