+ Reply to Thread
Results 1 to 11 of 11

Is there any formula can read the data from the file name and mark it onto an excel file ?

Hybrid View

davidtips0214 Is there any formula can read... 02-10-2023, 08:02 AM
protonLeah Re: Is there any formula can... 02-10-2023, 09:42 PM
davidtips0214 Re: Is there any formula can... 02-11-2023, 12:23 AM
protonLeah Re: Is there any formula can... 02-11-2023, 12:47 AM
davidtips0214 Re: Is there any formula can... 02-11-2023, 01:14 AM
protonLeah Re: Is there any formula can... 02-11-2023, 07:03 PM
davidtips0214 Re: Is there any formula can... 02-12-2023, 05:28 AM
davidtips0214 Re: Is there any formula can... 02-11-2023, 10:24 PM
protonLeah Re: Is there any formula can... 02-12-2023, 07:37 PM
protonLeah Re: Is there any formula can... 02-13-2023, 10:36 PM
davidtips0214 Re: Is there any formula can... 02-13-2023, 11:34 PM
  1. #1
    Registered User
    Join Date
    07-31-2022
    Location
    Hong Kong
    MS-Off Ver
    Office 365
    Posts
    25

    Is there any formula can read the data from the file name and mark it onto an excel file ?

    Every month, I need to scan thousands of testing reports into PDF format.

    The File naming system is : XXX ZZZZZ 2023-MM-DD.pdf

    XXX : station code (3 digits)
    ZZZZZ : Machine Number (from 2 digits to 5 digits)

    ALL pdf files will store in a directory
    C:\2023-01\XXX ZZZ 2023-MM-DD.pdf


    For example :
    I created 5 different PDF files

    CEN EAP1 2023-01-10.PDF
    CIO L2 2023-01-24.PDF
    JOR BL1 2023-01-07.PDF
    LIV L1 2023-01-21.PDF
    LON L1 2023-01-18.PDF


    My question is :
    Is there any excel formula be able to read from the above pdf file names and automatically mark "PM" in the attached excel file ?


    Or, is there any other intelligent ways to do the same ?
    Attached Files Attached Files

  2. #2
    Forum Guru
    Join Date
    03-02-2006
    Location
    Los Angeles, Ca
    MS-Off Ver
    WinXP/MSO2007;Win10/MSO2016
    Posts
    12,934

    Re: Is there any formula can read the data from the file name and mark it onto an excel fi

    Option Explicit
    Sub ProcessPDFs()
        Dim oFSO        As Object
        Dim PDFFolder   As Object
        Dim PDFFile     As Object
        Dim Parts       As Variant
        Dim DateCol     As Variant
        Dim Prefix      As String
        Dim PrefixRow   As Variant
        Dim FolderPath  As String
        
        FolderPath = "C:\2023-01"
        Set oFSO = CreateObject("Scripting.FileSystemObject")
        Set PDFFolder = oFSO.GetFolder(FolderPath)
        For Each PDFFile In PDFFolder.Files
            If UCase(Right(PDFFile.Name, 3)) = "PDF" Then
                Parts = Split(Left(PDFFile.Name, Len(PDFFile.Name) - 4), " ")
                Prefix = Parts(0) & "-" & Parts(1)
                Set PrefixRow = Columns("D").Find(Prefix)
                Parts = DateValue(Parts(2))
                Set DateCol = Range("2:2").Find(Parts)
                Cells(PrefixRow.Row, DateCol.Column) = "PM"
            End If
        Next PDFFile
    End Sub
    Ben Van Johnson

  3. #3
    Registered User
    Join Date
    07-31-2022
    Location
    Hong Kong
    MS-Off Ver
    Office 365
    Posts
    25

    Re: Is there any formula can read the data from the file name and mark it onto an excel fi

    wow ... that's awesome
    thank you so much for that

  4. #4
    Forum Guru
    Join Date
    03-02-2006
    Location
    Los Angeles, Ca
    MS-Off Ver
    WinXP/MSO2007;Win10/MSO2016
    Posts
    12,934

    Re: Is there any formula can read the data from the file name and mark it onto an excel fi

    You're welcome...

  5. #5
    Registered User
    Join Date
    07-31-2022
    Location
    Hong Kong
    MS-Off Ver
    Office 365
    Posts
    25

    Re: Is there any formula can read the data from the file name and mark it onto an excel fi

    @protonLeah

    your VBA helped me a lot, very appreicated

    Just a bit more request, is it possible to generate a date and put it onto sheet "PM date" ?
    Please refer to a new attachment

    As this is requested by our client.
    Attached Files Attached Files

  6. #6
    Forum Guru
    Join Date
    03-02-2006
    Location
    Los Angeles, Ca
    MS-Off Ver
    WinXP/MSO2007;Win10/MSO2016
    Posts
    12,934

    Re: Is there any formula can read the data from the file name and mark it onto an excel fi

    Sub ProcessPDFs()
        Dim SrcSht      As Worksheet
        Dim PMDateRec   As Worksheet
        Dim oFSO        As Object
        Dim PDFFolder   As Object
        Dim PDFFile     As Object
        Dim Parts       As Variant
        Dim DateCol     As Variant
        Dim Prefix      As String
        Dim PrefixRow   As Variant
        Dim FolderPath  As String
        Dim DestRow     As Variant
        Dim DestCol     As Long
        
        Set SrcSht = Worksheets("sheet1")
        Set PMDateRec = Sheets("pm date")
        
        FolderPath = "C:\2023-01"
        Set oFSO = CreateObject("Scripting.FileSystemObject")
        Set PDFFolder = oFSO.GetFolder(FolderPath)
        
        For Each PDFFile In PDFFolder.Files
            If UCase(Right(PDFFile.Name, 3)) = "PDF" Then
                Parts = Split(Left(PDFFile.Name, Len(PDFFile.Name) - 4), " ")
                Prefix = Parts(0) & "-" & Parts(1)
                
                ' update PM
                With SrcSht
                    Set PrefixRow = .Columns("D").Find(Prefix)
                    Parts = DateValue(Parts(2))
                    Set DateCol = .Range("2:2").Find(Parts)
                    .Cells(PrefixRow.Row, DateCol.Column) = "PM"
                End With    'source sheet
                
                'put PM dates
                With PMDateRec
                    Set DestRow = .Columns("D").Find(Prefix)
                    DestCol = .Cells(DestRow.Row, .Columns.Count).End(xlToLeft).Column + 1
                    .Cells(DestRow.Row, DestCol).Value = Format(Date, "dd-mmm")
                End With
            End If
        Next PDFFile
    End Sub

  7. #7
    Registered User
    Join Date
    07-31-2022
    Location
    Hong Kong
    MS-Off Ver
    Office 365
    Posts
    25

    Re: Is there any formula can read the data from the file name and mark it onto an excel fi

    @protonLeah

    Can you please take a look at the excel file again ?
    The result generated at the sheet "PM Date" seems have problems.

    Sorry for causing you troubles.
    Attached Files Attached Files

  8. #8
    Registered User
    Join Date
    07-31-2022
    Location
    Hong Kong
    MS-Off Ver
    Office 365
    Posts
    25

    Re: Is there any formula can read the data from the file name and mark it onto an excel fi

    wow . . . that's great !!!
    thank you so much

  9. #9
    Forum Guru
    Join Date
    03-02-2006
    Location
    Los Angeles, Ca
    MS-Off Ver
    WinXP/MSO2007;Win10/MSO2016
    Posts
    12,934

    Re: Is there any formula can read the data from the file name and mark it onto an excel fi

    In the attached zipfile, several of the pdf's have the same CODE & UNIT, but different dates:
                                    COUNT
    CEN	EAP1	CEN EAP1	2
    CEN	EAP5	CEN EAP5	2
    CIO	BL1	CIO BL1	2
    JOR	BL1	JOR BL1	2
    LIV	L2	LIV L2	2
    LON	BL1	LON BL1	2
    LON	L1	LON L1	2
    I think I have a fix, but it'll have to wait 'til tomorrow

  10. #10
    Forum Guru
    Join Date
    03-02-2006
    Location
    Los Angeles, Ca
    MS-Off Ver
    WinXP/MSO2007;Win10/MSO2016
    Posts
    12,934

    Re: Is there any formula can read the data from the file name and mark it onto an excel fi

    Option Explicit
    Sub ProcessPDFs()
        Dim SrcSht      As Worksheet
        Dim PMDateRec   As Worksheet
        Dim oFSO        As Object
        Dim PDFFolder   As Object
        Dim PDFFile     As Object
        Dim Parts       As Variant
        Dim DateCol     As Variant
        Dim Prefix      As String
        Dim PrefixRow   As Variant
        Dim FolderPath  As String
        Dim DestRow     As Variant
        Dim DestCol     As Long, r As Long
        
        Set SrcSht = Worksheets("sheet1")
        Set PMDateRec = Sheets("pm date")
        
        FolderPath = "C:\2023-01"
        Set oFSO = CreateObject("Scripting.FileSystemObject")
        Set PDFFolder = oFSO.GetFolder(FolderPath)
        
        For Each PDFFile In PDFFolder.Files
            r = r + 1
            Sheet5.Cells(r, 1).Value = PDFFile.Name
            If UCase(Right(PDFFile.Name, 3)) = "PDF" Then
                Parts = Split(Left(PDFFile.Name, Len(PDFFile.Name) - 4), " ")
                Prefix = Parts(0) & "-" & Parts(1)
                
                ' update PM
                With SrcSht
                    Set PrefixRow = .Columns("D").Find(Prefix)
                    Parts = DateValue(Parts(2))
                    Set DateCol = .Range("2:2").Find(Parts)
                    .Cells(PrefixRow.Row, DateCol.Column) = "PM"
                End With    'source sheet
                
                'put PM dates
                With PMDateRec
                    Set DestRow = .Columns("D").Find(Prefix)
                    DestCol = .Cells(DestRow.Row, .Columns.Count).End(xlToLeft).Column + 1
                    .Cells(DestRow.Row, DestCol).Value = Format(Date, "dd-mmm")
                    
                    'test for dup date and delete
                    If .Cells(DestRow.Row, DestCol - 1).Value = .Cells(DestRow.Row, DestCol).Value Then
                        .Cells(DestRow.Row, DestCol).Value = ""
                    End If
                End With
            End If
        Next PDFFile
    End Sub

  11. #11
    Registered User
    Join Date
    07-31-2022
    Location
    Hong Kong
    MS-Off Ver
    Office 365
    Posts
    25

    Re: Is there any formula can read the data from the file name and mark it onto an excel fi

    Hello Proton,

    Thanks for your hard work.
    some errors during debugging, I am studying right now.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Replies: 7
    Last Post: 07-19-2019, 06:30 AM
  2. Read binary data from non excel file and post in excel file
    By D S Rama Rao in forum Excel Programming / VBA / Macros
    Replies: 13
    Last Post: 01-05-2015, 05:33 PM
  3. read value of strings from a text file and make an excel file.
    By gashforing in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 11-08-2009, 07:12 AM
  4. Replies: 1
    Last Post: 08-18-2006, 10:40 AM
  5. Replies: 2
    Last Post: 12-12-2005, 02:35 PM
  6. [SOLVED] How to save a PC Excel file into MAC (it says file is read only)?
    By claudiosissa in forum Excel General
    Replies: 0
    Last Post: 11-29-2005, 10:00 PM
  7. Replies: 1
    Last Post: 09-12-2005, 09:05 AM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1