Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 557
Default SAME NAME FOR ALL SHEETS

Hi all, I have button on sheet which have macro allocated (see below)

Sub addsht ()
Sheets.Add After:=Sheets(Sheets.Count)
With ActiveSheet
..Name = "EXTRACTED DATA"
End With
End Sub

The problem is that when I press this button once it works fine but
when I press it again it gives error on
..Name = "EXTRACTED DATA"
what code line I can add so that when I press the button it give
sheets name like "EXTRACTED DATA (1) , EXTRACTED DATA (2) and so on.
in other words keeping the same name but adding 1 , 2 , 3..... in the
end. Please can any friend can help
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 4,393
Default SAME NAME FOR ALL SHEETS

This will work for a new workbook
Dim j As Integer
Sub addsht()
Sheets.Add After:=Sheets(Sheets.Count)
j = j + 1
ActiveSheet.Name = "EXTRACTED DATA (" & j & ")"
End Sub

By defining J outside the sub, its value is preserved each time the sub is
run
However, J goes back to 1 when the file is opened tomorrow

best wishes
--
Bernard V Liengme
Microsoft Excel MVP
http://people.stfx.ca/bliengme
remove caps from email

"K" wrote in message
...
Hi all, I have button on sheet which have macro allocated (see below)

Sub addsht ()
Sheets.Add After:=Sheets(Sheets.Count)
With ActiveSheet
.Name = "EXTRACTED DATA"
End With
End Sub

The problem is that when I press this button once it works fine but
when I press it again it gives error on
.Name = "EXTRACTED DATA"
what code line I can add so that when I press the button it give
sheets name like "EXTRACTED DATA (1) , EXTRACTED DATA (2) and so on.
in other words keeping the same name but adding 1 , 2 , 3..... in the
end. Please can any friend can help



  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5,600
Default SAME NAME FOR ALL SHEETS

Another one -

Sub test2()
Dim i As Long
Dim s As String
Dim ws As Worksheet

On Error Resume Next
Err.Clear
i = 0
While Err.Number = 0
i = i + 1
s = "EXTRACTED DATA (" & i & ")"
Set ws = ActiveWorkbook.Worksheets(s)
Wend
On Error GoTo 0

With ActiveWorkbook
Set ws = .Worksheets.Add(after:=.Sheets(.Sheets.Count))
ws.Name = s
End With

End Sub


If you have, say, sheets 1, 2 and 4, the above will insert sheet "3". That
may or may not be a useful "feature" if "3" had been deleted.

Regards,
Peter T


"K" wrote in message
...
Hi all, I have button on sheet which have macro allocated (see below)

Sub addsht ()
Sheets.Add After:=Sheets(Sheets.Count)
With ActiveSheet
.Name = "EXTRACTED DATA"
End With
End Sub

The problem is that when I press this button once it works fine but
when I press it again it gives error on
.Name = "EXTRACTED DATA"
what code line I can add so that when I press the button it give
sheets name like "EXTRACTED DATA (1) , EXTRACTED DATA (2) and so on.
in other words keeping the same name but adding 1 , 2 , 3..... in the
end. Please can any friend can help



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
Needle in a haystack...searching for VBA code in all Excel sheets orfinding xls users of xla sheets.... CodeMonkey[_2_] Excel Programming 1 May 28th 08 06:10 PM
Print sheets by "All Sheets in workbook, EXCEPT for specific named sheets". Possible? Corey Excel Programming 2 December 11th 06 01:35 AM
How to repeat a code for selected sheets (or a contiguous range of sheets) in a Workbook? Dmitry Excel Programming 6 March 29th 06 12:43 PM
Macro for filter on protected workbook that works for all sheets, no matter what sheets are named? StargateFanFromWork[_3_] Excel Programming 6 January 26th 06 06:31 PM
Allocate Files to Sheets and Build a Master Sheet which Summarises All Sheets hanuman Excel Programming 0 September 9th 03 11:23 AM


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

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"