Copy data from open workbooks

Inactiver183192

New Member
Joined
Jun 12, 2011
Messages
15
Hi all first post!

I'm still learning but this one is probably an easy one. I just want to copy a dynamic range from all open workbooks (or windows? I still get a bit confused with which one to use) and then create a new worksheet in the one file "Datadump" and copy that data to it. So each open workbook data is now in its own worksheet in the one workbook. I was hoping to scroll through the open workbooks but I'm obviously missing something.

My second query was regarding the bypassing of the clipboard when you copy paste. Is there a way to do for a destination that will be newly created? I'd appreciate any help thank you!

Code:
Sub ImportData()
Dim WBnum As Integer
Dim WB As Workbook
 

For Each WB In Workbooks
If WB.Name <> "Datadump.xls" Then
    Range("B1", Range("B1").End(xlToRight)).Copy
    Workbooks("Datadump.xls").Worksheets.Add
    Range("B20").PasteSpecial
 
End If
Next WB
 

Excel Facts

Test for Multiple Conditions in IF?
Use AND(test, test, test, test) or OR(test, test, test, ...) as the logical_test argument of IF.
You're looping through workbooks, which is good, but for instance this line of code has no reference to the correct workbook and worksheet:

Code:
Range("B1", Range("B1").End(xlToRight)).Copy

If you do not specify these, the active workbook and worksheet will be taken...
 
Upvote 0
Hello Dixon123, and welcome to forum!

The following code, modified from what you posted, copies the data into an array, then into the newly-added target worksheet, avoiding the clipboard (as mentioned in your post).
Code:
Sub ImportData()
'copies data into an array rather than the clipboard, 
'      then puts it into a new sheet
Dim WB As Workbook
Dim vArray()
Dim LastCol As Long
 
For Each WB In Workbooks
    If WB.Name <> "DataDump.xls" Then
        With WB.Worksheets("Sheet1")
            LastCol = .Range("B1").End(xlToRight).Column
            vArray = .Range(.Cells(1, 2), .Cells(1, LastCol)).Value
        End With
        Workbooks("Datadump.xls").Worksheets.Add
 
        ActiveSheet.Range(Cells(20, 2), Cells(20, LastCol)).Value = vArray
 
    End If
Next WB
End Sub

There may be other or better ways to do this...but in my admittedly limited testing it worked OK.

Hope that helps,

Cindy
 
Upvote 0
You're looping through workbooks, which is good, but for instance this line of code has no reference to the correct workbook and worksheet:

Code:
Range("B1", Range("B1").End(xlToRight)).Copy

If you do not specify these, the active workbook and worksheet will be taken...


hi! ah I see I didn't specify since the workbooks open would have different names each time and the worksheets won't have any specific worksheet names either. Cindy your code works a charm thank you!
 
Upvote 0
Ok so I may have got a bit ahead of myself. The line

With WB.Worksheets("Sheet1")


is giving me issues if I want to delete all worksheets except for one called "Input". It gives me a subscript out of range error and I assume its to do something with the order and the fact that there will be only one worksheet to begin within in the datadump spreadsheet. If I changed Sheet1 to Input it still gives me an error. All the other data workbooks do have a worksheet named Sheet1. Any help again?
 
Upvote 0
I think I know what the problem is...the If statement excludes a file named "DataDump.xls", but I think your file is "Datadump.xls" without the extra capital D in the middle. In the IF statement, change the test to the exact spelling and capitalization of the file that will hold all of the new sheets, and let me know if that solved the problem.
Cindy
 
Upvote 0
I think I know what the problem is...the If statement excludes a file named "DataDump.xls", but I think your file is "Datadump.xls" without the extra capital D in the middle. In the IF statement, change the test to the exact spelling and capitalization of the file that will hold all of the new sheets, and let me know if that solved the problem.
Cindy


of course! cheers Cindy appreciate it! :)
 
Upvote 0

Forum statistics

Threads
1,224,521
Messages
6,179,285
Members
452,902
Latest member
Knuddeluff

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top