PC Review


Reply
Thread Tools Rate Thread

Code to create worksheet names based on the values in the selectedrange

 
 
Mike C
Guest
Posts: n/a
 
      17th Mar 2008

Is there any chance someone would know some code that allows me to
create new worksheets with the names of a selection of cells.

So, for example, if I selected the following cells:

Rice
Macaroni
Chicken
Hash Browns

I would like the macro to create four sheets by those names.

Any suggestions would be appreciated.
 
Reply With Quote
 
 
 
 
Chip Pearson
Guest
Posts: n/a
 
      17th Mar 2008
Try some code like the following. The code tests whether a sheet already
exists with the particular name, and won't create a new sheet if that name
is already in use, but it doesn't test whether the name is valid for a new
worksheet.

Sub CreateSheetsFromList()
Dim R As Range
For Each R In Selection.Cells
If R.Text <> vbNullString Then
If SheetExists(R.Text, ThisWorkbook) = False Then
With ThisWorkbook.Worksheets
.Add(after:=.Item(.Count)).Name = R.Text
End With
End If
End If
Next R
End Sub

Private Function SheetExists(SHName As String, WB As Workbook) As Boolean
On Error Resume Next
SheetExists = CBool(Len(WB.Worksheets(SHName).Name))
End Function


--
Cordially,
Chip Pearson
Microsoft Most Valuable Professional
Excel Product Group
Pearson Software Consulting, LLC
www.cpearson.com
(email on web site)




"Mike C" <(E-Mail Removed)> wrote in message
news:cb77e9ac-407a-4c2c-b8ed-(E-Mail Removed)...
>
> Is there any chance someone would know some code that allows me to
> create new worksheets with the names of a selection of cells.
>
> So, for example, if I selected the following cells:
>
> Rice
> Macaroni
> Chicken
> Hash Browns
>
> I would like the macro to create four sheets by those names.
>
> Any suggestions would be appreciated.


 
Reply With Quote
 
Mark Ivey
Guest
Posts: n/a
 
      17th Mar 2008
Here is a rough one...

Mark

Sub test()
Dim row As Long
Dim mysheetname As String
Dim myworksheet As String

myworksheet = ActiveSheet.Name

For row = 1 To 4
mysheetname = Cells(row, 1).Text
Sheets.Add
ActiveSheet.Name = mysheetname
Sheets(myworksheet).Select
Next
End Sub





"Mike C" <(E-Mail Removed)> wrote in message
news:cb77e9ac-407a-4c2c-b8ed-(E-Mail Removed)...
>
> Is there any chance someone would know some code that allows me to
> create new worksheets with the names of a selection of cells.
>
> So, for example, if I selected the following cells:
>
> Rice
> Macaroni
> Chicken
> Hash Browns
>
> I would like the macro to create four sheets by those names.
>
> Any suggestions would be appreciated.


 
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
using the Excel generic worksheet names instead of user-given names in code Paul Microsoft Excel Misc 5 26th Jun 2009 08:44 PM
I want to format a worksheet based on values in another worksheet Ag Microsoft Excel Programming 2 29th Sep 2007 11:43 PM
Code to create a new worksheet (based on an original pre-format sheet) Mikey C Microsoft Excel Discussion 4 30th Mar 2007 10:25 AM
Create Field Names based on values in another field Dane Cooper Microsoft Access Queries 7 25th Jan 2006 07:54 PM
Count unique values and create list based on these values =?Utf-8?B?dmlwYTIwMDA=?= Microsoft Excel Worksheet Functions 7 5th Aug 2005 01:17 AM


Features
 

Advertising
 

Newsgroups
 


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