excel 匹配不同列中的值,并将匹配项放置在单独的列中,VBA?

0kjbasz6  于 2023-08-08  发布在  其他
关注(0)|答案(2)|浏览(107)

这就是目前的数据。列sku具有~2K行。列image具有~ 8 k行。我删除了图像文件名的文件扩展名和前导零,所以我可以更容易地匹配。
x1c 0d1x的数据
大部分的图像都不需要,只有第一场比赛是我需要的。所以你在上面的例子中看到,列sku/image之间有一个匹配-- function将在中间列中传递值。
所需的结果如下所示,请注意05099.jpg&05103.jpg存在于image列中:



我在SO上得到了另一个人的帮助,他们提供了这个函数,但是字典没有填充中间列的值。我完全搞不懂为什么,一开始是有效的。

Function getSKUFromFilename(filename As String) As Long
    getSKUFromFilename = Val(filename)
End Function

Sub FillImageNames()
Dim ws As Worksheet, imageArr()
Set ws = ThisWorkbook.Worksheets("Sheet1")

imageArr = ws.Range("C2:C" & ws.Cells(ws.Rows.Count, "A").End(xlUp).row)

' (1) Fill Dictionary
Dim d As New Dictionary
Dim row As Long
For row = 1 To UBound(imageArr, 1)
    Dim filename As String, sku As Long
    filename = Trim(imageArr(row, 1))
    If filename <> "" Then
        sku = getSKUFromFilename(filename)
        d(sku) = filename
    End If
Next

' (2) Fill sheet
Dim lastRow as Long
lastRow = ws.Cells(ws.Rows.Count, "A").End(xlUp).row
For row = 2 To lastRow
    sku = Val(ws.Cells(row, 1))
    If d.Exists(sku) Then
        ws.Cells(row + 1, 2) = d(sku)
    End If
Next
End Sub

字符串

3phpmpom

3phpmpom1#

匹配转换数据


的数据

Excel公式

=IFNA(INDEX(C2:C8,MATCH(A2:A6,
    VALUE(LEFT(C2:C8,FIND(".",C2:C8)-1)),0)),"Nope")

个字符

VBA

Function GetSkuFromImageName(ByVal Imagename As String) As Long
    GetSkuFromImageName = Val(Imagename)
End Function

Sub MatchImageNames()
    
    Dim ws As Worksheet: Set ws = ThisWorkbook.Worksheets("Sheet1")
    
    Dim srrg As Range: ' Source Return Range
    Set srrg = ws.Range("C2", ws.Cells(ws.Rows.Count, "C").End(xlUp))
    Dim sData(): sData = srrg.Value ' assuming more than one cell
    
    Dim sDict As Object: Set sDict = CreateObject("Scripting.Dictionary")
    
    Dim r As Long, sNumber As Long, sString As String
    
    For r = 1 To UBound(sData, 1)
        sString = CStr(sData(r, 1))
        sNumber = GetSkuFromImageName(sString)
        If sNumber > 0 Then
            If Not sDict.Exists(sNumber) Then ' ensuring first match
                sDict(sNumber) = sString
            End If
        End If
    Next r
            
    ' The keys of the dictionary hold the lookup data (numbers)
    ' while the corresponding items hold the return data (strings):
    Erase sData
            
    Dim dlrg As Range: ' Destination Lookup Range
    Set dlrg = ws.Range("A2", ws.Cells(ws.Rows.Count, "A").End(xlUp))
    Dim dData(): dData = dlrg.Value ' assuming more than one cell
    
    Dim dValue, IsFound As Boolean
    
    For r = 1 To UBound(dData, 1)
        dValue = dData(r, 1)
        If VarType(dValue) = vbDouble Then ' is a number
            If sDict.Exists(dValue) Then
                IsFound = True
            End If
        End If
        ' Using the same array for the results.
        If IsFound Then
            dData(r, 1) = sDict(dValue)
            IsFound = False
        Else
            dData(r, 1) = "Nope" ' or Empty, or...
        End If
    Next r
    
    Dim drrg As Range: ' Destination Return Range
    Set drrg = dlrg.EntireRow.Columns("B")
    
    drrg.Value = dData
    
    MsgBox "Image names matched.", vbInformation
    
End Sub

b09cbbtk

b09cbbtk2#

本质上,您希望比较一组值对,如果这些值对相等,则将一些信息写入工作表。
您提到您正在处理大量数据,因此我建议将数据存储在一个数组中,完成所有查找匹配项的工作,然后将所有内容写回工作表。这比在Excel中逐行比较要快得多。
下面是一个简单的例子:


的数据

Private Sub FillMatches()

    ' Store data in an array
    Dim RangeData As Range
    Set RangeData = ThisWorkbook.Sheets("Sheet1").Range("A2:D4")
    
    Dim ArrayImageInfo()
    ArrayImageInfo() = RangeData.Value
    
    ' Iterate through data and check for matches
    Dim ii As Long
    For ii = 1 To UBound(ArrayImageInfo, 1)
    
        If ArrayImageInfo(ii, 1) = ArrayImageInfo(ii, 3) Then
            ArrayImageInfo(ii, 2) = ArrayImageInfo(ii, 4)
        End If
    
    Next ii

    ' Write the data back to the sheet
    RangeData.Value = ArrayImageInfo
    
End Sub

字符串
结果是:


相关问题