我在此代码中使用最后一个公式块获得“应用程序定义或对象定义的错误”。另外两个工作正常,最后一个公式,当插入一个单元格时,也可以很好地工作。
什么想法会引发错误?
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
该公式用于计算井的顶部和底部的x,y和z坐标的钻井的井深,井的方向和井倾角。下面是一些逗号分隔的示例数据,它们应该与代码一起使用:
Well no,X,Y,Z,Drilled length,Azimuth,Inclination,objtype,X bunn,Y bunn,Z bunn
28738,83124.42153,1233423.379,121,120,0,0,Energibrønn,83124.42153,1233423.379,1
29674,87717.14427,1237872.851,139.8000031,135,0,0,Energibrønn,87717.14427,1237872.851,4.800003052
29652,90488.64255,1242007.401,229.8000031,201,0,0,Energibrønn,90488.64255,1242007.401,28.80000305
28791,90899.79513,1243677.054,274.1000061,114,0,0,Energibrønn,90899.79513,1243677.054,160.1000061
29171,88224.0884,1233361.655,127.9000015,102,100,10,Energibrønn,88241.53142,1233358.579,27.44961072
30393,88204.31375,1233372.11,127.5999985,160,125,2,Energibrønn,88208.88783,1233368.907,-32.30253385
只需复制并粘贴到Excel即可。请原谅任何挪威字符/单词;)
答案 0 :(得分:0)
好吧,我设法使用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