0

I want to do lookup in a specified range(rows X to AF in "main" tab[Sheet1]) which I am doing using VBA hlookup function. The problem I am facing is that I am not able to do this lookup in a loop, which means once the hlookup is done in X2:AF2, then it should do the calculation in X3:AF3 for next row.

I need to do this because the Tablehandle[sheeet2] result will change every time (macro will clear this sheet) and the headers will not in order.

So can someone help me to get hlookup in a loop for a specified row?

My "Main" sheet enter image description here

"TableHandle" sheet enter image description here

Option Explicit
Sub hlookup1()

Dim i, r As Long
For i = 1 To Range("K100000").End(xlUp).Row - 1

'first macro will get the table inside sheet ...

Sheets("TableHandle").Select
    'Range("A2").Select
    'Range(Selection, Selection.End(xlDown)).Select
    'Range("A2:B10").Select
    'Selection.Copy
    Range("F1").Select
    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
        :=False, Transpose:=True

'hlookup
    Sheets("Main").Select
    Range("X2").Select
    Range("X" & i + 1).Select
    ActiveCell.FormulaR1C1 = "=HLOOKUP(R1C,TableHandle!R1C6:R2C14,2,0)"
    Selection.Copy
    Range("X2:AF2").Select  'PROBLEM from Here, it will again calculate in x2 to af2 range)
    ActiveSheet.Paste
    Application.CutCopyMode = False
    Selection.Copy
    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
        :=False, Transpose:=False
            Cells.Replace What:="#N/A", Replacement:="", LookAt:=xlPart, SearchOrder _
        :=xlByRows, MatchCase:=False, SearchFormat:=False, ReplaceFormat:=False
    i = i + 1
    
    Next
    
End Sub
4

2 回答 2

0

Yours is a good idea but in practice it has a number of drawbacks as demonstrated by the error handling you had planned. I think you would also have to replace the formulas you insert with so much effort with the values they produce. Please try this code instead. It transfers data from the TableHandler sheet to the Main without the use of worksheet formulas.

Sub hlookup1()
    ' 211

    Dim Data        As Variant                  ' source data
    Dim Headers     As Range                    ' column captions in 'Main'
    Dim Fnd         As Range                    ' Find the column caption
    Dim Rs          As Long                     ' Row: Source (= 'Data')
    Dim Cs          As Long                     ' Column: Source
    Dim Rt          As Long                     ' Row: Target
    Dim Ct          As Long                     ' Column: Target
    
    'first macro will get the table inside sheet ...
    With Worksheets("TableHandle")              ' data source
        Rs = .Cells(.Rows.Count, "A").End(xlUp).Row
        Data = .Range(.Cells(2, "A"), .Cells(Rs, "X")).Value
        ' Row 1 of 'Data' holds the column headers because
        ' SheetRow(2) became the ArrayRow(1)
    End With
    
    Application.ScreenUpdating = False          ' speed up execution
    With Worksheets("Main")
        Set Headers = .Range("A2:X2")
        
        ' start in array row 2 because array row 1 holds the column captions
        For Rs = 2 To UBound(Data)              ' loop through all rows
            Rt = .Cells(.Rows.Count, "A").End(xlUp).Row + 1
            For Cs = 1 To UBound(Data, 2)       ' loop through the columns of each row
                Set Fnd = Headers.Find(Data(1, Cs), LookIn:=xlValues, lookat:=xlWhole)
                If Not Fnd Is Nothing Then      ' skip column not found
                    .Cells(Rt, Fnd.Column).Value = Data(Rs, Cs)
                End If
            Next Cs
        Next Rs
    End With
    Application.ScreenUpdating = True
End Sub

If a column in the TableHandler should not be found the macro will not transfer data, leaving the target column blank. If you want a warning it would have to be added. There is a little confusion in your question as to rows and columns. Should I have guessed wrongly here or there I trust you will be able to make the required modifications. However, you are welcome to ask for help, too.

于 2021-04-03T08:33:26.563 回答
0

it was important and i spent all day to make this possible, i ans my question here.

the code is shortned as below, it works fast and efficient. thanks for suggestions!

    Dim Hlookp As Variant
    Hlookp = Application.HLookup(Range("B1:J1").Value, Sheets("TableHandle").Range("F1:N" & Cells(Rows.Count, 1).End(xlUp).row), 2, False)
    Range("B" & i + 1 & ":J" & i + 1).Value = Hlookp
于 2021-04-03T12:48:30.513 回答