#1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 355
Default Sheet copy

Greetings
My code:

Private Sub CommandButton1_Click()
'

'
Dim sNewSheet As String

sNewSheet = Range("I10").Value
On Error Resume Next
If SheetExists(sNewSheet) = True Then
MsgBox "A requestRequest already exists for" & sNewSheet
Else
Sheets("template").Select
Sheets("template").Copy After:=Worksheets(Worksheets.Count)
Cells.Select
Selection.Copy
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone,
SkipBlanks _
:=False, Transpose:=False
Range("A1").Select
ActiveSheet.Name = Range("i10").Value
ActiveWindow.SelectedSheets.PrintOut Copies:=1, Collate:=True
End If
On Error GoTo 0

End Sub

Function SheetExists(SName As String, _
Optional ByVal WB As Workbook) As Boolean
'Chip Pearson
On Error Resume Next
If WB Is Nothing Then Set WB = ThisWorkbook
SheetExists = CBool(Len(WB.Sheets(SName).Name))
End Function

What do I need to add so the button is not copied to the new sheet?

Thanks!
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 167
Default Sheet copy

Sandy,

You are coping the entire sheet. This included any controls on the sheet
and any code modules included on the sheet. I'm not sure if there is a way
to not copy those items when doing a sheet copy, but you can always "post
process."

I assumed that the "template" sheet is the sheet your code is in. The code
below will duplicate the sheet named "template," remove any code in the new
sheet module, and remove the button named "CommanButton1" from the new sheet.
NOTE: You must use the button (NAME) not the Caption.

If the "template" sheet is not the sheet your code is on, or you have code
on that sheet that you want to keep then you can replace "RemoveButton" in
the CommandButton1_Click procedure with
"ActiveSheet.Shapes("CommandButton1").Delete and remove the RemoveButton
procedure.

NOTE: To use the RemoveButton procedure you will need to have a reference to
"Microsoft Visual Basic For Applications Extensibility 5.3." If you don't
know how to do this, visit http://www.cpearson.com/excel/vbe.htm


Option Explicit
Private Sub CommandButton1_Click()
'

'
Dim sNewSheet As String

sNewSheet = Range("I10").Value
On Error Resume Next
If SheetExists(sNewSheet) = True Then
MsgBox "A request already exists for" & sNewSheet
Else
Sheets("template").Select
Sheets("template").Copy After:=Worksheets(Worksheets.Count)
ActiveSheet.Name = Range("i10").Value
RemoveButton
ActiveWindow.SelectedSheets.PrintOut Copies:=1, Collate:=True
End If
On Error GoTo 0

End Sub
Sub RemoveButton()
Dim VBCodeMod As CodeModule
Dim StartLine As Long
Dim HowManyLines As Long

Set VBCodeMod =
ThisWorkbook.VBProject.VBComponents(ActiveSheet.Co deName).CodeModule
With VBCodeMod
StartLine = 1
HowManyLines = .CountOfLines
.DeleteLines StartLine, HowManyLines
End With
ActiveSheet.Shapes("CommandButton1").Delete
End Sub
Function SheetExists(SName As String, _
Optional ByVal WB As Workbook) As Boolean
'Chip Pearson
On Error Resume Next
If WB Is Nothing Then Set WB = ThisWorkbook
SheetExists = CBool(Len(WB.Sheets(SName).Name))
End Function

Let me know if you have any questions.

Mike
mike.milligan AT ngc DOT com

"Sandy" wrote:

Greetings
My code:

Private Sub CommandButton1_Click()
'

'
Dim sNewSheet As String

sNewSheet = Range("I10").Value
On Error Resume Next
If SheetExists(sNewSheet) = True Then
MsgBox "A requestRequest already exists for" & sNewSheet
Else
Sheets("template").Select
Sheets("template").Copy After:=Worksheets(Worksheets.Count)
Cells.Select
Selection.Copy
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone,
SkipBlanks _
:=False, Transpose:=False
Range("A1").Select
ActiveSheet.Name = Range("i10").Value
ActiveWindow.SelectedSheets.PrintOut Copies:=1, Collate:=True
End If
On Error GoTo 0

End Sub

Function SheetExists(SName As String, _
Optional ByVal WB As Workbook) As Boolean
'Chip Pearson
On Error Resume Next
If WB Is Nothing Then Set WB = ThisWorkbook
SheetExists = CBool(Len(WB.Sheets(SName).Name))
End Function

