Solved

Loop Copy data from different  Workbook

Posted on 2011-03-15
12
202 Views
Last Modified: 2012-05-11
Hi Experts,

I would like to request Experts help to copy data (only number) from Column A for all sheets at Workbook1 and paste it at “CopyData” Workbook at “Data” sheet at Column A. I need to loop this function so that I can copy other workbooks which is also having the similar data format (currently I’m having 14 workbook). Different workbook data need to copy  at  different  Column at “CopyData” workbook. Hope Experts will help me to create this macro.



CopyData.xls
Workbook1.xls
0
Comment
Question by:Cartillo
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 7
  • 5
12 Comments
 
LVL 18

Expert Comment

by:Curt Lindstrom
ID: 35147379
Try these 2 macros. The assume the data to be copied starts at row 8 on each sheet in your workbooks that you copy from. Try the code in the attached file. You need to provide the correct filename and path for your workbook(s)

Curt
Sub Copy_Data()
    Dim Ws As Worksheet
    Dim Wb As Workbook
    Dim i As Long, j As Long, k As Long, Col As Long
    Dim SourceFile As String
    Dim FlPath As String

    FlPath = "c:/Mydata/" 'Change to the path where you store your workbooks to be copied
    For Col = 1 To 1    'Change the last 1 to the number filenames you got entered on row 1 and stored in your file path
        FlName = ThisWorkbook.Worksheets("Data").Cells(1, Col)
        SourceFile = FlPath & FlName

        Workbooks.Open Filename:=SourceFile
        Set Wb = ActiveWorkbook
        k = 2
        For Each Ws In Wb.Worksheets
            With Ws
                For j = 8 To xlLastRow(Ws.Name)
                    i = j + k - 8
                    If .Cells(j, 1).Value = "" Then
                        k = k - 1
                    Else
                        ThisWorkbook.Worksheets("Data").Cells(i, 1) = .Cells(j, 1)
                    End If
                Next j
            End With
            k = i + 1
        Next Ws
    Next Col
    ThisWorkbook.Activate
    Range("A1").Select
End Sub
Function xlLastRow(Optional WorksheetName As String) As Long
     
    'Check for optional worksheetname else use activesheet
    If WorksheetName = vbNullString Then
        WorksheetName = ActiveSheet.Name
    End If
    
     '    find the last populated row in a worksheet
    With Worksheets(WorksheetName)
        xlLastRow = .Cells.Find("*", .Cells(1), xlFormulas, _
        xlWhole, xlByRows, xlPrevious).Row
    End With
     
End Function

Open in new window

CopyData.xls
0
 

Author Comment

by:Cartillo
ID: 35148903
Hi Curt,

Can I select the workbook instead of naming it. I have various workbook name. Also each workbook need to copied at different column. Hope you add this feature.  
0
 
LVL 18

Expert Comment

by:Curt Lindstrom
ID: 35152199
You can select the workbooks if you want to do that.

Just to clarify what the idea with the current code is:

1. You have all your workbooks in the same folder
2. You replace the names on row 1 in the CopyData file with the real names.
3. You change line 8 in the code and enter your own path
4. You change line 9 in the code to how many workbooks you are processing

You press the button and all the workbooks are processed and copied to the correct columns.

Before I attempt to update the macro:

1. Do you want to select all the workbooks in one go? For that to work they need to be in their own unique folder.
2. Or do you want to select the workbooks one by one from different folders?

Curt
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:Cartillo
ID: 35152883
Hi Curt,

> Do you want to select all the workbooks in one go? For that to work they need to be in their own unique folder.

- I'll select the workbooks one by one from different folders
0
 
LVL 18

Expert Comment

by:Curt Lindstrom
ID: 35153104
Try this code. It's included in the attached file where you can try it.

Cheers,
Curt
Sub Copy_Data()
    Dim Ws As Worksheet
    Dim Wb As Workbook
    Dim i As Long, j As Long, k As Long, Col As Long
    Dim SourceFile As String, Response As String

    Col = 1
    Do Until LCase(Left(Response, 1)) = "n"
        SourceFile = Application.GetOpenFilename(Title:="Please select your file from the folder that contains the data, then click Open.")
        If SourceFile = "False" Then
            MsgBox ("No file selected!")
            Exit Sub
        End If


        Workbooks.Open SourceFile
        Set Wb = ActiveWorkbook
        k = 2
        For Each Ws In Wb.Worksheets
            With Ws
                For j = 8 To xlLastRow(Ws.Name)
                    i = j + k - 8
                    If .Cells(j, 1).Value = "" Then
                        k = k - 1
                    Else
                        ThisWorkbook.Worksheets("Data").Cells(i, Col) = .Cells(j, 1)
                    End If
                Next j
            End With
            k = i + 1
        Next Ws
        Wb.Close
        Response = InputBox("Process another workbook?", , "Yes")
        Col = Col + 1
    Loop
    ThisWorkbook.Activate
    Range("A1").Select
