Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 21
Default Counting worksheets with a particular name

I have a macro that generates worksheets. Each new worksheet must of course
have an individual unique name. however every new worksheet that i generate
have the same beginning of name i.e. Report. The sub that generates a new
worksheet is run one time every time the program runs. After that the program
ends. The program may then be ran again and new worksheets are generated.
Therefore using static is not possible. Now I count the worksheets in the
worksbook and assign the worksheet with a constant name (Report) and a
varaible e.g. Report 1, Report 2 etc. The problem is that counting worksheets
is not good because I have number of worksheets that are not reports and so
the first report gets a higher number than 1. The number of worksheets that
are not Reports may vary. Is there a way of counting worksheets that have the
same name beginning (i.e. counting all worksheets that have name starting
with Report) or is there any other way of solving the problem? Please help me!

The code is:
iCtr = WB.Worksheets.Count
Set sh = Worksheets.Add(after:=Worksheets(iCtr))
sh.Name = sName & iCtr
Call workSheetSettings(sh.Name)
x = sh.Name
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 4,391
Default Counting worksheets with a particular name

Dim WS As Worksheet
Dim ReportCount As Long
For each WS In ThisWorkbook.Worksheets
If Instr(WS.Name, "Report) 0 Then
ReportCount=ReportCount+1
End If
Next

NickHK

"Freddie Mac" wrote in message
...
I have a macro that generates worksheets. Each new worksheet must of

course
have an individual unique name. however every new worksheet that i

generate
have the same beginning of name i.e. Report. The sub that generates a new
worksheet is run one time every time the program runs. After that the

program
ends. The program may then be ran again and new worksheets are generated.
Therefore using static is not possible. Now I count the worksheets in the
worksbook and assign the worksheet with a constant name (Report) and a
varaible e.g. Report 1, Report 2 etc. The problem is that counting

worksheets
is not good because I have number of worksheets that are not reports and

so
the first report gets a higher number than 1. The number of worksheets

that
are not Reports may vary. Is there a way of counting worksheets that have

the
same name beginning (i.e. counting all worksheets that have name starting
with Report) or is there any other way of solving the problem? Please help

me!

The code is:
iCtr = WB.Worksheets.Count
Set sh = Worksheets.Add(after:=Worksheets(iCtr))
sh.Name = sName & iCtr
Call workSheetSettings(sh.Name)
x = sh.Name



  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default Counting worksheets with a particular name


Dim iReports as Integer

iReports = 0

For i = 1 to WB.Worksheets.Count

If Instr(WB.Worksheets(i).name, "Report") Then iReports = iReport
+ 1

Next

--
Kaa
-----------------------------------------------------------------------
Kaak's Profile: http://www.excelforum.com/member.php...nfo&userid=751
View this thread: http://www.excelforum.com/showthread.php?threadid=55726

  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5,302
Default Counting worksheets with a particular name

Hi Freddie,

Try:

'=============
Public Sub Tester()
Dim WB As Workbook
Dim SH As Worksheet
Dim i As Long
Dim iCtr As Long
Const sName As String = "Report"

For i = 1 To ThisWorkbook.Worksheets.Count
If Worksheets(i).Name Like sName & "*" Then iCtr = iCtr + 1
Next i

Set SH = Worksheets.Add(after:=Worksheets(sName & iCtr + 1))
SH.Name = sName & iCtr

End Sub
'<<=============


--
---
Regards,
Norman



"Freddie Mac" wrote in message
...
I have a macro that generates worksheets. Each new worksheet must of course
have an individual unique name. however every new worksheet that i
generate
have the same beginning of name i.e. Report. The sub that generates a new
worksheet is run one time every time the program runs. After that the
program
ends. The program may then be ran again and new worksheets are generated.
Therefore using static is not possible. Now I count the worksheets in the
worksbook and assign the worksheet with a constant name (Report) and a
varaible e.g. Report 1, Report 2 etc. The problem is that counting
worksheets
is not good because I have number of worksheets that are not reports and
so
the first report gets a higher number than 1. The number of worksheets
that
are not Reports may vary. Is there a way of counting worksheets that have
the
same name beginning (i.e. counting all worksheets that have name starting
with Report) or is there any other way of solving the problem? Please help
me!

The code is:
iCtr = WB.Worksheets.Count
Set sh = Worksheets.Add(after:=Worksheets(iCtr))
sh.Name = sName & iCtr
Call workSheetSettings(sh.Name)
x = sh.Name



  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 108
Default Counting worksheets with a particular name


Freddie Mac wrote:
Is there a way of counting worksheets that have the
same name beginning (i.e. counting all worksheets that have name starting
with Report) or is there any other way of solving the problem? Please help me!


Hi Freddie

You could use a For... Next Loop and include

If Left(sh.Name,6) = "Report" Then
shtcount = shtcount + 1

Report shtcount at the end of your routine

Regards

Steve



  #6   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 21
Default Counting worksheets with a particular name

Great that seems like a very fine code! However when I try to use it the
program says "Error! Next without For" and it highlights the sub where i try
to run the code.
I copied your code but I submit it anyhow in case there is something missing.

Public Sub mainProgram()
Call worksheetMaker
....................
End Sub

Private Sub worksheetMaker()
Dim WB As Workbook
Dim SH As Worksheet
Dim i As Long
Dim iCtr As Long
Const sName As String = "Report "

For i = 1 To ThisWorkbook.Worksheets.Count
If Worksheets(i).Name Like sName & "*" Then
iCtr = iCtr + 1
Next i

Set SH = Worksheets.Add(after:=Worksheets(sName & iCtr + 1))
SH.Name = sName & iCtr
End Sub

If you have any idea how to solve this please post an answer! Thank you!

"Norman Jones" skrev:

Hi Freddie,

Try:

'=============
Public Sub Tester()
Dim WB As Workbook
Dim SH As Worksheet
Dim i As Long
Dim iCtr As Long
Const sName As String = "Report"

For i = 1 To ThisWorkbook.Worksheets.Count
If Worksheets(i).Name Like sName & "*" Then iCtr = iCtr + 1
Next i

Set SH = Worksheets.Add(after:=Worksheets(sName & iCtr + 1))
SH.Name = sName & iCtr

End Sub
'<<=============


--
---
Regards,
Norman



"Freddie Mac" wrote in message
...
I have a macro that generates worksheets. Each new worksheet must of course
have an individual unique name. however every new worksheet that i
generate
have the same beginning of name i.e. Report. The sub that generates a new
worksheet is run one time every time the program runs. After that the
program
ends. The program may then be ran again and new worksheets are generated.
Therefore using static is not possible. Now I count the worksheets in the
worksbook and assign the worksheet with a constant name (Report) and a
varaible e.g. Report 1, Report 2 etc. The problem is that counting
worksheets
is not good because I have number of worksheets that are not reports and
so
the first report gets a higher number than 1. The number of worksheets
that
are not Reports may vary. Is there a way of counting worksheets that have
the
same name beginning (i.e. counting all worksheets that have name starting
with Report) or is there any other way of solving the problem? Please help
me!

The code is:
iCtr = WB.Worksheets.Count
Set sh = Worksheets.Add(after:=Worksheets(iCtr))
sh.Name = sName & iCtr
Call workSheetSettings(sh.Name)
x = sh.Name




  #7   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5,302
Default Counting worksheets with a particular name

Hi Freddie,

Try:

Public Sub mainProgram()
Call worksheetMaker

End Sub

Private Sub worksheetMaker()
Dim WB As Workbook
Dim SH As Worksheet
Dim i As Long
Dim iCtr As Long
Const sName As String = "Report"

For i = 1 To ThisWorkbook.Worksheets.Count
If Worksheets(i).Name Like sName & "*" Then
iCtr = iCtr + 1
End If
Next i

Set SH = Worksheets.Add(after:=Worksheets(sName & iCtr))
SH.Name = sName & iCtr + 1
End Sub


---
Regards,
Norman



"Freddie Mac" wrote in message
...
Great that seems like a very fine code! However when I try to use it the
program says "Error! Next without For" and it highlights the sub where i
try
to run the code.
I copied your code but I submit it anyhow in case there is something
missing.

Public Sub mainProgram()
Call worksheetMaker
...................
End Sub

Private Sub worksheetMaker()
Dim WB As Workbook
Dim SH As Worksheet
Dim i As Long
Dim iCtr As Long
Const sName As String = "Report "

For i = 1 To ThisWorkbook.Worksheets.Count
If Worksheets(i).Name Like sName & "*" Then
iCtr = iCtr + 1
Next i

Set SH = Worksheets.Add(after:=Worksheets(sName & iCtr + 1))
SH.Name = sName & iCtr
End Sub

If you have any idea how to solve this please post an answer! Thank you!

"Norman Jones" skrev:

Hi Freddie,

Try:

'=============
Public Sub Tester()
Dim WB As Workbook
Dim SH As Worksheet
Dim i As Long
Dim iCtr As Long
Const sName As String = "Report"

For i = 1 To ThisWorkbook.Worksheets.Count
If Worksheets(i).Name Like sName & "*" Then iCtr = iCtr + 1
Next i

Set SH = Worksheets.Add(after:=Worksheets(sName & iCtr + 1))
SH.Name = sName & iCtr

End Sub
'<<=============


--
---
Regards,
Norman



"Freddie Mac" wrote in message
...
I have a macro that generates worksheets. Each new worksheet must of
course
have an individual unique name. however every new worksheet that i
generate
have the same beginning of name i.e. Report. The sub that generates a
new
worksheet is run one time every time the program runs. After that the
program
ends. The program may then be ran again and new worksheets are
generated.
Therefore using static is not possible. Now I count the worksheets in
the
worksbook and assign the worksheet with a constant name (Report) and a
varaible e.g. Report 1, Report 2 etc. The problem is that counting
worksheets
is not good because I have number of worksheets that are not reports
and
so
the first report gets a higher number than 1. The number of worksheets
that
are not Reports may vary. Is there a way of counting worksheets that
have
the
same name beginning (i.e. counting all worksheets that have name
starting
with Report) or is there any other way of solving the problem? Please
help
me!

The code is:
iCtr = WB.Worksheets.Count
Set sh = Worksheets.Add(after:=Worksheets(iCtr))
sh.Name = sName & iCtr
Call workSheetSettings(sh.Name)
x = sh.Name






  #8   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 21
Default Counting worksheets with a particular name

Yes Thank you it works better.....However I have a problem with this code, it
does not work for the first report i.e. when there is no sheet named Report
1. How do you solve that? Please help me on this one!

"Norman Jones" skrev:

Hi Freddie,

Try:

Public Sub mainProgram()
Call worksheetMaker

End Sub

Private Sub worksheetMaker()
Dim WB As Workbook
Dim SH As Worksheet
Dim i As Long
Dim iCtr As Long
Const sName As String = "Report"

For i = 1 To ThisWorkbook.Worksheets.Count
If Worksheets(i).Name Like sName & "*" Then
iCtr = iCtr + 1
End If
Next i

Set SH = Worksheets.Add(after:=Worksheets(sName & iCtr))
SH.Name = sName & iCtr + 1
End Sub


---
Regards,
Norman



"Freddie Mac" wrote in message
...
Great that seems like a very fine code! However when I try to use it the
program says "Error! Next without For" and it highlights the sub where i
try
to run the code.
I copied your code but I submit it anyhow in case there is something
missing.

Public Sub mainProgram()
Call worksheetMaker
...................
End Sub

Private Sub worksheetMaker()
Dim WB As Workbook
Dim SH As Worksheet
Dim i As Long
Dim iCtr As Long
Const sName As String = "Report "

For i = 1 To ThisWorkbook.Worksheets.Count
If Worksheets(i).Name Like sName & "*" Then
iCtr = iCtr + 1
Next i

Set SH = Worksheets.Add(after:=Worksheets(sName & iCtr + 1))
SH.Name = sName & iCtr
End Sub

If you have any idea how to solve this please post an answer! Thank you!

"Norman Jones" skrev:

Hi Freddie,

Try:

'=============
Public Sub Tester()
Dim WB As Workbook
Dim SH As Worksheet
Dim i As Long
Dim iCtr As Long
Const sName As String = "Report"

For i = 1 To ThisWorkbook.Worksheets.Count
If Worksheets(i).Name Like sName & "*" Then iCtr = iCtr + 1
Next i

Set SH = Worksheets.Add(after:=Worksheets(sName & iCtr + 1))
SH.Name = sName & iCtr

End Sub
'<<=============


--
---
Regards,
Norman



"Freddie Mac" wrote in message
...
I have a macro that generates worksheets. Each new worksheet must of
course
have an individual unique name. however every new worksheet that i
generate
have the same beginning of name i.e. Report. The sub that generates a
new
worksheet is run one time every time the program runs. After that the
program
ends. The program may then be ran again and new worksheets are
generated.
Therefore using static is not possible. Now I count the worksheets in
the
worksbook and assign the worksheet with a constant name (Report) and a
varaible e.g. Report 1, Report 2 etc. The problem is that counting
worksheets
is not good because I have number of worksheets that are not reports
and
so
the first report gets a higher number than 1. The number of worksheets
that
are not Reports may vary. Is there a way of counting worksheets that
have
the
same name beginning (i.e. counting all worksheets that have name
starting
with Report) or is there any other way of solving the problem? Please
help
me!

The code is:
iCtr = WB.Worksheets.Count
Set sh = Worksheets.Add(after:=Worksheets(iCtr))
sh.Name = sName & iCtr
Call workSheetSettings(sh.Name)
x = sh.Name






  #9   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5,302
Default Counting worksheets with a particular name

Hi Freddie,

In tthat case try instead:

'=============
Private Sub worksheetMaker()
Dim WB As Workbook
Dim SH As Worksheet
Dim i As Long
Dim iCtr As Long
Const sName As String = "Report"

For i = 1 To ThisWorkbook.Worksheets.Count
If Worksheets(i).Name Like sName & "*" Then
iCtr = iCtr + 1
End If
Next i

If iCtr = 0 Then iCtr = 1
Set SH = Worksheets.Add(after:=Worksheets(Worksheets.Count) )
SH.Name = sName & iCtr + 1
End Sub
'<<=============


---
Regards,
Norman


"Freddie Mac" wrote in message
...
Yes Thank you it works better.....However I have a problem with this code,
it
does not work for the first report i.e. when there is no sheet named
Report
1. How do you solve that? Please help me on this one!

"Norman Jones" skrev:

Hi Freddie,

Try:

Public Sub mainProgram()
Call worksheetMaker

End Sub

Private Sub worksheetMaker()
Dim WB As Workbook
Dim SH As Worksheet
Dim i As Long
Dim iCtr As Long
Const sName As String = "Report"

For i = 1 To ThisWorkbook.Worksheets.Count
If Worksheets(i).Name Like sName & "*" Then
iCtr = iCtr + 1
End If
Next i

Set SH = Worksheets.Add(after:=Worksheets(sName & iCtr))
SH.Name = sName & iCtr + 1
End Sub


---
Regards,
Norman



"Freddie Mac" wrote in message
...
Great that seems like a very fine code! However when I try to use it
the
program says "Error! Next without For" and it highlights the sub where
i
try
to run the code.
I copied your code but I submit it anyhow in case there is something
missing.

Public Sub mainProgram()
Call worksheetMaker
...................
End Sub

Private Sub worksheetMaker()
Dim WB As Workbook
Dim SH As Worksheet
Dim i As Long
Dim iCtr As Long
Const sName As String = "Report "

For i = 1 To ThisWorkbook.Worksheets.Count
If Worksheets(i).Name Like sName & "*" Then
iCtr = iCtr + 1
Next i

Set SH = Worksheets.Add(after:=Worksheets(sName & iCtr + 1))
SH.Name = sName & iCtr
End Sub

If you have any idea how to solve this please post an answer! Thank
you!

"Norman Jones" skrev:

Hi Freddie,

Try:

'=============
Public Sub Tester()
Dim WB As Workbook
Dim SH As Worksheet
Dim i As Long
Dim iCtr As Long
Const sName As String = "Report"

For i = 1 To ThisWorkbook.Worksheets.Count
If Worksheets(i).Name Like sName & "*" Then iCtr = iCtr + 1
Next i

Set SH = Worksheets.Add(after:=Worksheets(sName & iCtr + 1))
SH.Name = sName & iCtr

End Sub
'<<=============


--
---
Regards,
Norman



"Freddie Mac" wrote in message
...
I have a macro that generates worksheets. Each new worksheet must of
course
have an individual unique name. however every new worksheet that i
generate
have the same beginning of name i.e. Report. The sub that generates
a
new
worksheet is run one time every time the program runs. After that
the
program
ends. The program may then be ran again and new worksheets are
generated.
Therefore using static is not possible. Now I count the worksheets
in
the
worksbook and assign the worksheet with a constant name (Report) and
a
varaible e.g. Report 1, Report 2 etc. The problem is that counting
worksheets
is not good because I have number of worksheets that are not reports
and
so
the first report gets a higher number than 1. The number of
worksheets
that
are not Reports may vary. Is there a way of counting worksheets that
have
the
same name beginning (i.e. counting all worksheets that have name
starting
with Report) or is there any other way of solving the problem?
Please
help
me!

The code is:
iCtr = WB.Worksheets.Count
Set sh = Worksheets.Add(after:=Worksheets(iCtr))
sh.Name = sName & iCtr
Call workSheetSettings(sh.Name)
x = sh.Name








  #10   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5,302
Default Counting worksheets with a particular name

Hi Freddie,

Repalce the suggested code with the following version:

'=============
Private Sub worksheetMaker()
Dim WB As Workbook
Dim SH As Worksheet
Dim i As Long
Dim iCtr As Long
Const sName As String = "Report"

For i = 1 To ThisWorkbook.Worksheets.Count
If Worksheets(i).Name Like sName & "*" Then
iCtr = iCtr + 1
End If
Next i
'
'If iCtr = 0 Then iCtr = 1
Set SH = Worksheets.Add(after:=Worksheets(Worksheets.Count) )
SH.Name = sName & iCtr + 1
End Sub
'<<=============

--
---
Regards,
Norman



"Norman Jones" wrote in message
...
Hi Freddie,

In tthat case try instead:

'=============
Private Sub worksheetMaker()
Dim WB As Workbook
Dim SH As Worksheet
Dim i As Long
Dim iCtr As Long
Const sName As String = "Report"

For i = 1 To ThisWorkbook.Worksheets.Count
If Worksheets(i).Name Like sName & "*" Then
iCtr = iCtr + 1
End If
Next i

If iCtr = 0 Then iCtr = 1
Set SH = Worksheets.Add(after:=Worksheets(Worksheets.Count) )
SH.Name = sName & iCtr + 1
End Sub
'<<=============


---
Regards,
Norman


"Freddie Mac" wrote in message
...
Yes Thank you it works better.....However I have a problem with this
code, it
does not work for the first report i.e. when there is no sheet named
Report
1. How do you solve that? Please help me on this one!

"Norman Jones" skrev:

Hi Freddie,

Try:

Public Sub mainProgram()
Call worksheetMaker

End Sub

Private Sub worksheetMaker()
Dim WB As Workbook
Dim SH As Worksheet
Dim i As Long
Dim iCtr As Long
Const sName As String = "Report"

For i = 1 To ThisWorkbook.Worksheets.Count
If Worksheets(i).Name Like sName & "*" Then
iCtr = iCtr + 1
End If
Next i

Set SH = Worksheets.Add(after:=Worksheets(sName & iCtr))
SH.Name = sName & iCtr + 1
End Sub


---
Regards,
Norman



"Freddie Mac" wrote in message
...
Great that seems like a very fine code! However when I try to use it
the
program says "Error! Next without For" and it highlights the sub where
i
try
to run the code.
I copied your code but I submit it anyhow in case there is something
missing.

Public Sub mainProgram()
Call worksheetMaker
...................
End Sub

Private Sub worksheetMaker()
Dim WB As Workbook
Dim SH As Worksheet
Dim i As Long
Dim iCtr As Long
Const sName As String = "Report "

For i = 1 To ThisWorkbook.Worksheets.Count
If Worksheets(i).Name Like sName & "*" Then
iCtr = iCtr + 1
Next i

Set SH = Worksheets.Add(after:=Worksheets(sName & iCtr + 1))
SH.Name = sName & iCtr
End Sub

If you have any idea how to solve this please post an answer! Thank
you!

"Norman Jones" skrev:

Hi Freddie,

Try:

'=============
Public Sub Tester()
Dim WB As Workbook
Dim SH As Worksheet
Dim i As Long
Dim iCtr As Long
Const sName As String = "Report"

For i = 1 To ThisWorkbook.Worksheets.Count
If Worksheets(i).Name Like sName & "*" Then iCtr = iCtr + 1
Next i

Set SH = Worksheets.Add(after:=Worksheets(sName & iCtr + 1))
SH.Name = sName & iCtr

End Sub
'<<=============


--
---
Regards,
Norman



"Freddie Mac" wrote in message
...
I have a macro that generates worksheets. Each new worksheet must of
course
have an individual unique name. however every new worksheet that i
generate
have the same beginning of name i.e. Report. The sub that generates
a
new
worksheet is run one time every time the program runs. After that
the
program
ends. The program may then be ran again and new worksheets are
generated.
Therefore using static is not possible. Now I count the worksheets
in
the
worksbook and assign the worksheet with a constant name (Report)
and a
varaible e.g. Report 1, Report 2 etc. The problem is that counting
worksheets
is not good because I have number of worksheets that are not
reports
and
so
the first report gets a higher number than 1. The number of
worksheets
that
are not Reports may vary. Is there a way of counting worksheets
that
have
the
same name beginning (i.e. counting all worksheets that have name
starting
with Report) or is there any other way of solving the problem?
Please
help
me!

The code is:
iCtr = WB.Worksheets.Count
Set sh = Worksheets.Add(after:=Worksheets(iCtr))
sh.Name = sName & iCtr
Call workSheetSettings(sh.Name)
x = sh.Name










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
Function for counting Worksheets? JonWardellBuilders Excel Worksheet Functions 2 November 10th 08 06:44 PM
counting data across worksheets franki_85[_2_] Excel Worksheet Functions 4 November 21st 07 04:21 PM
counting data across worksheets franki_85[_2_] Excel Discussion (Misc queries) 5 November 21st 07 02:19 PM
Counting Worksheets, Second Verse Janie Excel Worksheet Functions 0 October 22nd 06 07:46 PM
Help with counting across worksheets Biff Excel Discussion (Misc queries) 4 April 12th 05 04:16 AM


All times are GMT +1. The time now is 08:15 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"