What do I need to add so the button is not copied to the new sheet?

Thanks!

  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 355
Default Sheet copy

Thanks
Works great!

"crazybass2" wrote:

Sandy,

You are coping the entire sheet. This included any controls on the sheet
and any code modules included on the sheet. I'm not sure if there is a way
to not copy those items when doing a sheet copy, but you can always "post
process."

I assumed that the "template" sheet is the sheet your code is in. The code
below will duplicate the sheet named "template," remove any code in the new
sheet module, and remove the button named "CommanButton1" from the new sheet.
NOTE: You must use the button (NAME) not the Caption.

If the "template" sheet is not the sheet your code is on, or you have code
on that sheet that you want to keep then you can replace "RemoveButton" in
the CommandButton1_Click procedure with
"ActiveSheet.Shapes("CommandButton1").Delete and remove the RemoveButton
procedure.

NOTE: To use the RemoveButton procedure you will need to have a reference to
"Microsoft Visual Basic For Applications Extensibility 5.3." If you don't
know how to do this, visit http://www.cpearson.com/excel/vbe.htm


Option Explicit
Private Sub CommandButton1_Click()
'

'
Dim sNewSheet As String

sNewSheet = Range("I10").Value
On Error Resume Next
If SheetExists(sNewSheet) = True Then
MsgBox "A request already exists for" & sNewSheet
Else
Sheets("template").Select
Sheets("template").Copy After:=Worksheets(Worksheets.Count)
ActiveSheet.Name = Range("i10").Value
RemoveButton
ActiveWindow.SelectedSheets.PrintOut Copies:=1, Collate:=True
End If
On Error GoTo 0

End Sub
Sub RemoveButton()
Dim VBCodeMod As CodeModule
Dim StartLine As Long
Dim HowManyLines As Long

Set VBCodeMod =
ThisWorkbook.VBProject.VBComponents(ActiveSheet.Co deName).CodeModule
With VBCodeMod
StartLine = 1
HowManyLines = .CountOfLines
.DeleteLines StartLine, HowManyLines
End With
ActiveSheet.Shapes("CommandButton1").Delete
End Sub
Function SheetExists(SName As String, _
Optional ByVal WB As Workbook) As Boolean
'Chip Pearson
On Error Resume Next
If WB Is Nothing Then Set WB = ThisWorkbook
SheetExists = CBool(Len(WB.Sheets(SName).Name))
End Function

Let me know if you have any questions.

Mike
mike.milligan AT ngc DOT com

"Sandy" wrote:

Greetings
My code:

Private Sub CommandButton1_Click()
'

'
Dim sNewSheet As String

sNewSheet = Range("I10").Value
On Error Resume Next
If SheetExists(sNewSheet) = True Then
MsgBox "A requestRequest already exists for" & sNewSheet
Else
Sheets("template").Select
Sheets("template").Copy After:=Worksheets(Worksheets.Count)
Cells.Select
Selection.Copy
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone,
SkipBlanks _
:=False, Transpose:=False
Range("A1").Select
ActiveSheet.Name = Range("i10").Value
ActiveWindow.SelectedSheets.PrintOut Copies:=1, Collate:=True
End If
On Error GoTo 0

End Sub

Function SheetExists(SName As String, _
Optional ByVal WB As Workbook) As Boolean
'Chip Pearson
On Error Resume Next
If WB Is Nothing Then Set WB = ThisWorkbook
SheetExists = CBool(Len(WB.Sheets(SName).Name))
End Function

What do I need to add so the button is not copied to the new sheet?

Thanks!

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 rows from one Data sheet to another sheet based on cell conte John McKeon Excel Discussion (Misc queries) 2 May 15th 10 06:49 AM
Auto Copy/autofill Text from sheet to sheet if meets criteria Joyce Excel Discussion (Misc queries) 0 November 20th 08 11:05 PM
Search for rows in one sheet and copy into another sheet based on customer id [email protected] Excel Worksheet Functions 1 October 22nd 07 03:09 AM
relative sheet references ala sheet(-1)!B11 so I can copy a sheet. RonMc5 Excel Discussion (Misc queries) 9 February 3rd 05 12:51 AM
providing a sheet-copy event or copy CustomProperties Carlos Cortes Excel Programming 2 November 11th 04 08:24 AM


All times are GMT +1. The time now is 03:53 PM.

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"