使用带有索引匹配的组合框的Excel VBA

使用带有索引匹配的组合框的Excel VBA,vba,indexing,match,Vba,Indexing,Match,我有一个Excel VBA用户表单组合框,用于扫描资产标签,以便与Sheet1中的站点基线进行比较。最多可以有50000多个资产。命名的范围都是正确的 我希望循环填充找到的类型、序列、MakeModel、位置和PrinterHost的资产属性文本框 下面的代码没有额外的索引匹配查找额外的资产属性,因为过程是相同的。感谢您的帮助,因为我不确定我会错在哪里。提前谢谢 Private Sub ComboScanTag_Change() Dim x As Integer Dim AssetCount

我有一个Excel VBA用户表单组合框,用于扫描资产标签,以便与Sheet1中的站点基线进行比较。最多可以有50000多个资产。命名的范围都是正确的

我希望循环填充找到的类型、序列、MakeModel、位置和PrinterHost的资产属性文本框

下面的代码没有额外的索引匹配查找额外的资产属性,因为过程是相同的。感谢您的帮助,因为我不确定我会错在哪里。提前谢谢

Private Sub ComboScanTag_Change()

Dim x As Integer
Dim AssetCount As Long
Dim BASELINE As Range
Dim AssetID As Range
Dim FoundType As Variant
Dim FoundSerial As Variant
Dim FoundMakeModel As Variant
Dim FoundLocation As Variant
Dim FoundPrinterHostName As Variant

If Me.ComboScanTag.Value = "" Then                                      'ScanTag has no value
MsgBox "Asset not Found - Re-Scan or enter New Asset details"
Me.ComboScanTag.SetFocus
End If
If Me.ComboScanTag.Value <> "" Then                                     'ScanTag has a value
Application.ScreenUpdating = False                                      'Turn off screen updating to speed app
For x = 1 To AssetCount                                                 'Number of loop iterations from Baseline Assets Count D1 cell
    FoundType = Application.Index("BASELINE", Application.Match(Me.ComboScanTag.Value, "AssetID", False), 3)
        If Not IsError(FoundType) = False Then                          'if error value in lookup return 0
            Me.txtFoundType.Value = FoundType                           'Fill textbox FoundType with lookup value from baseline
        Else
    On Error GoTo 0                                                         'reset error handler
FoundSerial = Application.Index("BASELINE", Application.Match(Me.ComboScanTag.Value, "AssetID", False), 11)
    If Not IsError(FoundSerial) = False Then
Me.txtFoundSerial.Value = FoundSerial

    End If
Next x
End If
Application.ScreenUpdating = True

End Sub
AssetCount未初始化。在像AssetCount=10那样使用它之前,需要对其进行初始化。 基线和AssetID也未设置

如果基线和AssetID是命名范围,则不能像在Application.Index或Application.Match中那样使用它。 您需要将其作为对象而不是字符串传递,如下所示:

Set BASELINE = ThisWorkbook.Names("BASELINE").RefersToRange
Set AssetID = ThisWorkbook.Names("AssetID").RefersToRange
然后您可以在应用程序中使用它。索引和匹配:


感谢您分享您的知识-这对我帮助很大,因为我看到的大多数线程在使用索引和匹配在VBA中执行查找时都没有提到作为对象传递
With Application
    FoundType = .Index(BASELINE, .Match(Me.ComboScanTag.Value, AssetID, False), 3)
End With