Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
brianproctorla
 
Posts: n/a
Default Using VBA to create a new worksheet, and then target new worksheet


Hello,
I have read numerous threads about creating a new worksheet, and having
the user enter the new name for the worksheet. I would like to know how
to target this new worksheet.


Example.
I would like to run a MACRO that copies data from a set worksheet,
creates a new worksheet and prompts the user for a name. Then I would
like to target this worksheet and paste the data.

The result would be that everytime this macro is run, a new mutually
exclusive worksheet is created, and named by a user. This can be done
over and over again, until there are multiple worksheets
Thanks,
-Brian


--
brianproctorla
------------------------------------------------------------------------
brianproctorla's Profile: http://www.excelforum.com/member.php...o&userid=27567
View this thread: http://www.excelforum.com/showthread...hreadid=470896

  #2   Report Post  
Dave Peterson
 
Posts: n/a
Default

You can copy the worksheet or you can copy the cells on the worksheet and paste
to the new worksheet.

The first example inserts a new worksheet and copies data from a worksheet named
Master:

Option Explicit
Sub testme1()

Dim NewWks As Worksheet
Dim OldWks As Worksheet
Dim NewName As String

Set OldWks = Worksheets("Master")

NewName = Trim(InputBox(prompt:="what do you want to call it?"))

If NewName = "" Then
Exit Sub
End If

Set NewWks = Worksheets.Add
On Error Resume Next
NewWks.Name = NewName
If Err.Number < 0 Then
MsgBox "Something went wrong with the naming!" & vbLf & _
"Please change: " & NewWks.Name & " to what you want."
Err.Clear
End If

OldWks.Cells.Copy _
Destination:=NewWks.Range("a1")

End Sub

This second example copies the sheet and renames it:

Option Explicit
Sub testme2()

Dim NewWks As Worksheet
Dim OldWks As Worksheet
Dim NewName As String

NewName = Trim(InputBox(prompt:="what do you want to call it?"))

If NewName = "" Then
Exit Sub
End If

Set OldWks = Worksheets("Master")
OldWks.Copy after:=OldWks
Set NewWks = ActiveSheet

On Error Resume Next
NewWks.Name = NewName
If Err.Number < 0 Then
MsgBox "Something went wrong with the naming!" & vbLf & _
"Please change: " & NewWks.Name & " to what you want."
Err.Clear
End If

End Sub



brianproctorla wrote:

Hello,
I have read numerous threads about creating a new worksheet, and having
the user enter the new name for the worksheet. I would like to know how
to target this new worksheet.

Example.
I would like to run a MACRO that copies data from a set worksheet,
creates a new worksheet and prompts the user for a name. Then I would
like to target this worksheet and paste the data.

The result would be that everytime this macro is run, a new mutually
exclusive worksheet is created, and named by a user. This can be done
over and over again, until there are multiple worksheets
Thanks,
-Brian

--
brianproctorla
------------------------------------------------------------------------
brianproctorla's Profile: http://www.excelforum.com/member.php...o&userid=27567
View this thread: http://www.excelforum.com/showthread...hreadid=470896


--

Dave Peterson
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



All times are GMT +1. The time now is 01:50 PM.

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"