PC Review


Reply
Thread Tools Rate Thread

Create a new worksheet (excel 2003) if one or more cells in a range is not empty

 
 
Luc Ferrari
Guest
Posts: n/a
 
      20th Nov 2009
I want to create a new "tab, worksheet or whatever you call it " (in excel
2003) if one or more cells in a range contain a value.
The code starts after i hit a "button".

And name the sheet after the value of the cell left of the first cell of
that range.

I have no clue in how to program this.
Can someone provide me the code for this....


Thanks for your help

Luc

 
Reply With Quote
 
 
 
 
jason
Guest
Posts: n/a
 
      20th Nov 2009
On Nov 20, 2:17*pm, "Luc Ferrari" <lf...@versatel.be> wrote:
> I want to create a new "tab, worksheet or whatever you call it " (in excel
> 2003) if one or more cells in a range contain a value.
> The code starts after i hit a "button".
>
> And name the sheet after the value of the cell left of the first cell of
> that range.
>
> I have no clue in how to program this.
> Can someone provide me the code for this....
>
> Thanks for your help
>
> Luc


a finite range? ie: cells A1:A20 or an infinite range?

if finite:

sub A()
dim i
for i=1 to 20
if thisworkbook.activeshees.cells(i,1)<>"" then
thisworkbook.sheets.add.name= thisworkbook.sheets.cells(i,
1).value 'or thisworkbook.sheets.cells(i,1).text
end if
next i

this should work.
 
Reply With Quote
 
OssieMac
Guest
Posts: n/a
 
      20th Nov 2009
Hi Luc,

"if one or more cells in a range contain a value." What is this range and
what worksheet is it on? or is it simply a user selected range?

--
Regards,

OssieMac


"Luc Ferrari" wrote:

> I want to create a new "tab, worksheet or whatever you call it " (in excel
> 2003) if one or more cells in a range contain a value.
> The code starts after i hit a "button".
>
> And name the sheet after the value of the cell left of the first cell of
> that range.
>
> I have no clue in how to program this.
> Can someone provide me the code for this....
>
>
> Thanks for your help
>
> Luc
>
> .
>

 
Reply With Quote
 
Luc Ferrari
Guest
Posts: n/a
 
      20th Nov 2009
Hi Luc,

"if one or more cells in a range contain a value." What is this range and
what worksheet is it on? or is it simply a user selected range?

--
Regards,

OssieMac


The range is part of a row , example : B5:V5
The range is always on the same sheet


Regards,
Luc
 
Reply With Quote
 
Luc Ferrari
Guest
Posts: n/a
 
      20th Nov 2009
if finite:

sub A()
dim i
for i=1 to 20
if thisworkbook.activeshees.cells(i,1)<>"" then
thisworkbook.sheets.add.name= thisworkbook.sheets.cells(i,
1).value 'or thisworkbook.sheets.cells(i,1).text
end if
next i


Thanks for the reply !
Does this create a worksheet for every cell in the range that contains a
value ? (example : 5 cells have a value => 5 sheets added)
I would achieve the following : Example : if 8 cells in the range contain a
value, then only 1 sheet should be added (never more than 1 sheet should be
added)

Thanxxxx

Luc

 
Reply With Quote
 
OssieMac
Guest
Posts: n/a
 
      21st Nov 2009
Hi again Luc,

To add the button in Excel 2007: (see below for other versions)
If Developer Ribbon not displayed then see help under Developer ribbon to
turn it on.
You might also have to see how to set macro security to allow the macro to
run and when you save the workbook, save as a macro enabled workbook (.xlsm)
Developer ribbon -> Insert and Select button from ActiveX controls and then
click on worksheet and hold left mouse button and drag button out to required
size.

To add the button in earlier versions of Excel:
On the Controls Toolbox Toolbar (Not the forms toolbar) Select command
button and then click on worksheet and hold left mouse button and drag button
out to required size.

Note while doing the above the Ribbon or Toolbar Design mode button (Like a
set square, ruler and pencil) is activated (orange color). Leave it like that
for time being.

Right click the new button and select View code.

Copy the below code (between asterisk lines) and paste between the sub and
end sub in the VBA editor.

Click the X with red background top right to close the VBA editor.

Click the Design Mode button to turn it off.

You can now click the button to run your code.

If you want to make changes to the button design then click the Design mode
button and then right click the button and select properties.

Set the Caption property to what you want to display on the button.

To open the VBA editor again use Alt/F11 and then in the project explorer on
the left select the worksheet that has the button (If correct worksheet code
is not already displayed.)

Note that lines in green after copying into the VBA editor are comments
only. Also a space and underscore at the end of a line is a line break in an
otherwise single line of code.

'*************************************
'Uses a specified range to test
'In this example, the worksheet
'is named by value in cell A5
Dim rngToTest As Range
Dim strNewShtName As String
Dim wsNewSht As Worksheet

'Edit "Sheet1" to your sheet name
With Sheets("Sheet1")
'Edit "B5:V5" to your range
Set rngToTest = .Range("B5:V5")
End With

'Assign proposed new worksheet name to variable
strNewShtName = rngToTest _
.Cells(1, 1).Offset(0, -1).Value

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

'If no error then the attempt to assign to variable
'was successful so worksheet already exists
If Err.Number = 0 Then 'No error
MsgBox "Worksheet " & strNewShtName _
& " already exists"
Exit Sub
End If

On Error GoTo 0 'Resume error trapping ASAP

'If get to here worksheet does not exist
'so create and name
If WorksheetFunction.CountA(rngToTest) > 0 Then
Sheets.Add After:=Sheets(Sheets.Count)
ActiveSheet.Name = _
rngToTest.Cells(1, 1).Offset(0, -1).Value
End If

'Return to original worksheet.
Sheets("Sheet1").Select
Range("A1").Select

'*****************************************

Note that example will fail if the range starts in column A because there is
no cell to the left of the range.

--
Regards,

OssieMac


 
Reply With Quote
 
Luc
Guest
Posts: n/a
 
      22nd Nov 2009
Thanks for your help :=)
 
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
Re: Create a new worksheet (excel 2003) if one or more cells in arange is not empty jason Microsoft Excel Programming 0 20th Nov 2009 08:00 PM
Excel 2003 attachments open and show only empty cells when openedvia Outlook 2003? Rob Gordon Microsoft Outlook Discussion 1 2nd Dec 2008 09:34 PM
Excel 2003 attachments open and show only empty cells when openedvia Outlook 2003? Rob Gordon Microsoft Excel Misc 1 2nd Dec 2008 09:34 PM
Sort Worksheet Range from another worksheet range, Excel 2000 & 2003 jfcby Microsoft Excel Programming 1 21st Aug 2007 02:55 AM
total a range of cells in Excel into a different worksheet =?Utf-8?B?Sm9pbnQgQ291bmNpbA==?= Microsoft Excel Worksheet Functions 2 21st Nov 2005 04:15 PM


Features
 

Advertising
 

Newsgroups
 


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