首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在代码中使用最新的文件函数执行vlookup

如何在代码中使用最新的文件函数执行vlookup
EN

Stack Overflow用户
提问于 2019-07-09 17:23:10
回答 1查看 45关注 0票数 0

你好,我使用一个函数来调用我文件夹中的最新文件。我需要为这个文件自动化一个vlookup。我在正确调用文件时遇到了问题

我已经试着按它的路径叫它了,但我相信我没有正确地称呼它。

代码语言:javascript
复制
Sub oversub()

    'Newest file function

        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


'Vlookupfunction
    Range("N2").Select
    ActiveCell.FormulaR1C1=VLOOKUP(C[-13],'Router_level_crosstab.csv'!C1:C11,11,FALSE)"

目前,我调用的直接文件,这是可行的,但当我有一个新的文件,我将需要手动更改文件名。我需要将最新的文件合并到Vlookup中。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-07-09 17:30:00

代码语言:javascript
复制
Dim wbCSV As Workbook, wbPlanning as Workbook
Set wbPlanning = Workbooks("Planning_tool.xlsm")
'...
'...
Set wbCSV = Workbooks.Open(MyPath & LatestFile) '<< save the reference to the opened file
'...
'...
'don't rely on activeworkbook, activesheet, etc
wbPlanning.Sheets(1).Range("N2").FormulaR1C1 = _
         "=VLOOKUP(C[-13],'" & wbCSV.Name & "'!C1:C11,11,FALSE)"
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/56957743

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档