Solved

Loop Copy data from different  Workbook

Posted on 2011-03-15
12
198 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
  • 7
  • 5
12 Comments
 
LVL 18

Expert Comment

by:Curt Lindstrom
Comment Utility
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
Comment Utility
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
Comment Utility
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
 

Author Comment

by:Cartillo
Comment Utility
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
Comment Utility
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
Comment Utility
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
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 
LVL 18

Expert Comment

by:Curt Lindstrom
Comment Utility
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
Comment Utility
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
Comment Utility
Hi Cartillo,

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

Curt
0
 

Author Comment

by:Cartillo
Comment Utility
Hi Curt,

It works perfectly. Thanks for the great help.
0
 

Author Closing Comment

by:Cartillo
Comment Utility
Hi Curt,

Thanks for the help.
0
 
LVL 18

Expert Comment

by:Curt Lindstrom
Comment Utility
Great! Glad to help!

Cheers,
Curt
0

Featured Post

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Sparklines have been introduced with Excel 2010 and are a useful tool for creating small in-cell charts, used for example in dashboards. Excel 2010 offers three different types of Sparklines: Line, Column and Win/Loss. What it does not offer is a…
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…
The viewer will learn how to use the =DISCRINV command to create a discrete random variable, use this command to model a set of probabilities and outcomes in a Monte Carlo simulation, and learn how to find the standard deviation of a set of probabil…
This Micro Tutorial will demonstrate on a Mac how to change the sort order for chart legend values and decrpyt the intimidating chart menu.

763 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now