Results 1 to 7 of 7

Opening only xlsx files

Threaded View

RoyLittle0 Opening only xlsx files 04-21-2014, 11:04 AM
jaslake Re: Opening only xlsx files 04-21-2014, 12:23 PM
Leith Ross Re: Opening only xlsx files 04-21-2014, 12:26 PM
RoyLittle0 Re: Opening only xlsx files 04-21-2014, 12:53 PM
Leith Ross Re: Opening only xlsx files 04-21-2014, 01:14 PM
RoyLittle0 Re: Opening only xlsx files 04-21-2014, 01:22 PM
RoyLittle0 Re: Opening only xlsx files 04-22-2014, 02:27 AM
  1. #1
    Registered User
    Join Date
    01-04-2012
    Location
    Derby, England
    MS-Off Ver
    2010, 2013 2016 Pro
    Posts
    85

    Opening only xlsx files

    Hi All,

    My current code will open and compile all my required cells to a Master document and all works fine until I come across another file type, my problem is I need to only open files of a xlsx type, my current code will try to open any file beginning with PJ, even a doc file, how can I alter to code to only open xlsx files?

    Option Explicit
    Sub DoStuff()
        Application.ScreenUpdating = True
        
    ' ****************************************************
        ' Set this line to the path and Parent Folder ...don't forget True at the end of the line
        OpenFilesInAllFolders "F:\Excel Test Files\Excel Compile", True
    ' ****************************************************
        
        Sheets("Sheet1").Activate
        Application.ScreenUpdating = True
    End Sub
    
    'Summary: Open all files in a single folder or all files in the subfolders of the parent
    '         folder, Only look at files that begin with PJ.
    
    Sub OpenFilesInAllFolders(ByVal SourceFolderName As String, ByVal IncludeSubfolders As Boolean)
        Dim FSO As Object
        Dim SourceFolder As Object
        Dim subfolder As Object
        Dim FileItem As Object
        Dim LR As Long
        Dim ws As Worksheet
        
        Set ws = Sheets("Sheet1") 'Target Sheet Name
        Set FSO = CreateObject("Scripting.FileSystemObject")
        Set SourceFolder = FSO.GetFolder(SourceFolderName)
    
        For Each FileItem In SourceFolder.Files
            If Not InStr(FileItem.Name, "Master") = 1 _
                    And Not InStr(FileItem.Name, "~$Master") = 1 _
                    And InStr(FileItem.Name, "PJ") Then
    
    
                Workbooks.Open SourceFolder & "\" & FileItem.Name
                
    '            ActiveSheet.Range("A98").Value = "Compiled" This allows the Script
    '            To know that the file has been read already, stops any duplications
                
                If Not ActiveSheet.Range("A98").Value = "Compiled" Then
    
                With ws
                    LR = .Range("A" & .Rows.Count).End(xlUp).Row + 1
                    .Cells(LR, "A") = ActiveSheet.Range("E12") 'Customer
                    .Cells(LR, "B") = ActiveSheet.Range("E13") 'Location
                    .Cells(LR, "C") = ActiveSheet.Range("E14") 'Contact
                    .Cells(LR, "D") = ActiveSheet.Range("U12") 'Technician
                    .Cells(LR, "E") = ActiveSheet.Range("U13") 'Chargable
                    .Cells(LR, "F") = ActiveSheet.Range("G15") 'Machine Type 1
                    .Cells(LR, "G") = ActiveSheet.Range("G16") 'Serial Number 1
                    .Cells(LR, "H") = ActiveSheet.Range("G17") 'Hours Run 1
                    .Cells(LR, "I") = ActiveSheet.Range("I20") 'Reason For Visit
                    .Cells(LR, "J") = ActiveSheet.Range("Y10") 'PJ**-0000-0000-00
                    .Cells(LR, "K") = ActiveSheet.Range("AB10") 'PJ00-****-0000-00
                    .Cells(LR, "L") = ActiveSheet.Range("AG10") 'PJ00-0000-****-00
                    .Cells(LR, "M") = ActiveSheet.Range("AL10") 'PJ00-0000-0000-**
                    .Cells(LR, "N") = ActiveSheet.Range("A23") 'Line 1 of
                    .Cells(LR, "O") = ActiveSheet.Range("A24") 'Line 2
                    .Cells(LR, "P") = ActiveSheet.Range("A25") 'Line 3
                    .Cells(LR, "Q") = ActiveSheet.Range("A26") 'Line 4
                    .Cells(LR, "R") = ActiveSheet.Range("A27") 'Line 5
                    .Cells(LR, "S") = ActiveSheet.Range("A28") 'Line 6
                    .Cells(LR, "T") = ActiveSheet.Range("A29") 'Line 7
                    .Cells(LR, "U") = ActiveSheet.Range("A30") 'Line 8
                    .Cells(LR, "V") = ActiveSheet.Range("A31") 'Line 9
                    .Cells(LR, "W") = ActiveSheet.Range("A32") 'Line 10
                    .Cells(LR, "X") = ActiveSheet.Range("A33") 'Line 11
                    .Cells(LR, "Y") = ActiveSheet.Range("A34") 'Line 12
                    .Cells(LR, "Z") = ActiveSheet.Range("A35") 'Line 13
                    .Cells(LR, "AA") = ActiveSheet.Range("A36") 'Line 14
                    .Cells(LR, "AB") = ActiveSheet.Range("A37") 'Line 15
                    .Cells(LR, "AC") = ActiveSheet.Range("A67") 'Line 16
                    .Cells(LR, "AD") = ActiveSheet.Range("A68") 'Line 17
                    .Cells(LR, "AE") = ActiveSheet.Range("Y50") 'Total Hours Worked
                    ActiveSheet.Range("A98").Value = "Compiled"
                End With
                End If
                ActiveWorkbook.Close True
            End If
        Next FileItem
    
        If IncludeSubfolders Then
            For Each subfolder In SourceFolder.SubFolders
                OpenFilesInAllFolders subfolder.Path, True
            Next subfolder
        End If
    
        Set FileItem = Nothing
        Set SourceFolder = Nothing
        Set FSO = Nothing
    End Sub
    Last edited by RoyLittle0; 04-21-2014 at 12:49 PM. Reason: Corrected Code Tags and Full code added

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. Replies: 3
    Last Post: 01-05-2014, 05:50 AM
  2. Replies: 0
    Last Post: 03-13-2013, 09:08 PM
  3. How to automatically get values from one .xlsx to another without opening files?
    By kummakki in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 10-22-2012, 12:44 PM
  4. Merge Data from different .xlsx files & different sheet to a new .xlsx
    By QcSylvanio in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 10-03-2012, 01:11 PM
  5. How to add the columns data of several xlsx files of a folder in another xlsx file
    By ravikumar00008 in forum Excel Programming / VBA / Macros
    Replies: 9
    Last Post: 05-25-2012, 04:29 AM

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