簡體   English   中英

Excel VBA使用帶有索引匹配的組合框

[英]Excel VBA using Combobox with Index Match

我有一個Excel VBA UserForm組合框,用於掃描資產標簽以與Sheet1中保存的網站基准進行比較。 最多可以有50,000個以上的資產。 命名范圍都是正確的。

我希望循環填充Type,Serial,MakeModel,Location和PrinterHost的“ Found”資產屬性文本框。

下面的代碼沒有針對額外資產屬性的額外索引匹配查找,因為該過程將是相同的。 感謝您的幫助,因為我不確定我要去哪里出錯。 提前致謝。

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM