如果找到EXACT MATCH,则需要返回值

时间:2017-08-01 19:36:15

标签: excel excel-vba vba

快速提问..我知道它必须简单..但是我想改变下面的代码,当在电子表格1中找到一个精确匹配时,单元格的范围(列A,列2到最后一行)在电子表格2(列A,从第2行开始,到最后一行,逐个单元格循环)中,它将电子表格1中的值“1”返回到由变量指定的列,(因为它对应于一个月)用户在表格/组合框中选择。)

我需要它才能成为一个精确的比赛。这有效..但看起来并不完全匹配。我不能有偏斜的结果,因为这些是零件编号。换一种说法.. 12345-6应该不匹配12345-61,这就是正在发生的事情,因为我不知道如何匹配VBA中的整个单元格内容..我知道它是LookIn,LookAt,但我不知道如何合并使用此变量在我现有代码中的正确语句。

Dim CycleCountCell As Integer
Dim wb1 As Workbook
Dim wb2 As Workbook
Dim sht1 As Worksheet
Dim sht As Worksheet
Dim lRow As Long
Dim rng As Range

Set wb1 = Workbooks(vFileName1) 'ABC Matrix File
Set wb2 = Workbooks(vFileName2) 'ABC Transacations by Item
Set sht = wb1.Worksheets(1) 'ABC Matrix File
Set sht1 = wb2.Worksheets(1) 'ABC Transactions by Item

lRow = sht.Cells(sht.Rows.Count, 1).End(xlUp).Row

Select Case ABCMatrixMonthSelect.ComboBox1.value
    Case "January": CycleCountCell = 22
    Case "February": CycleCountCell = 24
    Case "March": CycleCountCell = 26
    Case "April": CycleCountCell = 4
    Case "May": CycleCountCell = 6
    Case "June": CycleCountCell = 8
    Case "July": CycleCountCell = 10
    Case "August": CycleCountCell = 12
    Case "September": CycleCountCell = 14
    Case "October": CycleCountCell = 16
    Case "November": CycleCountCell = 18
    Case "December": CycleCountCell = 20
End Select

'Execute Match (Vlookup)
Msgbox "Preparing update to Cycle Count for " & ABCMatrixMonthSelect.ComboBox1.value & ".", vbInformation, "ABC Matrix Macro"

'Display customized message on Excel Status Bar letting user know lookup is in progress

Application.StatusBar = "ABC Matrix Macro is updating all " & lRow & " " & "cells within" & ABCMatrixMonthSelect.ComboBox1 & "column with Cycle Count.  " & "This process may take a few minutes..."

On Error Resume Next
For i = 2 To lRow
If sht.Cells(i, 1).value <> "" Then
    Set rng = sht1.Range("A:A").Find(sht.Cells(i, 1).value)
    If Not rng Is Nothing Then
         sht.Cells(i, CycleCountCell).value = ("1")
    End If
End If
Next

1 个答案:

答案 0 :(得分:0)

未经测试但可能更快地运作:

Dim d, m

With sht
    d = .Range(.Cells(2, "A"), .Cells(lRow, "A")).Value
End With

For i = 1 To UBound(d, 1) '<<< EDIT
    If Len(d(i, 1)) > 0 Then
        m = Application.Match(d(i, 1), sht1.Range("A:A"), 0)
        d(i, 1) = IIf(IsError(m), "", "1")
    End If
Next

sht.Cells(2, CycleCountCell).Resize(UBound(d, 1), 1).Value = d
相关问题