Проверьте это, а также проверьте правильность введенных вами правил. В отправленных вами файлах нет совпадений ...
Private Sub InventoryInterpretation()
Dim strFoldPath As String, w As Workbook, wInv As Workbook, shI As Worksheet
Dim wSpec As Workbook, shS As Worksheet, boolInv As Boolean, boolSpec As Boolean
Dim strSpec As String, strInv As String, arrInv As Variant, arrSp As Variant, arrRez() As String
Dim i As Long, s As Long, strclass As String
strFoldPath = "Your folder path"
strSpec = strFoldPath & "\" & "Specification1.txt" 'user your file name
strInv = strFoldPath & "\" & "Inventory1.txt" 'user your file name
For Each w In Workbooks 'check if the necessary .txt/.csv files are opened in Excel:
If w.FullName = strSpec Then Set wSpec = w: boolSpec = True
If w.FullName = strInv Then Set wInv = w: boolInv = True
Next
If Not boolInv Then
If Dir(strInv) <> "" Then 'check if file exists
Set wInv = Workbooks.Open(strInv)
Else
MsgBox "No Inventory file in folder """ & strFoldPath & """.": Exit Sub
End If
End If
If Not boolSpec Then ' if the spec file is not opened in Excel
If Dir(strSpec) <> "" Then 'check if file exists
Set wSpec = Workbooks.Open(strSpec)
Else
MsgBox "No Specification file in folder """ & strFoldPath & """.": Exit Sub
End If
End If
Set shI = wInv.Sheets(1): Set shS = wSpec.Sheets(1)
arrInv = shI.Range("A1").CurrentRegion.Value: ' Debug.Print UBound(arrInv, 1), UBound(arrInv, 2)
arrSp = shS.Range("A1").CurrentRegion.Value: 'Debug.Print UBound(arrSp, 1), UBound(arrSp, 2)
ReDim arrRez(UBound(arrInv, 1))
'Making the real job:
shI.Cells(1, UBound(arrInv, 2) + 2).EntireColumn.Clear ' clear the column where data are returned
For i = 2 To UBound(arrInv, 1)
strclass = arrInv(i, 3) 'col 3 of inventory array
For s = 2 To UBound(arrSp, 1)
If arrSp(s, 6) = strclass Then
Stop
If CDbl(arrInv(i, 4)) >= CDbl(arrSp(s, 8)) And _
CDbl(arrInv(i, 4)) <= CDbl(arrSp(s, 9)) And _
CDbl(arrInv(i, 5)) >= CDbl(arrSp(s, 10)) And _
CDbl(arrInv(i, 7)) <= CDbl(arrSp(s, 7)) Then
arrRez(i) = "OK - " & strclass: Exit For
Else
Debug.Print CDbl(arrInv(i, 4)) >= CDbl(arrSp(s, 8))
Debug.Print CDbl(arrInv(i, 4)) <= CDbl(arrSp(s, 9))
Debug.Print CDbl(arrInv(i, 5)) >= CDbl(arrSp(s, 10))
Debug.Print CDbl(arrInv(i, 7)) <= CDbl(arrSp(s, 7))
arrRez(i - 1) = "No" & " - " & i: Exit For
End If
End If
Next s
Next i
shI.Range(shI.Cells(1, UBound(arrInv, 2) + 2), shI.Cells(UBound(arrInv, 1), _
UBound(arrInv, 2) + 2)).Value = WorksheetFunction.Transpose(arrRez)
wInv.Activate: shI.Activate
End Sub
Пожалуйста, используйте код в качестве вдохновения и для целей обучения ... Он соблюдает установленные вами правила, но между двумя этими данными не существует совпадений файлы. Пожалуйста, пересмотрите файлы или правила.
И дайте мне знать, что из вышеприведенных предположений соответствует действительности ...