Vba 应用程序定义或对象定义的公式错误

Vba 应用程序定义或对象定义的公式错误,vba,excel,excel-formula,Vba,Excel,Excel Formula,我得到了一个“应用程序定义或对象定义错误”与最后一个公式块在这段代码。另外两个很好,最后一个公式,当插入到单元格中时,在那里也很好 知道是什么导致了错误吗 Sub Wells_cartesian_to_spherical() Range("M1").Value = "Boret lengde" Range("NJ1").Value = "Asimuth" Range("O1").Value = "Boret helning" Dim RngX2 As Range Set RngX2 = Ran

我得到了一个“应用程序定义或对象定义错误”与最后一个公式块在这段代码。另外两个很好,最后一个公式,当插入到单元格中时,在那里也很好

知道是什么导致了错误吗

Sub Wells_cartesian_to_spherical()
Range("M1").Value = "Boret lengde"
Range("NJ1").Value = "Asimuth"
Range("O1").Value = "Boret helning"

Dim RngX2 As Range
Set RngX2 = Range(Cells(2, 2), Cells(Rows.Count, 2).End(xlUp)).Offset(, 11)
RngX2.Formula = "=SQRT(((B2-I2)^2+(C2-J2)^2)+(D2-K2)^2)"
RngX2.Value = RngX2.Value

Dim RngY2 As Range
Set RngY2 = Range(Cells(2, 2), Cells(Rows.Count, 2).End(xlUp)).Offset(, 13)
RngY2.Formula = "=DEGREES(ASIN(SQRT((B2-I2)^2+(C2-J2)^2)/(SQRT(((B2-I2)^2+(C2-J2)^2)+(D2-K2)^2))))"
'RngY2.Value = RngY2.Value

Dim RngZ2 As Range
Set RngZ2 = Range(Cells(2, 2), Cells(Rows.Count, 2).End(xlUp)).Offset(, 12)
RngZ2.Formula = "=DEGREES(IF(I2-B2>0,(PI()/2)-((ATAN((J2-C2)/(I2-B2)))),IF(I2-B2<0,((3*PI())/2)-((ATAN((J2-C2)/(I2-B2)))),IF(J2-C2<0,PI(),0))))"
RngZ2.Value = RngZ2.Value
End Sub

只需复制并粘贴到Excel即可。请原谅任何挪威字符/单词;)

好吧,我设法用R1C1表示法解决了这个问题。我刚刚录制了一个宏输入公式,并将生成的vba公式复制到代码中。成功了。因此,错误一定在单元寻址的某个地方。谢谢大家的意见,伙计们

Dim RngZ2 As Range
Set RngZ2 = Range(Cells(2, 2), Cells(Rows.Count, 2).End(xlUp)).Offset(, 12)
RngZ2.FormulaR1C1 = "=DEGREES(IF(RC[-5]-RC[-12]>0,(PI()/2)-((ATAN((RC[-4]-RC[-11])/(RC[-5]-RC[-12])))),IF(RC[-5]-RC[-12]<0,((3*PI())/2)-((ATAN((RC[-4]-RC[-11])/(RC[-5]-RC[-12])))),IF(RC[-4]-RC[-11]<0,PI(),0))))"
RngZ2.Value = RngZ2.Value
Dim RngZ2作为范围
设置RngZ2=范围(单元格(2,2),单元格(Rows.Count,2).End(xlUp)).Offset(,12)

RngZ2.FormulaR1C1=“=度(如果(RC[-5]-RC[-12]>0,(PI()/2)-((ATAN((RC[-4]-RC[-11])/(RC[-5]-RC[-12])),如果(RC[-5]-RC[-12]),我运行了你的代码,没有得到任何错误?嗨,我刚刚用你的数据尝试了你的代码,它对我来说很好。唯一的是
Range(“NJ1”)。Value=“Asimuth“
可能应该是
N1
而不是
NJ1
。更好的是:
范围(“M1:O1”)。值=数组(“Bo..”、“As..”、“Bo..”)
可以替换所有三行。真奇怪……那么错误肯定在其他地方。我看不出它失败的原因,所以我很困惑。@CallumDA,我不知道你的意思。你指的字符串在哪里?@CallumDA,对不起,我现在看到了
Dim RngZ2 As Range
Set RngZ2 = Range(Cells(2, 2), Cells(Rows.Count, 2).End(xlUp)).Offset(, 12)
RngZ2.FormulaR1C1 = "=DEGREES(IF(RC[-5]-RC[-12]>0,(PI()/2)-((ATAN((RC[-4]-RC[-11])/(RC[-5]-RC[-12])))),IF(RC[-5]-RC[-12]<0,((3*PI())/2)-((ATAN((RC[-4]-RC[-11])/(RC[-5]-RC[-12])))),IF(RC[-4]-RC[-11]<0,PI(),0))))"
RngZ2.Value = RngZ2.Value