Excel 是否有一种方法可以使用VBA比较单独工作表上的两个表,并查看它们是否符合标准?

Excel 是否有一种方法可以使用VBA比较单独工作表上的两个表,并查看它们是否符合标准?,excel,vba,autofilter,Excel,Vba,Autofilter,我一直在尝试创建一个代码,允许我将一个表(在本例中为库存清单)与另一个表(客户零件规格)进行比较。 我正在考虑创建一个循环,通过标签号(a列)搜索库存表,引用库存表的几个属性(类别、规格、宽度等),然后在零件号表中搜索这些属性(精确类别、规格范围、宽度范围等)。如果它找到了匹配项,我希望它显示匹配的零件号,或者至少有匹配项 我不熟悉循环或偏移引用标记,所以我不确定如何最好地实现这一点。任何帮助都将不胜感激!我对VBA非常陌生,并且在学习的过程中不断学习 这是我创建的手动搜索代码。它要求您选择要搜

我一直在尝试创建一个代码,允许我将一个表(在本例中为库存清单)与另一个表(客户零件规格)进行比较。 我正在考虑创建一个循环,通过标签号(a列)搜索库存表,引用库存表的几个属性(类别、规格、宽度等),然后在零件号表中搜索这些属性(精确类别、规格范围、宽度范围等)。如果它找到了匹配项,我希望它显示匹配的零件号,或者至少有匹配项

我不熟悉循环或偏移引用标记,所以我不确定如何最好地实现这一点。任何帮助都将不胜感激!我对VBA非常陌生,并且在学习的过程中不断学习

这是我创建的手动搜索代码。它要求您选择要搜索的标记,并参考零件号表上方的属性:

Sub FilterInventoryToPartSearch()

    Application.DisplayAlerts = False
    Application.ScreenUpdating = False

'   Filter Active Inventory

    Dim lo5 As ListObject
    Set lo5 = Sheet5.ListObjects(1)
    lo5.AutoFilter.ShowAllData
    With lo5.Range
'   Filter by Material Type

    If Sheet5.Range("f2").Text <> "" Then
        .AutoFilter field:=5, Criteria1:=Sheet5.Range("f2").Text
    End If

'   Filter by Gauge

    If Sheet5.Range("f3").Value <> "" Then
        .AutoFilter field:=7, Criteria1:="<=" & Sheet5.Range("f3").Value
    End If
    If Sheet5.Range("f3").Value <> "" Then
        .AutoFilter field:=8, Criteria1:=">=" & Sheet5.Range("f3").Value
    End If

'   Filter by Width

    If Sheet5.Range("f4").Value <> "" Then
        .AutoFilter field:=9, Criteria1:="<=" & Sheet5.Range("f4").Value
    End If

'   Filter by Max Weight

    If Sheet5.Range("f6").Value <> "" Then
        .AutoFilter field:=6, Criteria1:=">=" & Sheet5.Range("f6").Value
    End If

'   Filter by Length

    If Sheet5.Range("f5").Value <> "0" Then
        .AutoFilter field:=11, Criteria1:=">=" & Sheet5.Range("f5").Value
    End If


End With

    Application.DisplayAlerts = True
    Application.ScreenUpdating = True

End Sub
以下是零件号列表的示例:

INDEX   Customer    PartNumber      PartDesc                    MaterialType    MaxWgt  MinGage MaxGage Width   WidthTolerance  Length 
1       B1          .0138 X 2.161"  CR .0150 X 2.161            CR              3500    0.0142  0.0165  2.161   +/- 0.006       0
15      E1          .050 X 2.995    .050 X 2.995"               HR              3363    0.05    0.058   2.995   +/- 0.005       0
27      C1          04518G48        HD G60 CTD .045M X 48 X C   GALV            18000   0.044   0.049   48.124  -3              0
为了清晰起见,请使用图像

库存清单示例

零件号表和规格的示例

请对此进行测试,并检查您提供的规则是否正确。在您发送的文件中,没有匹配项

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
私有子目录解释()
Dim strFoldPath作为字符串,w作为工作簿,wInv作为工作簿,shI作为工作表
将wSpec设置为工作簿、shS设置为工作表、boolInv设置为布尔值、boolSpec设置为布尔值
Dim strSpec作为字符串,strInv作为字符串,arrInv作为变量,arrSp作为变量,arrRez()作为字符串
暗i一样长,s一样长,strclass一样长
strFoldPath=“您的文件夹路径”
strSpec=strFoldPath&“\”和“Specification1.txt”使用您的文件名
strInv=strFoldPath&“\”和“Inventory1.txt”使用您的文件名
对于工作簿中的每个w,检查是否在Excel中打开了必要的.txt/.csv文件:
如果w.FullName=strSpec,则设置wSpec=w:boolSpec=True
如果w.FullName=strInv,则设置wInv=w:boolInv=True
下一个
如果不是布林夫那么
如果Dir(strInv)“,则检查文件是否存在
设置wInv=工作簿。打开(strInv)
其他的
MsgBox“文件夹“”&strFoldPath&“”中没有库存文件”:退出子文件夹
如果结束
如果结束
如果不是boolSpec,则“如果规范文件未在Excel中打开
如果Dir(strSpec)“,则检查文件是否存在
设置wSpec=工作簿。打开(strSpec)
其他的
MsgBox“文件夹“”&strFoldPath&“”中没有规范文件”:退出子文件夹
如果结束
如果结束
设置shI=wInv.Sheets(1):设置shS=wSpec.Sheets(1)
arriv=shI.Range(“A1”).CurrentRegion.Value:'Debug.Print UBound(arriv,1),UBound(arriv,2)
arrSp=shS.Range(“A1”).CurrentRegion.Value:'Debug.Print UBound(arrSp,1),UBound(arrSp,2)
雷迪姆·阿雷兹(UBound(Arriv,1))
“做真正的工作:
shI.Cells(1,UBound(arriv,2)+2).entireclumn.Clear'清除返回数据的列
对于i=2到uBond(arriv,1)
strclass=arrInv(i,3)'库存数组的第3列
对于s=2至UBound(arrSp,1)
如果arrSp(s,6)=strclass,则
停止
如果CDbl(arriv(i,4))>=CDbl(arrSp(s,8))和_
CDbl(arriv(i,4))=CDbl(arrSp(s,10))和_
CDbl(arriv(i,7))=CDbl(arrSp(s,8))
调试.打印CDbl(arriv(i,4))=CDbl(arrSp(s,10))

事实证明,使用每个条件的匹配索引打印CDbl(arriv(i,7))非常有效。它能够评估每个参数,并在所有条件都为真时显示一个值,而不是在清单中循环。
谢谢你的帮助

评论不用于扩展讨论;这段对话已经结束。
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