Copy method of worksheet class failed Error

khawarameer

Board Regular
Joined
Jun 2, 2009
Messages
152
Dear All,

I was using the below mentioned code to combine several workbooks in single worksheet. However recently i copied this code in my personal macro book (personal.xlsb) and when i run this code from macro option it gives me "Run Time Error 1004" "Copy method of Worksheet class failed".

Can any one help?

Code:
Sub Combined_Sheets()

Path = "C:\Users\bal-admin\Desktop\DMS CLG\"

Dim NumSheets As Integer
Dim NumRows As Double
Dim wks As Worksheet

Dim number As Integer
number = 1
Filename = Dir(Path & "*.*")
Do While Filename <> ""
Workbooks.Open Filename:=Path & Filename, ReadOnly:=True, CorruptLoad:=xlRepairFile
For Each Sheet In ActiveWorkbook.Sheets
ActiveSheet.Name = number
Sheet.Copy After:=ThisWorkbook.Sheets(1)
number = number + 1
Next Sheet
Workbooks(Filename).Close savechanges:=False
Filename = Dir()
Loop

Application.DisplayAlerts = False
ThisWorkbook.Worksheets("Sheet1").Delete
Application.DisplayAlerts = True

NumSheets = ActiveWorkbook.Worksheets.Count

Worksheets(1).Select
Sheets.Add
ActiveSheet.Name = "Consolidated"
For x = 1 To NumSheets
Worksheets(x + 1).Select
Range("A1").Select
Range(Selection, ActiveCell.SpecialCells(xlLastCell)).Select
Selection.Copy
Worksheets("Consolidated").Select
ActiveSheet.Paste

ActiveCell.SpecialCells(xlLastCell).Offset(1, 0).Select
Selection.End(xlToLeft).Select
Selection.End(xlToLeft).Select

Worksheets(x + 1).Select
Range("A1").Select
Next x
Worksheets("Consolidated").Select
Range("A1").Select

Application.DisplayAlerts = False

For Each wks In Worksheets
If wks.Name <> "Consolidated" Then wks.Delete
Next wks

Application.DisplayAlerts = True

End Sub
 

Excel Facts

Copy a format multiple times
Select a formatted range. Double-click the Format Painter (left side of Home tab). You can paste formatting multiple times. Esc to stop
The ThisWorkbook keyword refers to the workbook that contains the VBA code. This is now your Personal.xlsb file. That shouldn't be the destination workbook for the copied sheets.

At the start of the macro, declare and set a Workbook variable to the active workbook.
Code:
[COLOR=darkblue]Sub[/COLOR] Combined_Sheets()

Path = "C:\Users\bal-admin\Desktop\DMS CLG\"

[COLOR=darkblue]Dim[/COLOR] NumSheets [COLOR=darkblue]As[/COLOR] [COLOR=darkblue]Integer[/COLOR]
[COLOR=darkblue]Dim[/COLOR] NumRows [COLOR=darkblue]As[/COLOR] [COLOR=darkblue]Double[/COLOR]
[COLOR=darkblue]Dim[/COLOR] wks [COLOR=darkblue]As[/COLOR] Worksheet
[B][COLOR=darkblue]Dim[/COLOR] wb [COLOR=darkblue]As[/COLOR] Workbook

[COLOR=darkblue]Set[/COLOR] wb = ActiveWorkbook[/B]

Then replace the two ThisWorkbook references with wb

ws.Copy After:=wb.Sheets(1)

wb.Worksheets("Sheet1").Delete
 
Upvote 0
Thank you very much AlphaFrog for your kind explanation and code. Now my code is working properly.

Best regards

Khawarameer
 
Upvote 0

Forum statistics

Threads
1,223,246
Messages
6,170,988
Members
452,373
Latest member
TimReeks

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