【问题标题】:Excel reporting - function hlookup doesn't work in nested for loopExcel 报告 - 函数 hlookup 在嵌套 for 循环中不起作用
【发布时间】:2021-06-29 20:50:41
【问题描述】:

我有一个问题。我正在尝试使用 hlookup 函数按 item_id 匹配特定值。但是这个函数不返回指定的值。 这是我的宏的代码:

Sub create_report()

Dim itemWs As Worksheet, offerWs As Worksheet, testWs As Worksheet
Dim itemLastRow As Long, offerLastRow As Long
Dim offerLastCol As Long, itemLastCol As Long
Dim dataRng As Range

Set itemWs = ThisWorkbook.Worksheets("nn_rfx_compare_per_lot")

Set offerWs = ThisWorkbook.Worksheets("Offers")

Set testWs = ThisWorkbook.Worksheets("Testowy")

itemLastRow = itemWs.Range("A" & Rows.Count).End(xlUp).Row
offerLastRow = offerWs.Range("A" & Rows.Count).End(xlUp).Row

offerLastCol = offerWs.Cells(1, Columns.Count).End(xlToLeft).Column
itemLastCol = itemWs.Cells(1, Columns.Count).End(xlToLeft).Column

Set dataRng = testWs.Range("I3:AF" & 4)


'For x = 2 To 7
    'On Error Resume Next
    'itemWs.Range("I" & x).Value = Application.WorksheetFunction.VLookup(itemWs.Range("C" & x).Value & itemWs.Range("B" & x).Value, dataRng, 3, 0)
'Next x



Sheets("Testowy").Range(Sheets("Testowy").Cells(offerLastCol - 1, 1), Sheets("Testowy").Cells(itemLastRow + 4, itemLastCol)) = _
Sheets("nn_rfx_compare_per_lot").Range(Sheets("nn_rfx_compare_per_lot").Cells(1, 1), Sheets("nn_rfx_compare_per_lot").Cells(itemLastRow, itemLastCol)).Value

Sheets("Testowy").Range(Sheets("Testowy").Cells(1, itemLastCol), Sheets("Testowy").Cells(offerLastCol - 2, offerLastRow - 2)) = _
WorksheetFunction.Transpose(Sheets("Offers").Range(Sheets("Offers").Cells(1, 2), Sheets("Offers").Cells(offerLastRow, offerLastCol - 1)))

Dim lastTestCol As Long
lastTestCol = testWs.Cells(1, Columns.Count).End(xlToLeft).Column

Dim ColumnLetter As String

For Row = 6 To 11

    For Col = 9 To lastTestCol
    On Error Resume Next
        testWs.Cells(Row, Col).Value = Application.WorksheetFunction.Index(testWs.Range( _
        "I4:AF4"), WorksheetFunction.Match(testWs.Cells(Row, 3).Value, testWs.Cells(3, Col), 0))
        
        'Match(testWs.Cells(Row, 3), dataRng, 1)
        
        'HLookup(testWs.Cells(Row, 3), dataRng, 2, 0)
        
    Next Col

Next Row

结束子

在此链接中显示了我要组织的报告 enter image description here

【问题讨论】:

  • 尝试更改Set dataRng = testWs.Range("I3:AF" & 4)。您正在查找的行必须是搜索范围内的第一行
  • 好的,这是工作,但它返回所有记录的重复 prop_id。如果你有任何想法,我会很高兴
  • 您想获取sup_id 行值吗?
  • 是的,我也想消除重复。我不知道 hlookup 是否适合我的问题
  • 要查找列,可以使用Application.Match(),然后选择找到的列中的单元格和所需的行。您可以使用内置的Collection 对象或Dictionary object 来检测重复项

标签: vba


【解决方案1】:

任务和条件不完全清楚(重复怎么办,是否会出现,item_id是否唯一等等)。 例如,如果需要选择item_id对应的sup_id,可以通过以下代码完成:

Set item_id_rng = testWS.Range("I3:AF3")
For Row = 6 To 11
    ' search `item_id` in Range("I3:AF3")
    find_col = Application.Match(testWS.Cells(Row, 3).Value, item_id_rng, 0)
    If IsNumeric(find_col) Then ' if found, get correspondent value from  correspondent row
        'output to 9 column (empty area), for example
        testWS.Cells(Row, 9).Value = item_id_rng(1).Offset(-1, find_col - 1)
    End If
Next Row

作为一个整体的任务,如果你制定任务的条件并放置一个结果的图像就好了

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-09-29
    • 1970-01-01
    • 1970-01-01
    • 2019-01-19
    • 1970-01-01
    • 2018-12-03
    相关资源
    最近更新 更多