Results 1 to 10 of 10

Thread: Copy specific data from an excel template and paste in new workbook

  1. #1
    Junior Member
    Join Date
    Jun 2013
    Posts
    25
    Rep Power
    0

    Exclamation Copy specific data from an excel template and paste in new workbook

    I have around 100 excel templates in a folder from which I need to extract data from specific cells and put in a separate workbook. Is this something that can be done macro. The logic is MACRO open first template -> Copy Specific Cells -> Paste into new workbook-> Close template-> Proceed open with next template....

    The cells(Summary sheet) which we need to copy are E5, D20, C43, D43, C46, D46, C85, D85, C87 and D87. I am attaching sample template and final output file.
    Attached Files Attached Files

  2. #2
    Administrator Admin's Avatar
    Join Date
    Mar 2011
    Posts
    1,122
    Rep Power
    10
    Hi

    try this

    Code:
    Option Explicit
    
    Sub kTest()
        
        Dim k() As String, i As Long, n As Long, myCells, wbkT  As Workbook
        Dim TemplatePath    As String, FName As String, WbkName As String
        Dim Concat As String
        
        myCells = Array("E5", "D20", "C43", "D43", "C46", "D46", "C85", "D85", "C87", "D87")
        
        TemplatePath = "C:\Test"        '<<<<  adjust the path here
        
        If Right$(TemplatePath, 1) <> Application.PathSeparator Then TemplatePath = TemplatePath & Application.PathSeparator
        
        ReDim k(1 To 150)
        
        FName = Dir(TemplatePath & "*.xls*")
        WbkName = ThisWorkbook.Name
        Application.ScreenUpdating = 0
        Application.DisplayAlerts = 0
        Do While Not FName = vbNullString
            If FName <> WbkName Then
                Set wbkT = Workbooks.Open(TemplatePath & FName)
                Concat = vbNullString
                For i = LBound(myCells) To UBound(myCells)
                    Concat = IIf(Len(Concat), Concat & "|" & wbkT.Worksheets(1).Range(myCells(i)).Value, wbkT.Worksheets(1).Range(myCells(i)).Value)
                Next
                n = n + 1
                k(n) = Concat
                wbkT.Close 0
                Set wbkT = Nothing
            End If
            FName = Dir()
        Loop
        If n Then
            With Worksheets(1)
                'always overwrite the new data
                .UsedRange.Offset(1).ClearContents
                .Range("a2").Resize(n) = k
                .Range("a2").Resize(n).TextToColumns .Range("A2"), Other:=True, OtherChar:="|"
            End With
        End If
        Application.ScreenUpdating = 1
        Application.DisplayAlerts = 1
        
    End Sub
    Cheers !

    Excel Range to BBCode Table
    Use Social Networking Tools If You Like the Answers !

    Message to Cross Posters

    @ Home - Office 2010/2013/2016 on Win 10 (64 bit); @ Work - Office 2016 on Win 10 (64 bit)

  3. #3
    Junior Member
    Join Date
    Jun 2013
    Posts
    25
    Rep Power
    0
    Great!!!! Its working like charm.
    Last edited by Admin; 07-02-2013 at 07:34 AM.

  4. #4
    Junior Member
    Join Date
    Jun 2013
    Posts
    25
    Rep Power
    0
    I have created a folder C:\Test and put 2 sample templates. When i run the macro only information from the 1st template is copied twice(in two rows) in result file.

    So information from second template is NOT copied at all.

    Please look into it.
    Last edited by excel_1317; 07-01-2013 at 11:59 PM.

  5. #5
    Junior Member
    Join Date
    Jun 2013
    Posts
    25
    Rep Power
    0
    Please help..

  6. #6
    Administrator Excel Fox's Avatar
    Join Date
    Mar 2011
    Posts
    1,401
    Rep Power
    10
    Can you check that the information in the two templates are not exactly the same?

    EDIT: And add a back-slash for the path you specified

    "C:\Test\"
    A dream is not something you see when you are asleep, but something you strive for when you are awake.

    It's usually a bad idea to say that something can't be done.

    The difference between dream and aim, is that one requires soundless sleep to see and the other requires sleepless efforts to achieve

    Join us at Facebook

  7. #7
    Junior Member
    Join Date
    Jun 2013
    Posts
    25
    Rep Power
    0
    I had changed the template name, cell E5 and cell D20..





    Quote Originally Posted by Excel Fox View Post
    Can you check that the information in the two templates are not exactly the same?

    EDIT: And add a back-slash for the path you specified

    "C:\Test\"

  8. #8
    Administrator Admin's Avatar
    Join Date
    Mar 2011
    Posts
    1,122
    Rep Power
    10
    Quote Originally Posted by Excel Fox View Post
    And add a back-slash for the path you specified

    "C:\Test\"
    That's already been taken care of in the code itself

    replace
    Code:
    .Range("a2").Resize(n) = k
    with

    Code:
    .Range("a2").Resize(n) = Application.Transpose(k)
    Cheers !

    Excel Range to BBCode Table
    Use Social Networking Tools If You Like the Answers !

    Message to Cross Posters

    @ Home - Office 2010/2013/2016 on Win 10 (64 bit); @ Work - Office 2016 on Win 10 (64 bit)

  9. #9
    Administrator Excel Fox's Avatar
    Join Date
    Mar 2011
    Posts
    1,401
    Rep Power
    10
    That's already been taken care of in the code itself
    Yep, my bad. Only later did I notice that there was one whole line 'dedicated' to take care of that.
    A dream is not something you see when you are asleep, but something you strive for when you are awake.

    It's usually a bad idea to say that something can't be done.

    The difference between dream and aim, is that one requires soundless sleep to see and the other requires sleepless efforts to achieve

    Join us at Facebook

  10. #10
    Junior Member
    Join Date
    Jun 2013
    Posts
    25
    Rep Power
    0
    Thank you guys

Similar Threads

  1. Copy/Paste Excel Range/Chart into Powerpoint VBA
    By Admin in forum Excel and VBA Tips and Tricks
    Replies: 1
    Last Post: 03-13-2014, 02:59 PM
  2. Replies: 8
    Last Post: 07-01-2013, 03:52 PM
  3. Macro to copy data in specific Columns
    By Howardc in forum Excel Help
    Replies: 0
    Last Post: 04-19-2013, 10:42 AM
  4. Replies: 2
    Last Post: 04-08-2012, 09:42 AM
  5. Replies: 9
    Last Post: 09-09-2011, 02:30 AM

Posting Permissions

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