我有调用我文件夹中最新文件的代码,如何在Vlookup函数中调用该文件

时间:2019-07-09 13:18:50

标签: excel vba excel-formula

您好,我使用的是调用文件夹中最新文件的函数。我需要自动执行该文件的vlookup。我在正确调出文件时遇到问题

我已经尝试通过它的路径来调用它,但是我认为我没有正确地调用它。

Sub oversub()

    Dim MyPath As String
    Dim MyFile As String
    Dim LatestFile As String
    Dim LatestDate As Date
    Dim LMD As Date

    MyPath = "C:\Users\TAmon1\Desktop\OverSubscription Dash"
    If Right(MyPath, 1) <> "\" Then MyPath = MyPath & "\"
    MyFile = Dir(MyPath & "*.csv", vbNormal)
    If Len(MyFile) = 0 Then
        MsgBox "No files were found...", vbExclamation
        Exit Sub
    End If
    Do While Len(MyFile) > 0
        LMD = FileDateTime(MyPath & MyFile)
        If LMD > LatestDate Then
            LatestFile = MyFile
            LatestDate = LMD
        End If
        MyFile = Dir
    Loop
    Workbooks.Open MyPath & LatestFile
Dim wb As Workbook
'wbstring = MyPath & LatestFile
Windows("Planning_tool.xlsm").Activate

Range("N2").Select
    ActiveCell.FormulaR1C1 = _
        "=VLOOKUP(C[-13],'Router_level_crosstab.csv'!C1:C11,11,FALSE)"
    Range("N2").Select
    Selection.AutoFill Destination:=Range("N2:N539")
    Range("N2:N539").Select
    Range("P2").Select
    ActiveCell.FormulaR1C1 = ""
    Range("O2").Select
    'ActiveSheet.Paste
    Range("O2").Select
    ActiveCell.FormulaR1C1 = _
        "=VLOOKUP(C[-14],'Router_level_crosstab.csv'!C1:C11,5,FALSE)"
    Range("O2").Select
    Selection.AutoFill Destination:=Range("O2:O539")
    Range("O2:O539").Select
    Range("Q2").Select
    ActiveCell.FormulaR1C1 = _
        "=VLOOKUP(C[-16],'Router_level_crosstab.csv'!C1:C11,9,FALSE)"
    Range("Q2").Select
    Selection.AutoFill Destination:=Range("Q2:Q539")
    Range("Q2:Q539").Select

当前,我正在调用可以正常工作的直接文件,但是当我有一个新文件时,我将需要手动更改文件名。我需要将最新文件合并到Vlookup中。

0 个答案:

没有答案