Произошло что-то очень странное.В какой-то момент это сработало, и внезапно я получил ошибку.
Проблема в fm
и sm
, когда включена отладка, fm = Empty
и sm = Empty
Поэтому tval = Empty
ошибка возникает в строке 34:
tval = WorksheetFunction.Sum(ws.Range(ws.Cells(fm, 11), ws.Cells(sm, 11)))
Полный код:
Private Sub TextBox6_Change()
Dim ws As Worksheet
Dim sr, leg, lr, fm, sm, tval, sval, i, j As Long
Dim wo, rc As String
Set ws = ThisWorkbook.Worksheets("Cost Analysis") ' ws = Work Sheet
sr = 5 ' sr = Start row
lr = ws.Cells(Rows.Count, 1).End(xlUp).Row ' lr = Last row
wo = Trim(UserForm1.TextBox4.Value) ' Work Order
leg = Trim(UserForm1.TextBox5.Value) ' leg
rc = Trim(UserForm1.ComboBox3.Value) ' rc = Root Cause
If wo <> "" And leg <> "" And rc <> "" And TextBox6.Value <> "" Then
If Application.WorksheetFunction.CountIfs(ws.Cells(sr, 1).Resize(lr, 1), wo, _
ws.Cells(sr, 4).Resize(lr, 1), leg, _
ws.Cells(sr, 5).Resize(lr, 1), rc) > 0 Then
For i = sr To lr
If Trim(ws.Cells(i, 1)) = wo And Trim(ws.Cells(i, 4)) = leg Then
j = j + 1
If j = 1 Then
fm = i
End If
If Trim(ws.Cells(i, 5)) = rc Then
sm = i
Exit For
Else
End If
End If
Next
tval = WorksheetFunction.Sum(ws.Range(ws.Cells(fm, 11), ws.Cells(sm, 11)))
sval = WorksheetFunction.Sum(ws.Range(ws.Cells(fm, 11), ws.Cells(sm, 11))) * Trim(TextBox6.Value)
Else
End If
End If
UserForm1.TextBox7 = tval
UserForm1.TextBox16 = sval
End Sub