slyclam
slyclam

Reputation: 290

Excel vba with variables and formula

I need some help Regarding VBA. I have a excel workbook with 2 sheets. In sheet 1, J1 cell I have a value which can vary. In sheet 2 I am running the following code to add values to cells.

Dim wb As Workbook
Dim nws, ws As Worksheet

Set wb = ActiveWorkbook
Set ws = wb.Sheets("Sheet1")
Set nws = wb.Sheets("Sheet2")

ws.Unprotect

With nws

.Cells(1, 2).Value2 = "0"

For i = 1 To cnt1

.Cells(j, 2).Value2 = "=.Cells(i, 2).Value2+1"

Next i

End With

I am failing in the part

=.Cells(i, 2).Value2+1

I know I'm writing it wrong. But that's the way I want the code to work.

Please excuse if I'm not putting the question correctly. Kindly help.

Upvotes: 1

Views: 105

Answers (1)

Andy G
Andy G

Reputation: 19367

.Cells(j, 2).Value2 = "=.Cells(i, 2).Value2+1"

remove the double-quotes and the extra equals. That makes it a string, rather than evaluating the expression.

Upvotes: 1

Related Questions