Copy from one Worksheet to another

scoobadave71

New Member
Joined
Aug 9, 2011
Messages
6
Hey guys..

I am looking to copy a worksheet to a new workbook. The workbook I am copying from will only have one worksheet, but the name of the worksheet will change each time it is uploaded. I am trying to use the below code, but am failing miserably at reading the copy from worksheet:

Dim Wkb1 As Workbook ' Destination Workbook
Dim Wkb2 As Workbook ' Copy FROM workbook
Dim spath As String
spath = ActiveWorkbook.Path
Dim wkshtName As Worksheet


' set the workbook you are copying to as active and give it a name for reference
Set Wkb1 = ActiveWorkbook
' Check for and Delete Sheet 1 if exists
Application.DisplayAlerts = False
On Error Resume Next
Sheets("Fulfillment").Delete
Application.DisplayAlerts = True
On Error GoTo 0
' open the workbook you are copying from and activate it
fNameAndPath = Application.GetOpenFilename(FileFilter:="Excel Files (*.XLS; *.CSV), *.XLS; *.CSV", Title:="Select File To Be Opened")


If fNameAndPath = False Then
MsgBox ("Please Select a File")
Exit Sub
End If
' if open, close the file
Set Wkb2 = Workbooks.Open(fNameAndPath)
'Wkb2.Activate
Wkb2.Worksheets(1).Activate 'this works

With Wkb2
' copy sheet across
.Sheets(1).Copy before:=Wkb1.Worksheets(1)
'Wkb2.Worksheets(2).Copy after:=Wkb1.Worksheets(1)
End With
'close the file
Wkb2.Close SaveChanges:=False 'or True
 

Excel Facts

Lock one reference in a formula
Need 1 part of a formula to always point to the same range? use $ signs: $V$2:$Z$99 will always point to V2:Z99, even after copying
so this is where I am failing:
With Wkb2
' copy sheet across
.Sheets(1).Copy before:=Wkb1.Worksheets(1) 'FAILS I get Automation Error
'Wkb2.Worksheets(2).Copy after:=Wkb1.Worksheets(1)
End With
 
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