Excel VBA自动化

时间:2018-07-16 02:35:01

标签: excel vba excel-vba

我正在使用VBA自动化excel。我创建了2个新列,其中一个列曾经用于vlookup函数。ode工作正常,但是我遇到了一个问题。可以更改Countrys_2018.xlsx文件的数量,这就是为什么我要使其动态化?我的意思是Countrys_2018.xlsx的数据在增长和收缩。你可以检查一下并给我个主意吗?预先谢谢你

Sub Test() Dim Rng1 As Range Dim Rng2 As Range
    Dim LastRow As Long

    Columns("B:B").Insert
    Cells(1, 2) = "Íàïðàâëåíèå"
    Columns("S:S").Insert
    Cells(1, 19) = "CDR â ìèíóòàõ"

    Application.ScreenUpdating = False

    Set Rng1 = Range("S2:S" & Range("A2").End(xlDown).Row)
    Rng1.FormulaR1C1 = "= RC[-4] / 60"
    Range("B2").Select
    ActiveCell.FormulaR1C1 = _
      "=VLOOKUP(RC[-1],[Countries_2018.xlsx]Sheet!R1C[-1]:R2588C,2,0)"
    Range("B2").Select
    Selection.AutoFill Destination:=Range(Selection, Selection.Offset(0, -1).End(xlDown).Offset(0, 1))

    Application.ScreenUpdating = True
End Sub

1 个答案:

答案 0 :(得分:0)

尝试一下:

Sub Test()

    Dim Rng1 As Range
    Dim Rng2 As Range

    Columns("B:B").Insert
    Cells(1, 2) = "Íàïðàâëåíèå"
    Columns("S:S").Insert
    Cells(1, 19) = "CDR â ìèíóòàõ"

    Set Rng1 = Range("S2:S" & Range("A2").End(xlDown).Row)
    Set Rng2 = Range("B2:B" & Range("A2").End(xlDown).Row)
    Rng1.FormulaR1C1 = "= RC[-4] / 60"
    Rng2.FormulaR1C1 = "=INDEX([Countries_2018.xlsx]Sheet!C2,MATCH(RC1,[Countries_2018.xlsx]Sheet!C1,0))"

End Sub