End Sub
Function xlLastRow(Optional WorksheetName As String) As Long
     
    'Check for optional worksheetname else use activesheet
    If WorksheetName = vbNullString Then
        WorksheetName = ActiveSheet.Name
    End If
    
     '    find the last populated row in a worksheet
    With Worksheets(WorksheetName)
        xlLastRow = .Cells.Find("*", .Cells(1), xlFormulas, _
        xlWhole, xlByRows, xlPrevious).Row
    End With
     
End Function

Open in new window

CopyData-2.xls
0
 

Author Comment

by:Cartillo
ID: 35157877
Hi,

Few workbook facing problem whilst copying the data, shows error as "Object variable or with block variable not set at this line:  


 xlLastRow = .Cells.Find("*", .Cells(1), xlFormulas, _
        xlWhole, xlByRows, xlPrevious).Row

How to fix this?
0
 
LVL 18

Expert Comment

by:Curt Lindstrom
ID: 35161325
Let's get rid of the Function. Try this one. If you still have problems maybe you can post a sample of a failing workbook.
The code is include in the attached file.

Curt
Sub Copy_Data()
    Dim Ws As Worksheet
    Dim Wb As Workbook
    Dim i As Long, j As Long, k As Long, Col As Long, xlLastRow As Long
    Dim SourceFile As String, Response As String

    Col = 1
    Do Until LCase(Left(Response, 1)) = "n"
        SourceFile = Application.GetOpenFilename(Title:="Please select your file from the folder that contains the data, then click Open.")
        If SourceFile = "False" Then
            MsgBox ("No file selected!")
            Exit Sub
        End If


        Workbooks.Open SourceFile
        Set Wb = ActiveWorkbook
        k = 2
        For Each Ws In Wb.Worksheets
        xlLastRow = Range("A65536").End(xlUp).Row
            With Ws
                For j = 8 To xlLastRow
                    i = j + k - 8
                    If .Cells(j, 1).Value = "" Then
                        k = k - 1
                    Else
                        ThisWorkbook.Worksheets("Data").Cells(i, Col) = .Cells(j, 1)
                    End If
                Next j
            End With
            k = i + 1
        Next Ws
        Wb.Close
        Response = InputBox("Process another workbook?", , "Yes")
        Col = Col + 1
    Loop
    ThisWorkbook.Activate
    Range("A1").Select
End Sub

Open in new window

CopyData-3.xls
0
 
LVL 18

Accepted Solution

by:
Curt Lindstrom earned 500 total points
ID: 35161395
Sorry, a small error with the new line. Change Line 20 in the code to:

        xlLastRow = Ws.Range("A65536").End(xlUp).Row

Fixed in attached file

Curt

CopyData-3.xls
0
 
LVL 18

Expert Comment

by:Curt Lindstrom
ID: 35177580
Hi Cartillo,

Any more problems with this or is everything working ok now?

Curt
0
 

Author Comment

by:Cartillo
ID: 35177760
Hi Curt,

It works perfectly. Thanks for the great help.
0
 

Author Closing Comment

by:Cartillo
ID: 35177764
Hi Curt,

Thanks for the help.
0
 
LVL 18

Expert Comment

by:Curt Lindstrom
ID: 35177782
Great! Glad to help!

Cheers,
Curt
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Compare Excel Spreadsheet. 4 33
Need Excel Formula for extract out date only 17 38
Excel formula that extracts out name of url 6 42
Tricky Shapes formula part 4 4 14
This code takes an Excel list of URL’s and adds a header titled “URL List”. It then searches through all URL’s in column “A”, looking for duplicates. When a duplicate is found, it is moved to the top of the list. The duplicate URL’s are then highlig…
Access developers frequently have requirements to interact with Excel (import from or output to) in their applications.  You might be able to accomplish this with the TransferSpreadsheet and OutputTo methods, but in this series of articles I will di…
This Micro Tutorial demonstrate the bugs in Microsoft Excel for Mac with Pivot Charts.
This Micro Tutorial demonstrates in Microsoft Excel how to consolidate your marketing data by creating an interactive charts using form controls. This creates cool drop-downs for viewers of your chart to choose from.

733 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question