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

Excel VBA using Combobox with Index Match

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

我希望循环填充 "Found" Type、Serial、MakeModel、Location 和 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.
BASELINEAssetID 也没有设置。

如果 BASELINEAssetID 是命名范围,则不能像在 Application.Index 或 Application.Match.
中那样使用它 您需要将其作为对象而不是像这样的字符串传递:

Set BASELINE = ThisWorkbook.Names("BASELINE").RefersToRange
Set AssetID = ThisWorkbook.Names("AssetID").RefersToRange

然后你可以在Application.IndexMatch中这样使用它:

With Application
    FoundType = .Index(BASELINE, .Match(Me.ComboScanTag.Value, AssetID, False), 3)
End With