Você está na página 1de 4

http://www.scribd.

com/brian_verdine
1
Data Extraction Macro
by Brian Verdine



Because this is being shared over the internet and (for security purposes) in fixed text form, what
appears below is instruction on how to use the macro. I have used the code and have had no problems,
but it should be used at your own risk and only after verifying its contents. I am not responsible for
misuse or any damages resulting from the use of this code. The code could be edited to be malicious if
you download this from somewhere other than my Scribd account, so please think of this primarily as a
template and one of many solutions for automatically copying/pasting data in Excel.

Purpose:
This spreadsheet was developed to extract data from multiple excel workbooks where data was being
stored for participants. This macro will open a referenced excel workbook, copy data out of the
specified tab in that workbook, paste it into the output tab in an excel sheet, close the excel sheet it
copies from, and repeat for every excel sheet referenced in the "DataReferences" sheet.

The macro was written to be flexible so that it can easily be changed to extract data from any block of
columns/rows in a workbook by specifying the pathname, filename, tab, and range of cells in a
"DataReferences" tab.



Create Spreadsheet and Output Tab:

Open a new Excel Workbook and save it as MLU Data Extraction Macro.xlsm. Then create a tab
named Output. This is where your data will be pasted into.



Create and Format DataReferences Tab:

Create a sheet in the spreadsheet named DataReferences with the format of the table below. It is
important that the data below the blue colored blocks appear in exactly the cells they are currently in or
you will need to edit the macro code (for example the A2 below has to appear in the first column and
second row to be referenced properly by the code). You can probably just copy and paste this table into
Excel depending on the format in which you are viewing this.







http://www.scribd.com/brian_verdine
2
Top/Left Cell to Start Pasting Into In
Output Tab

A2


Pathname, filename, and tab in which
data to be copied appears
Where
to copy
data
from in
each
sheet
Pathname Filename
Tabna
me
StartColu
mn
EndColu
mn
StartR
ow
EndR
ow
Pathname Filename Tab
Left-
most
Column
Right-
most
Column
Top
Row
Botto
m
Row
C:\Users\BrianVerdine\Desktop\Scribd
\Data\
ExampleFile
1.xls Data B U 5 9
C:\Users\BrianVerdine\Desktop\Scribd
\Publications\
ExampleFile
2.xlsx
DataT
ab B U 5 9
C:\Users\BrianVerdine\Desktop\Scribd
\Instructions\
ExampleFile
3.xlsx
FinalD
ata D F 6 20
C:\Users\BrianVerdine\Desktop\Scribd
\Examples\
ExampleFile
4.xls
Outpu
t D F 6 20



Copy/Paste Macro Code:

Copy/paste the code below into VBA available in the Developer Tab in Excel 2010

____________________________________________________
Sub CopyPasteVariables()
'
' CopyPaste Macro
'

'Set first row of the table where the data is that specifies the variables below for the first copy/paste
operation
Dim FirstDataReferenceRow As Integer
FirstDataReferenceRow = 7

'Set the first row where data needs to be copy/pasted into
Dim FirstPasteRow As Integer
FirstPasteRow = 2

'Variable that specifies the cell to start pasting data into
http://www.scribd.com/brian_verdine
3
Dim CellToStartPasting As String
CellToStartPasting = Range("A" & FirstPasteRow).Value


Do
'The lines below specify cell references for where to find the data and how many cells to copy/paste
'Folder where data sheet appears that this macro copy/pastes from
Dim Pathname As String
Worksheets("DataReferences").Activate
Pathname = Range("A" & FirstDataReferenceRow).Value
'Name of the file to copy/paste from
Dim File As String
Worksheets("DataReferences").Activate
File = Range("B" & FirstDataReferenceRow).Value
'Name of the tab in the referenced file where data is copy/pasted from
Dim Tabname As String
Worksheets("DataReferences").Activate
Tabname = Range("C" & FirstDataReferenceRow).Value
'Cell that specifies the left most column for data you want to copy
Dim StartColumn As String
Worksheets("DataReferences").Activate
StartColumn = Range("D" & FirstDataReferenceRow).Value
'Cell that specifies the top most row for data you want to copy
Dim StartRow As String
Worksheets("DataReferences").Activate
StartRow = Range("F" & FirstDataReferenceRow).Value
'Cell that specifies the right most column for data you want to copy
Dim EndColumn As String
Worksheets("DataReferences").Activate
EndColumn = Range("E" & FirstDataReferenceRow).Value
'Cell that specifies the bottom most row for data you want to copy
Dim EndRow As String
Worksheets("DataReferences").Activate
EndRow = Range("G" & FirstDataReferenceRow).Value

'Open file that contains data to be copy/pasted, select the sheet where the data is, and copy the data
Workbooks.Open Filename:=Pathname & File
Sheets(Tabname).Select
Range(StartColumn & StartRow, EndColumn & EndRow).Select
Selection.Copy
'Select the place inside of the sheet you are pasting into that the data is going,select the cell to paste
into, and paste
Windows("MLU Data Extraction Macro.xlsm").Activate
Sheets("Output").Select
Range("A" & FirstPasteRow).Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False

http://www.scribd.com/brian_verdine
4
'Close the sheet that the data was extracted from
Windows(File).Activate
ActiveWindow.Close False

'Need to add 1 to FirstDataReferenceRow so that the macro reads data from the next line down in the
DataReferences sheet
FirstDataReferenceRow = FirstDataReferenceRow + 1
'Need to add the number of rows extracted to "FirstPasteRow" in order to allow the program to shift
down that number of rows and paste the data below the original extraction
'Activate the DataReferences Sheet so that the code below references the correct cells
Windows("MLU Data Extraction Macro.xlsm").Activate
Sheets("DataReferences").Select
FirstPasteRow = ((Range("G" & FirstDataReferenceRow).Value) - (Range("F" &
FirstDataReferenceRow).Value)) + 1 + FirstPasteRow
'The ActiveSheet part below specifies the last row in the datasheet that has data, but the datasheet
must have data in row one for this to work
Loop Until FirstDataReferenceRow - 1 = ActiveSheet.UsedRange.Rows.Count
End Sub
____________________________________________________



Run the Macro:

1. Input the filename path, the filename of the workbook to reference, the top/left most row and
column, and the right most column/bottom rows to extract into the DataReferences sheet.

2. Run the macro, which will paste those rows/columns into the output datasheet from any pathname
listed in the DataReferences sheet. Output will be stacked one on top of another, so be sure to extract
header rows if necessary.

3. I tried to program this to be flexible for other uses, so you can specify in the DataReferences sheet
which cell to paste into in the output sheet (to make room for manually added header rows).

4. By specifying the range of rows/columns in the data references sheet for each copy/paste operation,
you gain the flexibility to extract different pieces of data from various sheets without having to alter the
macro code itself.

Você também pode gostar