+ Reply to Thread
Results 1 to 13 of 13

VBA to copy specific cells from all files in a folder

Hybrid View

  1. #1
    Registered User
    Join Date
    10-17-2014
    Location
    Australia
    MS-Off Ver
    2007
    Posts
    29

    VBA to copy specific cells from all files in a folder

    Hi guys,

    I am trying to copy values from specific cells in all the files stored in a folder and paste it onto a new file. I found a code which does almost what I am after. The only problem here is that it can not access the file names with an apostrophe like "Simon's Boilers", "Baker's extra", "Sauer's bakehouse" etc.

    Is there a way to fix this thing? Other than that it is all good.

    Sub test()
    Dim myDir As String, fn As String, i As Long
    myDir = "\\STM-FS2\shared documents\shared documents\JOB COSTINGS\"
    fn = Dir(myDir & "*.xlsx")
    Do While fn <> ""
        i = i + 1
        myFormula = "='" & myDir & "[" & fn & "]HOURS'!"
        With ThisWorkbook.Sheets(1).Cells(i, 1)
            .Value = fn
            .Offset(, 1).Resize(, 2).Formula = _
            Array(myFormula & "C4", myFormula & "O17")
        End With
        fn = Dir
    Loop
    End Sub

  2. #2
    Forum Expert
    Join Date
    12-10-2006
    Location
    Sydney
    MS-Off Ver
    Office 365
    Posts
    3,565

    Re: VBA to copy specific cells from all files in a folder

    Hi Mehul,

    The problem is that in your code (by design) a single apostrophe denotes a the start or end of a field. To get around this you to write code to replace any single apostrophe in the fn filename variable with two apostrophes, which though I can't test without being on your server, I would use something like this...

    If InStr(fn, "'") > 0 Then
                fn = fn & Replace(fn, "'", "''")
            End If
    ...just below this line:

    i = i + 1
    HTH

    Robert
    ____________________________________________
    Please ensure you mark your thread as Solved once it is. Click here to see how
    If this post helps, please don't forget to say thanks by clicking the star icon in the bottom left-hand corner of my post

  3. #3
    Registered User
    Join Date
    10-17-2014
    Location
    Australia
    MS-Off Ver
    2007
    Posts
    29

    Re: VBA to copy specific cells from all files in a folder

    I have one more query if you could please help me.

    As you see the code is operating in the worksheet named "Hours". Is it possible to program the code to look in Sheet2 if "Hours" worksheet is not present?

    Thanks once again.

  4. #4
    Registered User
    Join Date
    10-17-2014
    Location
    Australia
    MS-Off Ver
    2007
    Posts
    29

    Re: VBA to copy specific cells from all files in a folder

    Thanks a lot Trebor76. I just had to change the code a bit as below and it worked out perfectly.

    If InStr(fn, "'") > 0 Then
                fn = Replace(fn, "'", "''")
            End If

  5. #5
    Forum Expert
    Join Date
    12-10-2006
    Location
    Sydney
    MS-Off Ver
    Office 365
    Posts
    3,565

    Re: VBA to copy specific cells from all files in a folder

    Is it possible to program the code to look in Sheet2 if "Hours" worksheet is not present?
    Though it's much easier to do this while the file is open, try these lines of code...

    If IsError(ThisWorkbook.Sheets(1).Cells(i, 1)) = True Then
            myFormula = "='" & myDir & "[" & fn & "]Sheet2'!"
            .Value = fn
        End If
    ...immediately under this existing line:

    .Value = fn
    Regards,

    Robert

  6. #6
    Registered User
    Join Date
    10-17-2014
    Location
    Australia
    MS-Off Ver
    2007
    Posts
    29

    Re: VBA to copy specific cells from all files in a folder

    Thanks Robert. It still asks me to select the sheet every time it can't find Hours worksheet.

  7. #7
    Forum Expert
    Join Date
    12-10-2006
    Location
    Sydney
    MS-Off Ver
    Office 365
    Posts
    3,565

    Re: VBA to copy specific cells from all files in a folder

    Without being on your server and testing it's very hard to recommend a solution, but see how this goes:

        With ThisWorkbook.Sheets(1).Cells(i, 1)
            On Error Resume Next
                .Value = fn
                If IsError(ThisWorkbook.Sheets(1).Cells(i, 1)) = True Then
                    myFormula = "='" & myDir & "[" & fn & "]Sheet2'!"
                    .Value = fn
                End If
            On Error GoTo 0
            .Offset(, 1).Resize(, 2).Formula = _
            Array(myFormula & "C4", myFormula & "O17")
        End With
    Robert

  8. #8
    Registered User
    Join Date
    10-17-2014
    Location
    Australia
    MS-Off Ver
    2007
    Posts
    29

    Re: VBA to copy specific cells from all files in a folder

    I absolutely agree it is difficult to recommend a solution this way. It still continues to do the same thing.

    Not sure if this will help, here is a screenshot of the point where excel asks me to select a sheet as it couldn't find Hours sheet. Hope it helps.

    Untitled.jpg

  9. #9
    Forum Expert
    Join Date
    12-10-2006
    Location
    Sydney
    MS-Off Ver
    Office 365
    Posts
    3,565

    Re: VBA to copy specific cells from all files in a folder

    Not sure if this will help, here is a screenshot of the point where excel asks me to select a sheet as it couldn't find Hours sheet. Hope it helps.
    No it doesn't as the attachment doesn't open

  10. #10
    Registered User
    Join Date
    10-17-2014
    Location
    Australia
    MS-Off Ver
    2007
    Posts
    29

    Re: VBA to copy specific cells from all files in a folder

    I'm sorry for that.
    I have attached it once again or you can use this link to access it.

    http://postimg.org/image/7jbyuzx6v/

    Untitled.jpg

  11. #11
    Forum Expert
    Join Date
    12-10-2006
    Location
    Sydney
    MS-Off Ver
    Office 365
    Posts
    3,565

    Re: VBA to copy specific cells from all files in a folder

    OK, that really doesn't help either but try this where I use an ADO connection to the workbook to determine if the sheet exists or not:

    Option Explicit
    Sub test()
    
        '//As this macro uses early binding, a reference to 'Microsoft ActiveX Data Objects n.n library' (via Tools > References menu item) is required//
    
        Dim myDir As String, fn As String, i As Long, myFormula As String
        Dim cnn As New ADODB.Connection
        Dim rst As New ADODB.Recordset
        Dim strClosedWBSheetName As String
        
        myDir = "\\STM-FS2\shared documents\shared documents\JOB COSTINGS\"
        fn = Dir(myDir & "*.xlsx")    
        
        strClosedWBSheetName = "HOURS"
        
        Do While fn <> ""
            i = i + 1
            
            If InStr(fn, "'") > 0 Then
                fn = Replace(fn, "'", "''")
            End If
            
            'Create a connection to the closed workbook
            cnn.Open "Provider=Microsoft.Jet.OLEDB.4.0;" & _
                 "Data Source=" & myDir & fn & ";" & _
                 "Extended Properties=""Excel 8.0;HDR=Yes;"";"
            
            On Error Resume Next
                'If the tab 'strClosedWBSheetName' string variable exists in the workbook, then...
                rst.Open "SELECT * FROM [" & strClosedWBSheetName & "$];", cnn, adOpenStatic, adLockReadOnly
                If Err.Number = 0 Then
                    '...set the 'myFormula' string variable with it
                    myFormula = "='" & myDir & "[" & fn & "]" & strClosedWBSheetName & "'!"
                'Else...
                Else
                    '...set the 'myFormula' string variable with 'Sheet2'
                    myFormula = "='" & myDir & "[" & fn & "]Sheet2'!"
                End If
            On Error GoTo 0
            
            'Remove recordset and connecttion from memory
            Set rst = Nothing
            cnn.Close
                    
            With ThisWorkbook.Sheets(1).Cells(i, 1)
                .Value = fn
                .Offset(, 1).Resize(, 2).Formula = _
                Array(myFormula & "C4", myFormula & "O17")
            End With
            fn = Dir
            
        Loop
        
    End Sub
    Again this is untested so it may need some tweaking on your side. Also note the comment regarding making a reference to the Microsoft ActiveX Data Objects library which is required before you try and run the macro.

    Regards,

    Robert
    Last edited by Trebor76; 11-03-2014 at 01:04 AM.

  12. #12
    Registered User
    Join Date
    10-17-2014
    Location
    Australia
    MS-Off Ver
    2007
    Posts
    29

    Re: VBA to copy specific cells from all files in a folder

    I have just one last issue with this code. The code always gives the output in the first sheet of the workbook. For example, even if I have the code in Sheet3 and run it there the output goes to Sheet1. How can I fix that?
    Here is code that I am using currently.

    Sub test()
    Dim myDir As String, fn As String, i As Long, myFormula As String
    myDir = "\\STM-FS2\shared documents\shared documents\JOB COSTINGS\"
    fn = Dir(myDir & "*.xlsx")
    Do While fn <> ""
        i = i + 1
        If InStr(fn, "'") > 0 Then
                fn = Replace(fn, "'", "''")
            End If
        myFormula = "='" & myDir & "[" & fn & "]HOURS'!"
        With ThisWorkbook.Sheets(1).Cells(i, 1)
            .Value = fn
                
            .Offset(, 1).Resize(, 2).Formula = _
            Array(myFormula & "C4", myFormula & "O17")
        
        End With
        fn = Dir
    Loop
    End Sub

  13. #13
    Registered User
    Join Date
    10-17-2014
    Location
    Australia
    MS-Off Ver
    2007
    Posts
    29

    Re: VBA to copy specific cells from all files in a folder

    Thanks for your help Robert. Unfortunately this did not work either. So to make it less complicated I manually sorted out the files from the folder which did not have HOURS spreadsheet on it.

+ 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: 1
    Last Post: 01-28-2019, 06:55 PM
  2. [SOLVED] Need to copy values from all files in folder from specific worksheet
    By jh51745 in forum Excel Programming / VBA / Macros
    Replies: 14
    Last Post: 06-03-2013, 01:05 PM
  3. [SOLVED] VBA Code open files in folder, copy text to workbook-Next time folder opened copy only new
    By Bikeman in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 01-02-2013, 07:59 PM
  4. [SOLVED] Copy 10 rows from specific files in a folder
    By GDM69 in forum Excel Programming / VBA / Macros
    Replies: 14
    Last Post: 08-07-2012, 10:13 AM
  5. Replies: 1
    Last Post: 01-24-2006, 11:00 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