PC Review


Reply
Thread Tools Rate Thread

Copying command buttons

 
 
oldjay
Guest
Posts: n/a
 
      24th Nov 2009
I want to copy a sheet and its command buttons to a new sheet named for the
present date i.e.(11/24/09). Buttons created from the Toolbox in Excel 2003

oldjay
 
Reply With Quote
 
 
 
 
OssieMac
Guest
Posts: n/a
 
      24th Nov 2009
Hi Oldjay,

Note when copying a worksheet with the below method then the command buttons
with their associated VBA code will be copied with them. Therefore it is a
good idea to place most of the code in a standard module and just call the
code from the command buttons. That way you are not duplicating code
throughout your workbook and if you need to modifiy the code then it only
needs to be done in the one place.

Also you cannot use slashes (/) in the worksheet name. I changed them to
hyphens.

Note the comments in the code.
Sub CopyWorkSheet()

Dim wsShtToCopy As Worksheet
Dim strNewShtName As String
Dim wsNewSht As Worksheet

'Edit "Sheet1" to your worksheet name
Set wsShtToCopy = Sheets("Sheet1")

'Assign proposed new worksheet name to variable
strNewShtName = Format(Date, "mm-dd-yy")

'Test for new sheet name already existing
'by attempting to assign to a variable
On Error Resume Next
Set wsNewSht = Sheets(strNewShtName)

'If error is zero then worksheet exists
If Err.Number = 0 Then 'No error
MsgBox "Worksheet " & strNewShtName _
& " already exists"
Exit Sub
Else
On Error GoTo 0 'Resume error trapping ASAP
wsShtToCopy.Copy Before:=Sheets(1)
ActiveSheet.Name = strNewShtName
End If

End Sub

--
Regards,

OssieMac


 
Reply With Quote
 
Peter T
Guest
Posts: n/a
 
      24th Nov 2009
Sub test()
Dim ws As Worksheet
Set ws = Worksheets("Sheet1")
ws.Copy after:=ws

' will fail if a sheet named today's date already exits
ActiveSheet.Name = Date
End Sub

This will copy the controls and code behind the sheet, as well as contents.

Regards,
Peter T

"oldjay" <(E-Mail Removed)> wrote in message
news:CE711085-5B76-4C3B-A028-(E-Mail Removed)...
>I want to copy a sheet and its command buttons to a new sheet named for the
> present date i.e.(11/24/09). Buttons created from the Toolbox in Excel
> 2003
>
> oldjay



 
Reply With Quote
 
Peter T
Guest
Posts: n/a
 
      24th Nov 2009
As OssieMac points out best ensure there are no "/" in the new sheet name
change

> ActiveSheet.Name = Date


to (one way)
ActiveSheet.Name = Replace(Date, "/", "-")

(my default format is has "-" separators hence I forgot)

Regards,
Peter T


"Peter T" <peter_t@discussions> wrote in message
news:ONF%(E-Mail Removed)...
> Sub test()
> Dim ws As Worksheet
> Set ws = Worksheets("Sheet1")
> ws.Copy after:=ws
>
> ' will fail if a sheet named today's date already exits
> ActiveSheet.Name = Date
> End Sub
>
> This will copy the controls and code behind the sheet, as well as
> contents.
>
> Regards,
> Peter T
>
> "oldjay" <(E-Mail Removed)> wrote in message
> news:CE711085-5B76-4C3B-A028-(E-Mail Removed)...
>>I want to copy a sheet and its command buttons to a new sheet named for
>>the
>> present date i.e.(11/24/09). Buttons created from the Toolbox in Excel
>> 2003
>>
>> oldjay

>
>



 
Reply With Quote
 
OssieMac
Guest
Posts: n/a
 
      24th Nov 2009
An afterthought and not sure of your level in VBA so it might help. You can
change the following line to the active sheet in lieu of a specific sheet.
Set wsShtToCopy = Sheets("Sheet1")

Can change to this so it works from any worksheet.
Set wsShtToCopy = ActiveSheet

Also there are other options of where the worksheet should be placed in the
workbook. Here are a couple of examples.

wsShtToCopy.Copy After:=Sheets(Sheets.Count) 'After last sheet
wsShtToCopy.Copy After:=ActiveSheet

Can use Before in lieu of After.

--
Regards,

OssieMac


 
Reply With Quote
 
OssieMac
Guest
Posts: n/a
 
      24th Nov 2009
Hi Peter,

It is better to use code that will work for all users.
ActiveSheet.Name = Replace(Date, "/", "-") will fail if the user does not
have slashes in the date format.

By using the Format function you can set the sheet name with characters that
you know will not fail irrespective of the user's default date format.

--
Regards,

OssieMac


"Peter T" wrote:

> As OssieMac points out best ensure there are no "/" in the new sheet name
> change
>
> > ActiveSheet.Name = Date

