Ken*_*nes 1 syntax excel vba excel-vba excel-formula
我正在尝试浏览工作表中的一系列单元格,并在每个单元格中编写公式.但我一直收到错误:
Run-time error '1004'
Application-defined or object-defined error
Run Code Online (Sandbox Code Playgroud)
代码现在看起来像这样:
Sub updateFormulasForNamedRange()
'Application.Calculation = xlCalculationManual
'Application.ScreenUpdating = False
Dim row, col, fieldCount As Integer
colCount = 13
RowCount = 60
For col = 1 To colCount
For row = 1 To RowCount
Dim strColCharacter
If col > 26 Then
strColCharacter = Chr(Int((row - 1) / 26) + 64) & Chr(((row - 1) Mod 26) + 65)
Else
strColCharacter = Chr(row + 64)
End If
Worksheets("Rawdata1").Cells(row, col).FormulaR1C1 = "=IF(Numbers1!$E$" & col & "<>0;Numbers1!" & "$" & strColCharacter & "$" & row & ";"""")"
Next row
Next col
'Application.Calculation = xlCalculationAutomatic
'Application.ScreenUpdating = True
End Sub
Run Code Online (Sandbox Code Playgroud)
它在您将公式分配给单元格的行中失败.我试图用"测试"替换字符串,它的工作原理.但是这个字符串是不被接受的.即使它与当前在该确切单元格的公式栏中的字符串完全相同.字符串看起来很好吗?
"=IF(Numbers1!$E$1<>0;Numbers1!$A$1;"")"
Run Code Online (Sandbox Code Playgroud)
我不太清楚所有公式属性的区别,但我尝试了它们的变体并且都抛出相同的错误.那么可能导致此错误的原因是什么?
你的问题是.FormulaR1C1.这告诉公式期望行号,列号样式公式引用,但是然后给它一个地址(Column,Row)样式公式.
将.FormulaR1C1更改为.Formula