How can I reference a cell using a variable?

  • Thread starter Thread starter Eddie Stevens
  • Start date Start date
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
 
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
 
Thanks! That is exactly what I was looking for.
Didn't know about Cells(r,c).
Kind of new at this.

Eddie
 
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
 
Back
Top