VBA代码,合并单元格后vlookup出现问题

问题描述 投票:0回答:1

我有一个 VBA 代码,必须从几个不同的文件组中查找数据并对它们进行排序。最后,如果来自的两个文件之间的数据不同,则有一列将数据与“/”组合起来。合并后的数据位于单元格 C 中,到目前为止一切都运行良好。 我想用其中一个文件中 Sheet2 中的另一个数据库检查 C 列,但是我的代码仅返回 NA。我哪里错了? 这些是在sheet2 中提及的内容,并使用下一张图片上的数据进行vlookup 编辑 B列中的代码被vlookup到Sheet2

这是我正在使用的代码。


Sub PerformVLookup(mention As String, lookupRange2 As Range, lookupRange4 As Range, lookupRange2Sheet2 As Range, wsOutput As Worksheet, outputRow As Long)
    Dim lookupValue1 As String
    Dim lookupValue2 As String ' For the value in column C
    Dim lookupResult2 As Variant
    Dim lookupResult4 As Variant
    Dim lookupResultSheet2 As Variant ' Result for lookup in Sheet2
    Dim combinedResult As String
    
    lookupValue1 = Application.WorksheetFunction.Trim(Application.WorksheetFunction.Clean(CStr(mention)))
   
    ' Perform the VLOOKUP for this mention in Workbook2
    On Error Resume Next
    lookupResult2 = Application.VLookup(lookupValue1, lookupRange2, 2, False)
    On Error GoTo 0
    
    If IsError(lookupResult2) Then
        lookupResult2 = "#N/A"
    End If
    
    
    ' Perform the VLOOKUP for this mention in Workbook4
    On Error Resume Next
    lookupResult4 = Application.VLookup(lookupValue1, lookupRange4, 2, False)
    On Error GoTo 0
    
    If IsError(lookupResult4) Then
        lookupResult4 = "#N/A"
    End If
    
   ' Combine the results
    combinedResult = ""
    If lookupResult2 <> "#N/A" Then combinedResult = lookupResult2
    If lookupResult4 <> "#N/A" Then
        If combinedResult <> "" And combinedResult <> lookupResult4 Then
            combinedResult = combinedResult & " \ " & lookupResult4
        ElseIf combinedResult = "" Then
            combinedResult = lookupResult4
        End If
    End If
    
    ' If no matches, display #N/A
    If combinedResult = "" Then
        combinedResult = "#N/A"
    End If
    
    ' Output the final combined result in Column C
    wsOutput.Cells(outputRow, 3).Value = combinedResult
    
    ' If the combined result has "\", apply the color fill #FDBAB5 (light red)
    If InStr(combinedResult, "\") > 0 Then
        wsOutput.Cells(outputRow, 3).Interior.Color = RGB(253, 186, 181)
    End If
    
    ' Now perform the VLOOKUP for the value in column C in Workbook2 Sheet2
     If Not IsEmpty(wsOutput.Cells(outputRow, 3).Value) Then
        lookupValue2 = CStr(wsOutput.Cells(outputRow, 3).Value) ' Ensure lookupValue2 is a string
    On Error Resume Next
    lookupResultSheet2 = Application.VLookup(lookupValue2, lookupRange2Sheet2, 2, False)
    On Error GoTo 0
    
    If IsError(lookupResultSheet2) Then
        lookupResultSheet2 = "#N/A"
    End If
    
    ' Output the result from Workbook2 Sheet2 in column D
    wsOutput.Cells(outputRow, 4).Value = lookupResultSheet2
    Else
        ' If column C is empty, output NA in column D
        wsOutput.Cells(outputRow, 4).Value = "#N/A"
    End If
End Sub

excel vba
1个回答
0
投票

您的计算

lastCol = wsSource.Cells(1, wsSource.Columns.Count).End(xlToLeft).Column

可能会阻止您查找任何数据。考虑更改为

lastCol = wsSource.Cells(1, wsSource.Columns.Count).End(xlToRight).Column

此更改允许我使用您的代码并在表 2 上生成结果。

© www.soinside.com 2019 - 2024. All rights reserved.