PDA

View Full Version : Make VBA Cell a Formula



ronjon65
03-11-2012, 04:08 PM
I have some code that finds the value based upon a counter, such as:

Cells(10, 10) = Cells(CounterA, ColumnA)

...Lets say CounterA=1

I want Cells(10,10) to actually be "=A1", rather than whatever value was contained when it was run. Any ideas? For some reason, I thought I did this before, but can not find the code to do it.

Kenneth Hobs
03-11-2012, 05:04 PM
Dim CounterA As Long, ColumnA As String
ColumnA = "A"
CounterA = 1
Cells(10, 10).Formula = "=" & ColumnA & CounterA

ronjon65
03-11-2012, 05:28 PM
Is there a more "direct" way? What can one do if ColumnA is not a constant value (but rather obtained from a loop).

Perhaps I gave an oversimplified example. That said, I may still be able to get this to do what I want "as is".

GTO
03-11-2012, 07:46 PM
Try:
Sub example()
Const CounterA As Long = 1
Const ColumnA As Long = 1

Cells(10, 10).Formula = "=" & Cells(CounterA, ColumnA).Address(0, 0, , -1)
End Sub