VBA EXcel - 如果使用错误陷阱没有获得对象/应用程序定义的错误,我会收到错误(400)

VBA EXcel - 如果使用错误陷阱没有获得对象/应用程序定义的错误,我会收到错误(400)

我的 Excel 表包含这些数据,共 7 列

sl.no | bed | 2| 4| 3|4|3|

有时第 7 列是空的,然后我才会收到错误

我的代码


Sub sup_cal_area_sft()
Dim lru, plru As Long
Dim myless As String
Dim ttr_l, ttr_p, ttr_nql, ttr_nqp, trft As Variant

On Error GoTo Errorcatch

myless = "*" & "LESS" & "*"

    With ActiveSheet
        lru = .Cells(.Rows.Count, "c").End(xlUp).Row
        plru = .Cells(.Rows.Count, "h").End(xlUp).Row + 2
    End With
For i = plru To lru

ttr_l = "=PRODUCT((C" & i & "+D" & i & "/12)*(E" & i & "+F" & i & "/12)*-G" & i & ")"
ttr_p = "=PRODUCT((C" & i & "+D" & i & "/12)*(E" & i & "+F" & i & "/12)*G" & i & ")"

ttr_nql = "=PRODUCT((C" & i & "+D" & i & "/12)*-(E" & i & "+F" & i & "/12)" 
ttr_nqp = "=PRODUCT((C" & i & "+D" & i & "/12)*(E" & i & "+F" & i & "/12)" 
   trft = "=PRODUCT((C" & i & "+D" & i & "/12)*G" & i & ")"

If Cells(i, 5) <> "" Then

       If UCase(Cells(i, 2).Value) Like myless Then 'negative value

              If Cells(i, 7) <> "" Then
                Cells(i, 8) = ttr_l
              Else
                 Cells(i, 8) = ttr_nql
              End If



           ElseIf Not UCase(Cells(i, 2).Value) Like myless Then 'positive Value
             If Cells(i, 7) <> "" Then
                Cells(i, 8) = ttr_p
             Else
                Cells(i, 8) = ttr_nqp
              End If
          End If

' RFT Calculaion

   ElseIf Cells(i, 3) <> "" And Cells(i, 5) = "" and cells(i,4) <> "" Then
     Cells(i, 8) = trft


' Blank
Else
    Cells(i, 8) = ""

End If

Next i

Cells(lru + 3, 2).Select

exit sub

Errorcatch:
MsgBox Err.Description 

End Sub

答案1

您漏掉了以下行的一个括号:

ttr_nql = "=PRODUCT((C" & i & "+D" & i & "/12)*-(E" & i & "+F" & i & "/12)" 
ttr_nqp = "=PRODUCT((C" & i & "+D" & i & "/12)*(E" & i & "+F" & i & "/12)" 

我认为它们应该是:

ttr_nql = "=PRODUCT((C" & i & "+D" & i & "/12)*-(E" & i & "+F" & i & "/12))" 
ttr_nqp = "=PRODUCT((C" & i & "+D" & i & "/12)*(E" & i & "+F" & i & "/12))" 

请注意,您已完成On Error GoTo Errorcatch设置,但这可能会阻止您获取合适的调试信息(例如哪条线路实际上失败了)。因此,我会在尝试调试问题时将其删除。

相关内容