Copy Worksheet into Active Workbook

karabiner8

Board Regular
Joined
Jan 11, 2005
Messages
50
I have a macro that prompts the user to select several workbooks and it consolidates some data from each selected workbook into a master file (the one which the macro is run from). It copies a specific range of data from the ExportData worksheet of each of the selected workbooks. The code works fine but now I'd like add some additional code to copy an entire worksheet from the user selected workbooks. The worksheet is always called "Scorecard" and I'd like to copy it to the end of the open workbook.

I would also like to append the contents of cel A2 of Scorecard to the worksheet name (so that I don't get "Scorecard", "Scorecard(1)", "Scorecard(2)", etc. as my worksheet names after the macro runs.

Here is the code:

Sub Combine_BU_Results()

Dim x As Long, z As Variant
Dim bk As Workbook, sh As Worksheet
Dim sh1 As Worksheet

'
' Change the next line to reflect the proper
' name and workbook where the data will be
' consolidated
'

Set sh = Workbooks("Roll Up Master.xls").Worksheets("Consolidate")

z = Application.GetOpenFilename(FileFilter:= _
"Excel files (*.xls), *.xls", MultiSelect:=True)
If Not IsArray(z) Then
MsgBox "Nothing selected"
Exit Sub
End If

'Open loop for action to be taken on all selected workbooks.

Application.ScreenUpdating = False ' turn screen updating off
For x = 1 To UBound(z)

'Open the workbook(s) that were selected.
Set bk = Workbooks.Open(z(x))
'Check if sheet ExportData worksheet exists
On Error Resume Next
Set sh1 = bk.Worksheets("ExportData")
On Error GoTo 0
' if it exists, copy the data
If Not sh1 Is Nothing Then
Set rng = sh1.Range("A1:k100")
Set rng1 = sh.Cells(Rows.Count, 2).End(xlUp)(10)
rng.Copy
rng1.PasteSpecial xlValues
rng1.PasteSpecial xlFormats
' sh.Columns.AutoFit add this line back in if you to autofit columns widths

End If

'Close the Roll Up Master workbook without saving it.
Application.CutCopyMode = False
bk.Close False

Next x


'Message box to inform user the job is complete.
Application.ScreenUpdating = False
MsgBox "The import is complete.", 64, "Done !!"
Application.ScreenUpdating = False
Sheets("Scorecard").Select
Range("A1").Select
End Sub
 

Excel Facts

Format cells as currency
Select range and press Ctrl+Shift+4 to format cells as currency. (Shift 4 is the $ sign).

Forum statistics

Threads
1,223,214
Messages
6,170,774
Members
452,353
Latest member
strainu

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