Excel VBA - vlookup,用于宏中的可变行数

时间:2018-06-13 15:21:20

标签: excel excel-vba excel-formula vba

我已经创建了一个宏来组织数据集并以一种更有意义进行分析的方式编译成另一张表。该集最初由用户,时间戳和3个可能事件的列组成。用户可能出现在多行上,但我希望用户查看此数据集,并为每个时间戳分别设置一列。我创建的宏可以成功清理,按事件类型过滤,并按事件类型分成单独的工作表(无论有多少行数据),但我在将数据编译成一个数据时遇到问题使用vlookup并计算可变行数的工作表。我已经查看了这个问题的其他答案并尝试了这个:

ActiveCell.FormulaR1C1 =" = VLOOKUP(RC [-2],'电子邮件已打开'!R1C1:R"& LastRow0&" C3,2,FALSE )"

......但它一直给我错误。

我在下面的内容(Vlookup_events2)可以正常工作,但不适用于整个可变行数。请帮我调整vlookup的代码,以便无论行数多少都能正常工作。

以下是分离数据的代码(仅供参考),然后是问题宏 - 用vlookup编译它。我真的很感激一些帮助,我知道那里有一位了不起的VBA专家!

    Sheets.Add
    Sheets("Sheet1").Name = "Email Sent"
    ActiveSheet.Next.Select
    Selection.AutoFilter
    ActiveCell.Offset(0, 2).Range("A1").Select
    ActiveSheet.Range("$A$1:$D$1000000").AutoFilter Field:=3, Criteria1:= _
        "=Campaign Created", Operator:=xlOr, Criteria2:="=Email Sent"
    ActiveCell.Offset(0, -2).Range("A1:D2355").Select
    ActiveCell.Activate
    Selection.Copy
    ActiveSheet.Previous.Select
    Range("A1").Select
    ActiveSheet.Paste
    ActiveSheet.Next.Select
    Range("A1").Select
    ActiveSheet.Range("$A$1:$D$1000000").AutoFilter Field:=3, Criteria1:= _
        "=Campaign Created", Operator:=xlOr, Criteria2:="=Email Opened"
    Sheets.Add
    Sheets("Sheet2").Name = "Email Opened"
    ActiveSheet.Next.Select
    ActiveCell.Range("A1:D1000000").Select
    Application.CutCopyMode = False
    Selection.Copy
    ActiveSheet.Previous.Select
    Range("A1").Select
    ActiveSheet.Paste
    ActiveCell.Offset(0, 3).Range("A1").Select
    Application.CutCopyMode = False
    Selection.ClearContents
    ActiveCell.Columns("A:A").EntireColumn.Select
    Selection.Delete Shift:=xlToLeft
    Sheets.Add
    Sheets("Sheet3").Name = "Clicked Link"
    ActiveSheet.Next.Select
    ActiveSheet.Next.Select
    ActiveCell.Offset(0, 2).Range("A1").Select
    ActiveSheet.Range("$A$1:$D$1000000").AutoFilter Field:=3, Criteria1:= _
        "=Campaign Created", Operator:=xlOr, Criteria2:="=Clicked Link"
    ActiveCell.Offset(0, -2).Range("A1:D1000000").Select
    ActiveCell.Activate
    Selection.Copy
    ActiveSheet.Previous.Select
    ActiveSheet.Previous.Select
    Range("A1").Select
    ActiveSheet.Paste
    ActiveCell.Offset(0, 3).Columns("A:A").EntireColumn.Select
    Application.CutCopyMode = False
    Selection.Delete Shift:=xlToLeft
    Range("A1").Select
End Sub
Sub Vlookup_events2()
' Vlookup_events2 Macro

    ActiveSheet.Previous.Select
    Range("A2").Select
    ActiveCell.FormulaR1C1 = "user"
    Range("A3").Select
    ActiveSheet.Next.Select
    Range("A2").Select
    ActiveCell.FormulaR1C1 = "user"
    Range("A3").Select
    ActiveSheet.Next.Select
    Range("A2").Select
    ActiveCell.FormulaR1C1 = "user"
    Range("A3").Select
    Sheets.Add
    Sheets("Sheet4").Name = "Compiled Events"
    ActiveSheet.Previous.Select
    ActiveSheet.Previous.Select
    Range("A1").Select
    Range(Selection, Selection.End(xlToRight)).Select
    Range(Selection, Selection.End(xlDown)).Select
    Selection.Copy
    ActiveSheet.Next.Select
    ActiveSheet.Next.Select
    Range("A1").Select
    ActiveSheet.Paste
    Columns("C:D").Select
    Application.CutCopyMode = False
    Selection.Delete Shift:=xlToLeft
    Range("B1").Select
    ActiveCell.FormulaR1C1 = "Email Sent Time"
    Range("C1").Select
    ActiveCell.FormulaR1C1 = "Email Opened Time"
    Range("D1").Select
    ActiveCell.FormulaR1C1 = "Clicked Link Time"
    Range("A1").Select
    Application.Goto Reference:="R2C3"
    ActiveCell.FormulaR1C1 = "=VLOOKUP(RC[-2],'Email Opened'!R1C1:R601C3,2,FALSE)"
    Range("C3").Select
    Range(Selection, Selection.End(xlUp)).Select
    Columns("C:C").Select
    Selection.FormulaR1C1 = "=VLOOKUP(RC[-2],'Email Opened'!R1C1:R601C3,2,FALSE)"
    Columns("D:D").Select
    Selection.FormulaR1C1 = "=VLOOKUP(RC[-3],'Clicked Link'!R1C1:R56C3,2,FALSE)"
    Columns("C:C").Select
    Selection.NumberFormat = "m/d/yyyy h:mm"
    Columns("D:D").Select
    Selection.NumberFormat = "m/d/yyyy h:mm"
    Range("C1").Select
    ActiveCell.FormulaR1C1 = "Email Opened Time"
    Range("D1").Select
    ActiveCell.FormulaR1C1 = "Clicked Link Time"
    Range("C2").Select
End Sub

0 个答案:

没有答案