Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
QB QB is offline
external usenet poster
 
Posts: 57
Default copy worksheet

How can I copy a sheet entitled "Planning" as the first sheet in a workbook
name "tmplt_planning.xlsm" which is in the same folder as the current
workbook?

Thank you

QB
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default copy worksheet


Hi there,

You could use something like this ...

Option Explicit

Sub Copy_Planning_Worksheet()
Dim wb As Workbook, wbDest As Workbook, ws As Worksheet
Dim bWBOpen As Boolean, sName As String
Set wb = ThisWorkbook
If WSEXISTS("Planning", wb) = False Then
MsgBox "Worksheet (Planning) was not found in this workbook!",
vbExclamation, "ERROR!"
Exit Sub
End If
Set ws = wb.Worksheets("Planning")
sName = "tmplt_planning.xlsm"
If ISWBOPEN(sName) = True Then
Set wbDest = Workbooks(sName)
bWBOpen = False
Else
Set wbDest = Workbooks.Open(wb.Path & Application.PathSeparator
& sName)
bWBOpen = True
End If
If WSEXISTS("Planning", wbDest) = True Then
MsgBox "Worksheet already exists in target workbook (" & sName
& ")!", vbExclamation, "ERROR!"
Else
ws.Copy befo=wbDest.Worksheets(1)
End If
If bWBOpen = True Then
wbDest.Close SaveChanges:=True
End If
End Sub

Public Function ISWBOPEN(wbName As String) As Boolean
'Originally found written by Jake Marx
On Error Resume Next
ISWBOPEN = Len(Workbooks(wbName).Name)
End Function

Public Function WSEXISTS(wsName As String, Optional wkb As Workbook) As
Boolean
If wkb Is Nothing Then
If ActiveWorkbook Is Nothing Then Exit Function
Set wkb = ActiveWorkbook
End If
On Error Resume Next
WSEXISTS = CBool(Len(wkb.Worksheets(wsName).Name))
End Function


HTH


--
Zack Barresse
------------------------------------------------------------------------
Zack Barresse's Profile: http://www.thecodecage.com/forumz/member.php?userid=119
View this thread: http://www.thecodecage.com/forumz/sh...d.php?t=158629

Microsoft Office Help

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
copy method of worksheet class failed: trying to copy a hidden she sam Excel Programming 4 August 8th 09 11:19 PM
copy method of worksheet class failed: trying to copy a hidden she sam Excel Programming 0 August 7th 09 11:16 PM
copy method of worksheet class failed: trying to copy a hidden she sam Excel Programming 0 August 7th 09 11:16 PM
copy range on every worksheet (diff names) to a master worksheet (to be created) Bernie[_2_] Excel Programming 2 September 22nd 04 03:30 PM
Code to copy range vs Copy Entire Worksheet - can't figure it out Mike Taylor Excel Programming 1 April 15th 04 08:34 PM


All times are GMT +1. The time now is 04:06 AM.

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

About Us

"It's about Microsoft Excel"