>
> to (one way)
> ActiveSheet.Name = Replace(Date, "/", "-")
>
> (my default format is has "-" separators hence I forgot)
>
> Regards,
> Peter T
>
>
> "Peter T" <peter_t@discussions> wrote in message
> news:ONF%(E-Mail Removed)...
> > Sub test()
> > Dim ws As Worksheet
> > Set ws = Worksheets("Sheet1")
> > ws.Copy after:=ws
> >
> > ' will fail if a sheet named today's date already exits
> > ActiveSheet.Name = Date
> > End Sub
> >
> > This will copy the controls and code behind the sheet, as well as
> > contents.
> >
> > Regards,
> > Peter T
> >
> > "oldjay" <(E-Mail Removed)> wrote in message
> > news:CE711085-5B76-4C3B-A028-(E-Mail Removed)...
> >>I want to copy a sheet and its command buttons to a new sheet named for
> >>the
> >> present date i.e.(11/24/09). Buttons created from the Toolbox in Excel
> >> 2003
> >>
> >> oldjay

> >
> >

>
>
> .
>

 
Reply With Quote
 
Peter T
Guest
Posts: n/a
 
      24th Nov 2009
Hello OssieMac,

No that's no problem, try this -

MsgBox Replace("abc", "x", "y") ' abc

Probably I'd use the Format function. However if I wanted to distribute I
wouldn't want to prejudge if user wants dd-mm-yyyy or mm-dd-yyyy. The
Replace function is a simple way to ensure no slashes appear without
changing the order, and an as an alternative to what you had already
suggested, I did indicate. Of course there are other ways too, not least
Format.

Not sure if Date could ever be returned with some other invalid characters,
if so would need to think again.

Regards,
Peter T


"OssieMac" <(E-Mail Removed)> wrote in message
news:4EF6C6A4-81B7-425D-8F2C-(E-Mail Removed)...
> Hi Peter,
>
> It is better to use code that will work for all users.
> ActiveSheet.Name = Replace(Date, "/", "-") will fail if the user does not
> have slashes in the date format.
>
> By using the Format function you can set the sheet name with characters
> that
> you know will not fail irrespective of the user's default date format.
>
> --
> Regards,
>
> OssieMac
>
>
> "Peter T" wrote:
>
>> As OssieMac points out best ensure there are no "/" in the new sheet name
>> change
>>
>> > ActiveSheet.Name = Date

>>
>> to (one way)
>> ActiveSheet.Name = Replace(Date, "/", "-")
>>
>> (my default format is has "-" separators hence I forgot)
>>
>> Regards,
>> Peter T
>>
>>
>> "Peter T" <peter_t@discussions> wrote in message
>> news:ONF%(E-Mail Removed)...
>> > Sub test()
>> > Dim ws As Worksheet
>> > Set ws = Worksheets("Sheet1")
>> > ws.Copy after:=ws
>> >
>> > ' will fail if a sheet named today's date already exits
>> > ActiveSheet.Name = Date
>> > End Sub
>> >
>> > This will copy the controls and code behind the sheet, as well as
>> > contents.
>> >
>> > Regards,
>> > Peter T
>> >
>> > "oldjay" <(E-Mail Removed)> wrote in message
>> > news:CE711085-5B76-4C3B-A028-(E-Mail Removed)...
>> >>I want to copy a sheet and its command buttons to a new sheet named for
>> >>the
>> >> present date i.e.(11/24/09). Buttons created from the Toolbox in Excel
>> >> 2003
>> >>
>> >> oldjay
>> >
>> >

>>
>>
>> .
>>



 
Reply With Quote
 
oldjay
Guest
Posts: n/a
 
      25th Nov 2009
You guys have got me confused. What is the final code. I do not care about
the date format. What ever is the easiest.

oldjay

"Peter T" wrote:

> Hello OssieMac,
>
> No that's no problem, try this -
>
> MsgBox Replace("abc", "x", "y") ' abc
>
> Probably I'd use the Format function. However if I wanted to distribute I
> wouldn't want to prejudge if user wants dd-mm-yyyy or mm-dd-yyyy. The
> Replace function is a simple way to ensure no slashes appear without
> changing the order, and an as an alternative to what you had already
> suggested, I did indicate. Of course there are other ways too, not least
> Format.
>
> Not sure if Date could ever be returned with some other invalid characters,
> if so would need to think again.
>
> Regards,
> Peter T
>
>
> "OssieMac" <(E-Mail Removed)> wrote in message
> news:4EF6C6A4-81B7-425D-8F2C-(E-Mail Removed)...
> > Hi Peter,
> >
> > It is better to use code that will work for all users.
> > ActiveSheet.Name = Replace(Date, "/", "-") will fail if the user does not
> > have slashes in the date format.
> >
> > By using the Format function you can set the sheet name with characters
> > that
> > you know will not fail irrespective of the user's default date format.
> >
> > --
> > Regards,
> >
> > OssieMac
> >
> >
> > "Peter T" wrote:
> >
> >> As OssieMac points out best ensure there are no "/" in the new sheet name
> >> change
> >>
> >> > ActiveSheet.Name = Date
> >>
> >> to (one way)
> >> ActiveSheet.Name = Replace(Date, "/", "-")
> >>
> >> (my default format is has "-" separators hence I forgot)
> >>
> >> Regards,
> >> Peter T
> >>
> >>
> >> "Peter T" <peter_t@discussions> wrote in message
> >> news:ONF%(E-Mail Removed)...
> >> > Sub test()
> >> > Dim ws As Worksheet
> >> > Set ws = Worksheets("Sheet1")
> >> > ws.Copy after:=ws
> >> >
> >> > ' will fail if a sheet named today's date already exits
> >> > ActiveSheet.Name = Date
> >> > End Sub
> >> >
> >> > This will copy the controls and code behind the sheet, as well as
> >> > contents.
> >> >
> >> > Regards,
> >> > Peter T
> >> >
> >> > "oldjay" <(E-Mail Removed)> wrote in message
> >> > news:CE711085-5B76-4C3B-A028-(E-Mail Removed)...
> >> >>I want to copy a sheet and its command buttons to a new sheet named for
> >> >>the
> >> >> present date i.e.(11/24/09). Buttons created from the Toolbox in Excel
> >> >> 2003
> >> >>
> >> >> oldjay
> >> >
> >> >
> >>
> >>
> >> .
> >>

