简体   繁体   中英

Excel vba formula string doesn't calculate: #NAME? error

I have this sub that types in a sum formula in A1 cell:

Sub test_string()
    ThisWorkbook.Worksheets("test").Cells(1, 1) = "=СУММ(B1:D1)"
End Sub

At first it looks like it has worked, but when I open a worksheet there's a #NAME? error in A1 cell:

在此处输入图片说明

The error disappears when I manually calculate the formula (put cursor in formula's text and click Enter).

Why does this happen and is there a way to fix it? I tried

ThisWorkbook.Worksheets("test").Cells(1, 1).Calculate

But to no result.

You're not specifying which property of Cells you want.

The default property of Cells is Value. Therefore when you write:

Cells(1,1) = "=SUM(B1:D1)"

... what you're actually saying is:

Cells(1,1).Value = "=SUM(B1:D1)"

You need to use the .Formula property:

Cells(1,1).Formula = "=SUM(B1:D1)"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM