我需要在excel中重新格式化我的Vlookup公式

时间:2017-03-05 17:20:38

标签: vba excel-vba vlookup excel

我有一个VBA代码,如下所示,我录了它。在录制时,它以偏移格式编写了公式。所以我不知道如何改变范围

我怎样才能转换; R3C[-3]:R[1488]C[2]通常为B$3:G1489

为:

B $ 3 :(“G”栏中的最后一个活动单元格)

我该怎么做?

Sub duzenle()

Columns("E:E").Select
Selection.Copy
Range("L1").Select
ActiveSheet.Paste
Range("E12").Select
Application.CutCopyMode = False
ActiveCell.FormulaR1C1 = _
    "=IFERROR(VLOOKUP(RC[-3],C.A.!R3C[-3]:R[1488]C[2],4,0),IF(ISBLANK('2017'!RC[7]),"""",'2017'!RC[7]))"
Range("E12").Select
Selection.AutoFill Destination:=Range(ActiveCell, ActiveCell.End(xlDown)), Type:=xlFillDefault
Range(ActiveCell, ActiveCell.End(xlDown)).Select
Columns("E:E").Select
Selection.Copy
Range("E1").Select
ActiveSheet.Paste
Selection.PasteSpecial Paste:=xlValues, Operation:=xlNone, SkipBlanks:= _
    False, Transpose:=False
Columns("L:L").Select
Application.CutCopyMode = False
Selection.Delete Shift:=xlToLeft
Range("A1").Select
MsgBox "'C.A' Degerleri Guncellenmistir."

End Sub

1 个答案:

答案 0 :(得分:1)

您可以删除部分不必要的SelectSelection并替换您的前几行代码:

Columns("E:E").Select
Selection.Copy
Range("L1").Select
ActiveSheet.Paste

使用:

Columns("E:E").Copy Range("L1")

关于您的公式,您需要将FormulaR1C1替换为Formula,请参阅以下代码:

With Worksheets("C.A.")
    LastRow = .Cells(.Rows.Count, "G").End(xlUp).Row '<--get last row with data in Column G from "C.A." sheet
End With

Range("E12").Formula = _
    "=IFERROR(VLOOKUP(B3,C.A.!B$3:G" & LastRow & ",4,0)...' <-- the rest of your formula

编辑1 :刚刚更正了公式代码行:

Range("E12").Formula = "=IFERROR(VLOOKUP(B3,C.A.!B$3:G" & LastRow & ",4,0),IF(ISBLANK('2017'!L12)," & Chr(34) & Chr(34) & ",'2017'!L12))"