用于Vlookup的Excel VBA

时间:2018-01-23 03:16:56

标签: vba vlookup

我是VBA的新手,通常从在线获取VBA代码以自动化手动作品。我正在寻找vlookup的VBA代码。我已经记录了一个包含356行记录的宏,但是当我下次使用较少的行数运行宏时。它仍然在查找356.如何仅查找在参考单元格中具有值的单元格。请建议

1 个答案:

答案 0 :(得分:0)

好吧,既然你没有在这里发布你的代码,我所能做的就是猜你想要做什么。在实现vlookup方法之前,您可能需要找到最后使用过的行。这样的事情会有所帮助。

Sub FindingLastRow()
'PURPOSE: Different ways to find the last row number of a range
'SOURCE: www.TheSpreadsheetGuru.com

Dim sht As Worksheet
Dim LastRow As Long

Set sht = ActiveSheet

'Using Find Function (Provided by Bob Ulmas)
  LastRow = sht.Cells.Find("*", searchorder:=xlByRows, searchdirection:=xlPrevious).Row

'Using SpecialCells Function
  LastRow = sht.Cells.SpecialCells(xlCellTypeLastCell).Row

'Ctrl + Shift + End
  LastRow = sht.Cells(sht.Rows.Count, "A").End(xlUp).Row

'Using UsedRange
  sht.UsedRange 'Refresh UsedRange
  LastRow = sht.UsedRange.Rows(sht.UsedRange.Rows.Count).Row

'Using Table Range
  LastRow = sht.ListObjects("Table1").Range.Rows.Count

'Using Named Range
  LastRow = sht.Range("MyNamedRange").Rows.Count

'Ctrl + Shift + Down (Range should be first cell in data set)
  LastRow = sht.Range("A1").CurrentRegion.Rows.Count

End Sub

enter image description here

enter image description here

Sub Macro1()

Dim sht As Worksheet
Dim LastRow As Long
Set sht = ActiveSheet

LastRow = sht.Cells(sht.Rows.Count, "E").End(xlUp).Row

    Range("F1").Select
    ActiveCell.FormulaR1C1 = "=VLOOKUP(RC[-1],C[-5]:C[-4],2,FALSE)"
    Range("F1").Select
    Selection.AutoFill Destination:=Range("F1:F" & LastRow)

End Sub