>
>
> .
>

 
Reply With Quote
 
OssieMac
Guest
Posts: n/a
 
      25th Nov 2009
Hi oldjay,

If you use the original code I posted it should work under all situations.

--
Regards,

OssieMac


"oldjay" wrote:

> You guys have got me confused. What is the final code. I do not care about
> the date format. What ever is the easiest.
>
> oldjay
>
> "Peter T" wrote:
>
> > Hello OssieMac,
> >
> > No that's no problem, try this -
> >
> > MsgBox Replace("abc", "x", "y") ' abc
> >
> > Probably I'd use the Format function. However if I wanted to distribute I
> > wouldn't want to prejudge if user wants dd-mm-yyyy or mm-dd-yyyy. The
> > Replace function is a simple way to ensure no slashes appear without
> > changing the order, and an as an alternative to what you had already
> > suggested, I did indicate. Of course there are other ways too, not least
> > Format.
> >
> > Not sure if Date could ever be returned with some other invalid characters,
> > if so would need to think again.
> >
> > Regards,
> > Peter T
> >
> >
> > "OssieMac" <(E-Mail Removed)> wrote in message
> > news:4EF6C6A4-81B7-425D-8F2C-(E-Mail Removed)...
> > > Hi Peter,
> > >
> > > It is better to use code that will work for all users.
> > > ActiveSheet.Name = Replace(Date, "/", "-") will fail if the user does not
> > > have slashes in the date format.
> > >
> > > By using the Format function you can set the sheet name with characters
> > > that
> > > you know will not fail irrespective of the user's default date format.
> > >
> > > --
> > > Regards,
> > >
> > > OssieMac
> > >
> > >
> > > "Peter T" wrote:
> > >
> > >> As OssieMac points out best ensure there are no "/" in the new sheet name
> > >> change
> > >>
> > >> > ActiveSheet.Name = Date
> > >>
> > >> to (one way)
> > >> ActiveSheet.Name = Replace(Date, "/", "-")
> > >>
> > >> (my default format is has "-" separators hence I forgot)
> > >>
> > >> Regards,
> > >> Peter T
> > >>
> > >>
> > >> "Peter T" <peter_t@discussions> wrote in message
> > >> news:ONF%(E-Mail Removed)...
> > >> > Sub test()
> > >> > Dim ws As Worksheet
> > >> > Set ws = Worksheets("Sheet1")
> > >> > ws.Copy after:=ws
> > >> >
> > >> > ' will fail if a sheet named today's date already exits
> > >> > ActiveSheet.Name = Date
> > >> > End Sub
> > >> >
> > >> > This will copy the controls and code behind the sheet, as well as
> > >> > contents.
> > >> >
> > >> > Regards,
> > >> > Peter T
> > >> >
> > >> > "oldjay" <(E-Mail Removed)> wrote in message
> > >> > news:CE711085-5B76-4C3B-A028-(E-Mail Removed)...
> > >> >>I want to copy a sheet and its command buttons to a new sheet named for
> > >> >>the
> > >> >> present date i.e.(11/24/09). Buttons created from the Toolbox in Excel
> > >> >> 2003
> > >> >>
> > >> >> oldjay
> > >> >
> > >> >
> > >>
> > >>
> > >> .
> > >>

> >
> >
> > .
> >

 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

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


Similar Threads
Thread Thread Starter Forum Replies Last Post
Form buttons and Control Command Buttons ranswrt Microsoft Excel Programming 0 7th Aug 2008 11:36 PM
Copying buttons jim9912 Microsoft Excel Programming 2 16th May 2006 10:44 PM
copying macro buttons Pat Microsoft Excel Worksheet Functions 1 16th Sep 2004 04:04 PM
copying buttons from one tab to another =?Utf-8?B?Um9nZXI=?= Microsoft Access Forms 2 14th Jan 2004 02:06 AM
Control Buttons vs. Command Buttons Robert Gibson Microsoft Excel Programming 1 13th Oct 2003 04:33 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 11:47 AM.