简体   繁体   English

如何在Vlookup VBA中使用lastRow

[英]How to use lastRow in Vlookup VBA

I currently have three sheets called, Tracker , SIM Data , and Site Data . 我目前有三张名为TrackerSIM DataSite Data表格。 On the SIM Data sheet I have used the Vlookup function in VBA for columns G3 , H3 , and I3 to find the the site type, email, and site from the sheet Site Data that works just fine. SIM Data表中,我使用了VBA中的Vlookup功能,用于列G3H3I3 ,从工作表中的Site Data中找到站点类型,电子邮件和站点。 Once the data is organized, I'm transferring that information onto the Tracker sheet which also works just fine. 一旦数据被组织起来,我就会将这些信息传输到Tracker表,这也很好。

My problem is that once the information is transferred onto the Tracker sheet, if I want to add any additional data it replaces the top data rather than starting from the bottom data that is already on the tracker. 我的问题是,一旦将信息传输到Tracker表,如果我想添加任何其他数据,它将替换顶部数据,而不是从已经在跟踪器上的底部数据开始。 I am aware that lastRow can fix this issue but I'm not sure on how to input it (or where with the code I already have). 我知道lastRow可以解决这个问题,但我不确定如何输入它(或者我已经拥有的代码在哪里)。 Any help would be appreciated. 任何帮助,将不胜感激。

'SIM Data Macro
Sub SIM_Macro()
Dim lr As Long
Dim lr1 As Long
Dim lr2 As Long
 'Dim Gcell As Range
 'Dim lLastRow As Long
 'Dim LResult As String

lr = Sheets("SIM Data").Range("A" & Rows.Count).End(xlUp).Row

 'Select ranges and delete

    Range("A:A,H:J").Delete

 'Type in values in G3 and H3

    Range("$G$3").Value = "Site Type"
    Range("$H$3").Value = "Email"
    Range("$I$3").Value = "Site"

    Cells.EntireColumn.AutoFit

    Range("G4").Formula = "=VLOOKUP(LEFT(RC[-3],4),'Site Data '!C[-6]:C[4],2,FALSE)"

    Range("G4").AutoFill Destination:=Range("G4:G" & lr)

'VlookUp for email tab

    Range("H4").Formula = "=VLOOKUP(LEFT(RC[-4],4),'Site Data '!C[-7]:C[4],10,FALSE)"

    Range("H4").AutoFill Destination:=Range("H4:H" & lr)

'LEFT Formula for Site

    Range("I4").Formula = "=LEFT(RC[-5],4)"

    Range("I4").AutoFill Destination:=Range("I4:I" & lr)

    Cells.EntireColumn.AutoFit

    Range("G4").Sort key1:=Range("G4:G" & lr), _
    order1:=xlDescending, Header:=xlYes, _
    OrderCustom:=1, MatchCase:=False, _
    Orientation:=xlTopToBottom


' Keep the formulas on the tracker tab untouched.

    'Site Formula
    Sheets("Tracker").Activate


  Sheets("Tracker").Range("A5").Formula = "=INDEX('SIM Data'!A4:I100,1,9)"
  Range("A5").AutoFill Destination:=Range("A5:A" & lr + 1)


'SEV2 Formaul on Tracker sheet

    Sheets("Tracker").Range("B5").Formula = "=INDEX('SIM Data'!A4:H100,1,2)"
    Range("B5").AutoFill Destination:=Range("B5:B" & lr + 1)

' Formula for Site Type in Tracker Tab

    Sheets("Tracker").Range("F5").Formula = "=INDEX('SIM Data'!A4:H100,1,7)"
    Range("F5").AutoFill Destination:=Range("F5:F" & lr + 1)

' Formula for SIM in Tracker Tab

    Sheets("Tracker").Range("G5").Formula = "=INDEX('SIM Data'!A4:H100,1,1)"
    Range("G5").AutoFill Destination:=Range("G5:G" & lr + 1)

'Formula for Email

    Sheets("Tracker").Range("H5").Formula = "=INDEX('SIM Data'!A4:H100,1,8)"
    Range("H5").AutoFill Destination:=Range("H5:H" & lr + 1)

'Add hyperlinks to G (Sim) Column

lr1 = Sheets("Tracker").Range("G" & Rows.Count).End(xlUp).Row

    Sheets("Tracker").Range("G5:G" & lr1).copy
    Sheets("Tracker").Range("G5").PasteSpecial xlPasteValues

    Range("G5").Select
    For i = 5 To lr
        ActiveCell.Hyperlinks.Add Anchor:=Selection, Address:=ActiveCell.Offset(5, 7).Value
        ActiveCell.Offset(6, 7).Select
    Next

'Add hyperlinks to H (Email) Column

lr2 = Sheets("Tracker").Range("H" & Rows.Count).End(xlUp).Row

    Sheets("Tracker").Range("H5:H" & lr2).copy
    Sheets("Tracker").Range("H5").PasteSpecial xlPasteValues

    Range("H5").Select
    For i = 5 To lr
        ActiveCell.Hyperlinks.Add Anchor:=Selection, Address:=ActiveCell.Offset(5, 7).Value
        ActiveCell.Offset(6, 7).Select
    Next

    Application.CutCopyMode = False

    Range("A4").Select

End Sub

I ended up figuring it out. 我最终搞清楚了。

Dim r As Range 昏暗作为范围

Set r = Worksheets("SIM Data").Range("A4:A" & Application.Match("*", Worksheets("SIM Data").Range("G:G"), -1)) Set r2 = Worksheets("Tracker").Range("G" & Rows.Count).End(xlUp).Offset(1, 0) r.Copy r2.PasteSpecial xlValues 设置r =工作表(“SIM数据”)。范围(“A4:A”和Application.Match(“*”,工作表(“SIM数据”)。范围(“G:G”), - 1))设置r2 =工作表(“跟踪器”)。范围(“G”和Rows.Count).End(xlUp)。偏移(1,0)r.Copy r2.PasteSpecial xlValues

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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