当我只运行我的程序一行(而不是变量i,放任何其他数字)的工作表时,没有问题。但是当我尝试使用FOR循环为其他行执行此操作时,会显示错误消息Run time error 1004. Method 'Range' object '_Global' _ failed
。调试器始终突出显示第14行。但我不知道我是否必须解决这条问题。希望有人可以帮助我...我的代码如下。
Sub depth()
Dim VAR1 As Double
Dim VAR2 As Double
Dim VAR3 As Double
Dim finalDepth As Double
Dim i As Integer
' Calculation of depth when soil temperature is zero. Calculation
' was made considering linear change of temperature between 2 values
With ThisWorkbook.Worksheets("sheet1")
For i = 2 To 30547
' (PART HIGHLIGHTED BY DEBUGGER)
If Range("Fi").Value > 0 And _
Range("Gi").Value > 0 And _
Range("Hi").Value > 0 And _
Range("Ii").Value > 0 And _
Range("Ji").Value > 0 And _
Range("Ki").Value > 0 And _
Range("Li").Value > 0 And _
Range("Mi").Value > 0 And _
Range("Ni").Value > 0 And _
Range("Oi").Value > 0 And _
Range("Pi").Value > 0 Or _
Range("Fi").Value < 0 And _
Range("Gi").Value < 0 And _
Range("Hi").Value < 0 And _
Range("Ii").Value < 0 And _
Range("Ji").Value < 0 And _
Range("Ki").Value < 0 And _
Range("Li").Value < 0 And _
Range("Mi").Value < 0 And _
Range("Ni").Value < 0 And _
Range("Oi").Value < 0 And _
Range("Pi").Value < 0 Then
Range("Ri").Value = 0
' Calculation of depth when temperature is zero for each column on one row
ElseIf (Range("Fi").Value > 0 And Range("Gi").Value < 0) Then
VAR1 = (Range("Fi") - Range("Gi"))
VAR2 = VAR1 / (Range("G1") - Range("F1"))
VAR3 = Range("Fi") / VAR2
finalDepth = Range("F1") + VAR3
Range("Ri").Value = finalDepth
' Columns g and h
ElseIf (Range("Gi").Value > 0 And Range("Hi").Value < 0) Then
VAR1 = (Range("Gi") - Range("Hi"))
VAR2 = VAR1 / (Range("H1") - Range("G1"))
VAR3 = Range("Gi") / VAR2
finalDepth = Range("G1") + VAR3
Range("Ri").Value = finalDepth
' Columns h and i
ElseIf (Range("Hi").Value > 0 And Range("Ii").Value < 0) Then
VAR1 = (Range("Hi") - Range("Ii"))
VAR2 = VAR1 / (Range("I1") - Range("H1"))
VAR3 = Range("Hi") / VAR2
finalDepth = Range("H1") + VAR3
Range("Ri").Value = finalDepth
' Columns i and j
ElseIf (Range("Ii").Value > 0 And Range("Ji").Value < 0) Then
VAR1 = (Range("Ii") - Range("Ji"))
VAR2 = VAR1 / (Range("J1") - Range("I1"))
VAR3 = Range("Ii") / VAR2
finalDepth = Range("I1") + VAR3
Range("Ri").Value = finalDepth
' Columns j and k
ElseIf (Range("Ji").Value > 0 And Range("Ki").Value < 0) Then
VAR1 = (Range("Ji") - Range("Ki"))
VAR2 = VAR1 / (Range("K1") - Range("J1"))
VAR3 = Range("Ji") / VAR2
finalDepth = Range("J1") + VAR3
Range("Ri").Value = finalDepth
' Columns k and l
ElseIf (Range("Ki").Value > 0 And Range("Li").Value < 0) Then
VAR1 = (Range("Ki") - Range("Li"))
VAR2 = VAR1 / (Range("L1") - Range("K1"))
VAR3 = Range("Ki") / VAR2
finalDepth = Range("K1") + VAR3
Range("Ri").Value = finalDepth
' Columns l and m
ElseIf (Range("Li").Value > 0 And Range("Mi").Value < 0) Then
VAR1 = (Range("Li") - Range("Mi"))
VAR2 = VAR1 / (Range("M1") - Range("L1"))
VAR3 = Range("Li") / VAR2
finalDepth = Range("L1") + VAR3
Range("Ri").Value = finalDepth
' Columns m and n
ElseIf (Range("Mi").Value > 0 And Range("Ni").Value < 0) Then
VAR1 = (Range("Mi") - Range("Ni"))
VAR2 = VAR1 / (Range("N1") - Range("M1"))
VAR3 = Range("Mi") / VAR2
finalDepth = Range("M1") + VAR3
Range("Ri").Value = finalDepth
' Columns n and o
ElseIf (Range("Ni").Value > 0 And Range("Oi").Value < 0) Then
VAR1 = (Range("Ni") - Range("Oi"))
VAR2 = VAR1 / (Range("O1") - Range("N1"))
VAR3 = Range("Ni") / VAR2
finalDepth = Range("N1") + VAR3
Range("Ri").Value = finalDepth
' Column o and p
ElseIf (Range("Oi").Value > 0 And Range("Pi").Value < 0) Then
VAR1 = (Range("Oi") - Range("Pi"))
VAR2 = VAR1 / (Range("P1") - Range("O1"))
VAR3 = Range("Oi") / VAR2
finalDepth = Range("O1") + VAR3
Range("Ri").Value = finalDepth
End If
Next i
End With
End Sub
答案 0 :(得分:0)
您还需要在所有Range调用之前添加句点
.Range("F" & i).Value