具有变量和公式的Excel vba

时间:2021-08-10 15:10:04

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.

我需要一些关于VBA的帮助。我有一个2张的excel工作簿。在表1中,J1单元格I有一个值可以变化。在表2中,我运行以下代码向单元格添加值。

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.

如果我没把问题写对,请原谅。请帮助。

1 个解决方案

#1


1  

.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.

删除双引号和额外的equals。这使它成为一个字符串,而不是计算表达式。

#1


1  

.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.

删除双引号和额外的equals。这使它成为一个字符串,而不是计算表达式。