【问题标题】:Excel macro to read input from files created today onlyExcel 宏仅从今天创建的文件中读取输入
【发布时间】:2012-08-17 18:02:55
【问题描述】:

我有一个以 txt 格式导出每日报告的应用程序。 我有一个宏,可以从这些报告中提取某些数据行并将它们放入输出 xls 文件中。我的宏的输入目录目前是一个单独的文件夹,我手动将今天的报告移动到该文件夹​​中。

我希望我的宏能够仅从默认报告文件夹中读取,并且仅读取使用今天日期创建的文件。

报告文件的命名约定如下: 1101_16_16_AppServiceUser_YYYYMMDDhhmmssXXX.txt 不确定文件名的最后 3 位数字代表什么,但它们始终是数字。

帮助?

哇,真快!谢谢...第一次使用stackoverflow。 我想我应该包括提取数据并将其转储到 excel 的代码......这里是:

Sub PullLinesFromEPremisReport()
Dim FileName, PathN, InputLn As String
Dim SearchFor1, SearchFor2, OutpFile As String
Dim StringLen1, StringLen2 As Integer
Dim colFiles As New Collection
Dim bridgekey As String

PathO = "C:\Documents and Settings\GROMERO\Desktop\CM reconciliation\output\"
PathN = "C:\Documents and Settings\GROMERO\Desktop\CM reconciliation\input\"

FileName = Dir(PathN)

While FileName <> ""
    colFiles.Add (FileName)
    FileName = Dir

Wend

SearchFor1 = "BRIDGE KEY"

StringLen1 = Len(SearchFor1)


OutpFile = "RESULTS.xls"
Open PathO & OutpFile For Output As #2

For Each Item In colFiles
    Open PathN & Item For Input As #1
        Do Until EOF(1) = True
               Line Input #1, InputLn
               If (Left(LTrim$(InputLn), StringLen1) = SearchFor1) Then
                    bridgekey = InputLn
           End If

    Loop

Close #1
Next Item

Close #2
End Sub

【问题讨论】:

    标签: vba excel


    【解决方案1】:

    Daniel 的回答是正确的,但是使用 FileSystemObject 需要几个步骤:

    确保您参考了“Microsoft Scripting Runtime”:

    然后,遍历目录中的文件:

    Sub WorkOnTodaysReports()
    
    'the vars you'll need
    Dim fso As New FileSystemObject
    Dim fldr As Folder
    Dim fls As Files
    Dim fl As File
    
    Set fldr = fso.GetFolder("C:\Reports")
    Set fls = fldr.Files
    
    For Each fl In fls
    
        'InStr returns the position of the substring, or 0 if not found
        '    EDIT: you can explicitly use the reliable parts of your file name
        '    to avoid false positives
        If InStr(1, fl.Name, "AppServiceUser_" & Format(Now, "YYYYMMDD")) > 0 Then
    
            'Do your processing
    
        End If
    
    Next fl
    
    End Sub
    

    编辑:所以我认为,从您发布的代码中,您可以根据需要将PathN 发送到主报告文件夹,然后像这样修改您的While 语句:

    While FileName <> ""
    
        If InStr(1, FileName, "AppServiceUser_" & Format(Now, "YYYYMMDD")) > 0 Then
    
            colFiles.Add (FileName)
    
        End If
    
        FileName = Dir
    
    Wend
    

    【讨论】:

    • OP,我认为如果您在问题中添加“vba”标签,代码突出显示可能会起作用?只是猜测
    • 这是一种不可靠的方法。选择丹尼尔建议的第二个选项。原因是文件格式是YYYY MM DD hh mm ss X X X 例如:所以如果我正在寻找 8 月 18 日(2012 年 8 月 18 日)的文件,那么上述将在2012 08 19 20 12 08 184 上失败
    • 如果您的文件命名是来自另一个应用程序的自动输出,并且您可以依赖“AppServiceUser_YYYYMMDD....”,那么您可以添加到 If,例如:“AppServiceUser_”和格式(现在, "YYYYMMDD"))。 OP表示文件格式在文件名中间包含“YYYYMMDD”。不过你是对的,如果字符串出现在其他任何地方,它会产生误报。确认,不知道如何在评论中使用文字“。更新答案。
    • 是的。这将无法通过 8 月 18 日检查 1101_16_16_AppServiceUser_20120819201208188.txt 这是根据 OP 提到的 the naming convention of the report files is as follows: 1101_16_16_AppServiceUser_YYYYMMDDhhmmssXXX.txt not sure what the last 3 digits on the file name represents, but they're always numbers.
    • + 1 :) 像你一样添加“AppServiceUser_”会解决这个问题:)
    【解决方案2】:

    我想到了两种方法可以做到这一点。假设您通过FileSystemObject 使用File

    file.Name 上执行Instr,在字符串中查找Format(Date, "YYYYMMDD")

    或者使用更简单的方法循环遍历文件并在循环中执行以下操作:

    If File.DateCreate >= Date Then
        'Do something
    end if
    

    其中File 是用于循环文件的实际变量。

    【讨论】:

    • + 1 但我不推荐第一种方法。第二种方法很好。请参阅我在安迪的回答中的评论。
    • 确实如此。我列出了第一种方法,因为它在技术上可行。但是,由于我们没有明确指出它可能会导致误报。
    【解决方案3】:
    If fileName like "*AppServiceUser_" & Format(Now, "YYYYMMDD") & _
                                                  "#########.txt" Then
        'good to go
    End If
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-10-30
      • 2016-01-19
      • 1970-01-01
      • 2011-03-24
      • 1970-01-01
      • 2014-10-07
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多