Здравствуйте. Такой вот вопрос есть два модуля почти одинаковые в одном модуле(nap) условие срабатывает как положено а во втором(yfg) почему то значение находится но все равно срабатывает if err подскажите что не так.
А именно вот этот кусок кода:
Код (vb) Выделить
Sub yfg()
Workbooks.Open ThisWorkbook.Path & "\b.xlsm"
Dim rCell As Range, avArr, li As Long, i As Long, vCriteria
Set ws = Workbooks("b.xlsm").Sheets("B")
Set ws2 = Workbooks("Ж.xlsm").Sheets("К")
endrow = ws2.Cells(4, 2).End(xlDown).Row
For i = endrow To 4 Step -1
li = 0
If ws2.Cells(i, 1).Interior.Color = ws2.Cells(3, 18).Interior.Color Then GoTo 1
x = ws2.Cells(i, 2).Value
ReDim avArr(1 To Rows.Count, 1 To 1)
vCriteria = x
With New Collection
On Error Resume Next
For Each rCell In ws.Range("C2", ws.Cells(ws.Rows.Count, 3).End(xlUp))
If rCell = vbNullString Or rCell = "" Then Resume Next
If rCell.Offset(, -1).Value = vCriteria Then
.Add rCell.Value, CStr(rCell.Value)
If Err = 0 Then
li = li + 1: avArr(li, 1) = rCell.Value
Else: Err.Clear
End If
End If
Next
End With
If li Then
ws2.Rows(i + 1 & ":" & i + li).Insert Shift:=xlDown
ws2.Cells(i + 1, 2).Resize(li).Value = avArr
ws2.Range(ws2.Cells(i + 1, 1), ws2.Cells(i + li, 24)).Interior.Color = ws2.Cells(1, 18).Interior.Color
With ws2
NameX = "Другие"
NameY = "Другие2"
RowWs2 = Empty
RowWs2 = ws2.Range(ws2.Cells(i + 1, 2), ws2.Cells(i + li, 2)).Find(What:=NameX, LookIn:=xlFormulas _
, LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
MatchCase:=False, SearchFormat:=False).Row
If Err Then
RowWs2 = Empty
RowWs2 = ws2.Range(ws2.Cells(i + 1, 2), ws2.Cells(i + li, 2)).Find(What:=NameY, LookIn:=xlFormulas _
, LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
MatchCase:=False, SearchFormat:=False).Row
If Err Then
R = i + li
Else
v = .Cells(RowWs2, 2).Value
u = .Cells(i + li, 2).Value
.Cells(i + li, 2) = v
.Cells(RowWs2, 2) = u
R = i + li - 1
End If
Else
v = .Cells(RowWs2, 2).Value
u = .Cells(i + li, 2).Value
.Cells(i + li, 2) = v
.Cells(RowWs2, 2) = u
RowWs2 = Empty
RowWs2 = ws2.Range(ws2.Cells(i + 1, 2), ws2.Cells(i + li, 2)).Find(What:=NameY, LookIn:=xlFormulas _
, LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
MatchCase:=False, SearchFormat:=False).Row
If Err Then
R = i + li - 1
Else
v = .Cells(RowWs2, 2).Value
u = .Cells(i + li - 1, 2).Value
.Cells(i + li - 1, 2) = v
.Cells(RowWs2, 2) = u
R = i + li - 2
End If
End If
End With
ws2.Sort.SortFields.Clear
ws2.Sort.SortFields.Add Key:=Range("B:B"), _
SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
With ws2.Sort
.SetRange Range(ws2.Cells(i + 1, 2), ws2.Cells(R, 2))
.Header = xlNo
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
End If
1
Next
Workbooks("b.xlsm").Close 0
End Sub
А именно вот этот кусок кода:
Код (vb) Выделить
With ws2
NameX = "Другие"
NameY = "Другие2"
RowWs2 = Empty
RowWs2 = ws2.Range(ws2.Cells(i + 1, 2), ws2.Cells(i + li, 2)).Find(What:=NameX, LookIn:=xlFormulas _
, LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
MatchCase:=False, SearchFormat:=False).Row
If Err Then
RowWs2 = Empty
RowWs2 = ws2.Range(ws2.Cells(i + 1, 2), ws2.Cells(i + li, 2)).Find(What:=NameY, LookIn:=xlFormulas _
, LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
MatchCase:=False, SearchFormat:=False).Row
If Err Then
R = i + li
Else
v = .Cells(RowWs2, 2).Value
u = .Cells(i + li, 2).Value
.Cells(i + li, 2) = v
.Cells(RowWs2, 2) = u
R = i + li - 1
End If
Else
v = .Cells(RowWs2, 2).Value
u = .Cells(i + li, 2).Value
.Cells(i + li, 2) = v
.Cells(RowWs2, 2) = u
RowWs2 = Empty
RowWs2 = ws2.Range(ws2.Cells(i + 1, 2), ws2.Cells(i + li, 2)).Find(What:=NameY, LookIn:=xlFormulas _
, LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
MatchCase:=False, SearchFormat:=False).Row
If Err Then
R = i + li - 1
Else
v = .Cells(RowWs2, 2).Value
u = .Cells(i + li - 1, 2).Value
.Cells(i + li - 1, 2) = v
.Cells(RowWs2, 2) = u
R = i + li - 2
End If
End If
End With
