How can I reference a cell using a variable?

E

Eddie Stevens

How can I reference a cell using a variable?



Example:

Sub Help()

For x = 3 To 7

For y = 5 To 9

Range(x, y).Select 'THIS LINE DOES NOT WORK. WHAT IS THE
PROPER SYNTAX?

ActiveCell.FormulaR1C1 = x + y

Next y

Next x

End Sub
 
P

Per Jessen

Hi

You use Cells(r,c)
Range expect a column label like Range("A" & y)
No need to select the cell to put a value or formula in it, see below. As
you are just putting a value in the cell I would use:

Cells(y,x).Value=x+y

Sub Help()
For x = 3 To 7
For y = 5 To 9
Cells(y, x).Value = x + y
Next y
Next x
End Sub

Regards,
Per
 
E

Eddie Stevens

Thanks! That is exactly what I was looking for.
Didn't know about Cells(r,c).
Kind of new at this.

Eddie
 
P

Per Jessen

Hi Eddie

Thanks for your reply, I am glad to help.

I can recommend that you follow the group
Microsoft.Public.Excel.Programming, you will find a lot of usefull posts
regarding VBA programming there.

Per
 

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

Top