简体   繁体   中英

Vlookup Excel VBA

I need help on simple macro that will replicate a vlookup function. So, I have a table of values from H2:H9 in Sheet1 A value in H2 corresponds to the entire Column D in sheet 3 and the value needs to be copy/pasted into every cell in Column D from row 2 to the last row with data. Same for H3, H3 corresponds to the entire Column AC and a value in H3 must be copy/pasted into every cell in Column AC in sheet 3 from row 2 to the last row. I am using this code for figuring out the last line. Can anyone please please help me?

Dim lastRow As Long
Dim rng As Range

Set rng = ThisWorkbook.Sheets(2).Cells

lastRow = rng.Find(What:="*", After:=rng.Cells(1), Lookat:=xlPart, LookIn:=xlFormulas, SearchOrder:=xlByRows, SearchDirection:=xlPrevious, MatchCase:=False).Row

That simple line should help you find the last used row in your sheet.

lastRow= ActiveWorkbook.Sheets(3).UsedRange.Rows.Count

About the rest, is that what you mean?

sheets(3).range("D2:D" & lastRow).value = sheets(1).range("H2").value
sheets(3).range("AC2:AC" & lastRow).value = sheets(1).range("H3").value

Something like that

' Find Last row in Sheet1 in Column H
LastRow = ThisWorkbook.Worksheets(1).Cells(ThisWorkbook.Worksheets(1).Rows.Count, "H").End(xlUp).Row
' Get array of range
vArr = ThisWorkbook.Worksheets(1).Range("H2:I" & LastRow).Value
ThisWorkbook.Worksheets(3).Range("D2:D9").Cells.Resize(UBound(vArr), 1) = vArr

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM