Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default Help with copying a sheet from One workbook to another workbook

I have one excel file named Test1.xlsx with sheet named "List". I want to copy the sheet List into another file called Test2.xlsx. Both files are existing. I also want to delete the Sheet "List" in Test2.xlsx before copying.. Finally I want to save and close both the files.

I am new to Excel VBA.
I would appreciate if anybody can help me in showing how this can be done in Excel VBA 2013.



  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5,302
Default Help with copying a sheet from One workbook to another workbook

On 04/02/2014 21:46, wrote:
I have one excel file named Test1.xlsx with sheet named "List".
I want to copy the sheet List into another file called Test2.xlsx.
Both files are existing. I also want to delete the Sheet "List"
in Test2.xlsx before copying.. Finally I want to save and close
both the files.

I am new to Excel VBA.
I would appreciate if anybody can help me in showing how this can be
done in Excel VBA 2013.

Hi PPDS,

Alt-Fll to open the VBA editor
Alt-IM to insert a new code module
At the flashing cursor, paste the following code:

'=============
Option Explicit

'-------------
Public Sub CopySheet()
Dim srcWB As Workbook
Dim destWB As Workbook
Dim srcSH As Worksheet
Dim errStr As String
Dim CalcMode As Long
Const aStr As String = "Workbook"
Const bStr As String = "Worksheet"
Const srcWbName As String = "Test1.xlsx" '<<==== CHANGE
Const destWbName As String = "Test2.xlsx" '<<==== CHANGE
Const shName As String = "List"

With Application
CalcMode = .Calculation
.Calculation = xlCalculationManual
.ScreenUpdating = False
.EnableEvents = False
End With

On Error Resume Next
Set srcWB = Workbooks(srcWbName)
If Not srcWB Is Nothing Then
Set destWB = Workbooks(destWbName)
If Not destWB Is Nothing Then
Application.DisplayAlerts = False
destWB.Sheets(shName).Delete
Application.DisplayAlerts = True
Set srcSH = srcWB.Sheets(shName)
If Not srcSH Is Nothing Then
With destWB
srcSH.Copy After:=.Sheets(.Sheets.Count)
End With
Else
errStr = bStr & " " & shName
GoTo XIT
End If
Else
errStr = aStr & " " & destWbName
GoTo XIT
End If
Else
errStr = srcWbName
GoTo XIT
End If

XIT:
If Len(errStr) Then
Call MsgBox(Prompt:=errStr & " not found", _
Buttons:=vbCritical, _
Title:="ERROR")
Else
srcWB.Close SaveChanges:=True
destWB.Close SaveChanges:=True
End If

With Application
.ScreenUpdating = True
.EnableEvents = True
.Calculation = CalcMode
End With
End Sub
'<<=============

Alt-Q to close the VBA editor and return to Excel
Alt-F8 to open the macro window
Select CopySheet | Run | ok


===
Regards,
Norman
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
copying one sheet to another workbook boschuster50 Excel Discussion (Misc queries) 3 April 30th 09 10:40 PM
Copying sheet containing GetPivotData, new sheet references old workbook! rivkarak Excel Programming 0 January 18th 07 11:31 AM
Copying data from workbook/sheets to another workbook/sheet yukon_phil Excel Programming 0 July 26th 06 07:33 PM
loop through a column on a workbook copying data on each row to another workbook, then copy data back to the original workbook burl_rfc Excel Programming 1 April 1st 06 08:48 PM
Copying sheet to another workbook using VBA... Trevor[_4_] Excel Programming 1 August 27th 04 11:30 PM


All times are GMT +1. The time now is 07:56 AM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"