Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.worksheet.functions
external usenet poster
 
Posts: 49
Default Add sheets using macro

Hi,

I want to add sheet and call it a name. But when I record it a problem
occurs. Everytime I add a sheet it is given a new name i.e. sheet 2,
sheet 3, sheet 4 etc. This happens before I get a chance to rename
which means the macro always fails.

Any thoughts?

I want to add a new sheet to at lot of workbooks - the sheet is called
"Raabalance"

/Heine

  #2   Report Post  
Posted to microsoft.public.excel.worksheet.functions
external usenet poster
 
Posts: 11
Default Add sheets using macro


Try an inputbox (or replace the inputboxline with a fixed name)

Sub SheetInsert()

Dim strNameSheet As String

Sheets.Add
strNameSheet = InputBox("give sheet name")
ActiveSheet.Name = strNameSheet

End Sub

  #3   Report Post  
Posted to microsoft.public.excel.worksheet.functions
external usenet poster
 
Posts: 10,593
Default Add sheets using macro

worksheets.Add.name="Bob"

--
HTH

Bob Phillips

(replace somewhere in email address with gmail if mailing direct)

"Heine" wrote in message
ups.com...
Hi,

I want to add sheet and call it a name. But when I record it a problem
occurs. Everytime I add a sheet it is given a new name i.e. sheet 2,
sheet 3, sheet 4 etc. This happens before I get a chance to rename
which means the macro always fails.

Any thoughts?

I want to add a new sheet to at lot of workbooks - the sheet is called
"Raabalance"

/Heine



  #4   Report Post  
Posted to microsoft.public.excel.worksheet.functions
external usenet poster
 
Posts: 430
Default Add sheets using macro

I thought to add a worksheet one used the workbooks.add method.
Confused..
Jim

"Bob Phillips" wrote in message
:

worksheets.Add.name="Bob"

--
HTH

Bob Phillips

(replace somewhere in email address with gmail if mailing direct)

"Heine" wrote in message
ups.com...
Hi,

I want to add sheet and call it a name. But when I record it a problem
occurs. Everytime I add a sheet it is given a new name i.e. sheet 2,
sheet 3, sheet 4 etc. This happens before I get a chance to rename
which means the macro always fails.

Any thoughts?

I want to add a new sheet to at lot of workbooks - the sheet is called
"Raabalance"

/Heine


  #5   Report Post  
Posted to microsoft.public.excel.worksheet.functions
external usenet poster
 
Posts: 49
Default Add sheets using macro

Thanks - that works great.

Problem is now that if I run the macro more than once an error occurs
because I already added the sheet once. Is there an easy way to get
around that small problem?

/Heine
Gert wrote:
Try an inputbox (or replace the inputboxline with a fixed name)

Sub SheetInsert()

Dim strNameSheet As String

Sheets.Add
strNameSheet = InputBox("give sheet name")
ActiveSheet.Name = strNameSheet

End Sub




  #6   Report Post  
Posted to microsoft.public.excel.worksheet.functions
external usenet poster
 
Posts: 477
Default Add sheets using macro

Never mind; just having a "senior-moment".. daaaa

"Jim May" wrote:

I thought to add a worksheet one used the workbooks.add method.
Confused..
Jim

"Bob Phillips" wrote in message
:

worksheets.Add.name="Bob"

--
HTH

Bob Phillips

(replace somewhere in email address with gmail if mailing direct)

"Heine" wrote in message
ups.com...
Hi,

I want to add sheet and call it a name. But when I record it a problem
occurs. Everytime I add a sheet it is given a new name i.e. sheet 2,
sheet 3, sheet 4 etc. This happens before I get a chance to rename
which means the macro always fails.

Any thoughts?

I want to add a new sheet to at lot of workbooks - the sheet is called
"Raabalance"

/Heine



  #7   Report Post  
Posted to microsoft.public.excel.worksheet.functions
external usenet poster
 
Posts: 11
Default Add sheets using macro


the extended version

Sub SheetInsert()


Dim strNameSheet As String
Dim boolFound As Boolean
Dim MySheets As Worksheet

Sheets.Add
boolFound = False
strNameSheet = InputBox("give sheet name")
For Each MySheets In Worksheets
If MySheets.Name = strNameSheet Then boolFound = True
Next
If boolFound Then
MsgBox ("this sheet already exists")
Else
ActiveSheet.Name = strNameSheet
End If

End Sub

  #8   Report Post  
Posted to microsoft.public.excel.worksheet.functions
external usenet poster
 
Posts: 49
Default Add sheets using macro

Thanks Gert,

that´s quite crafty. One or two minor details, though:

I would like, if possible to avoid the use of an inputbox.
The macro still adds sheets called sheet 5, sheet 6, sheet 7 etc - can
I avoid this problem?

/Heine
Gert wrote:
the extended version

Sub SheetInsert()


Dim strNameSheet As String
Dim boolFound As Boolean
Dim MySheets As Worksheet

Sheets.Add
boolFound = False
strNameSheet = InputBox("give sheet name")
For Each MySheets In Worksheets
If MySheets.Name = strNameSheet Then boolFound = True
Next
If boolFound Then
MsgBox ("this sheet already exists")
Else
ActiveSheet.Name = strNameSheet
End If

End Sub


  #9   Report Post  
Posted to microsoft.public.excel.worksheet.functions
external usenet poster
 
Posts: 11
Default Add sheets using macro

Heine,

try this one:

Sub SheetInsert()

Dim strNameSheet As String
Dim boolFound As Boolean
Dim MySheets As Worksheet

Sheets.Add
boolFound = False
strNameSheet = "Raabalance"
For Each MySheets In Worksheets
If MySheets.Name = strNameSheet Then boolFound = True
Next
If boolFound Then
MsgBox ("this sheet already exists")
Else
ActiveSheet.Name = strNameSheet
End If

End Sub

best regards
Gert

  #10   Report Post  
Posted to microsoft.public.excel.worksheet.functions
external usenet poster
 
Posts: 49
Default Add sheets using macro

Thanks Gert - that is better.

Only one problem left as I see it. If I keep running the macro it keeps
adding new sheets called sheet 6,7,8 etc. I would like those sheet to
be deleted or not to be added in the first place. Any ideas?


best regards

Heine
Gert wrote:
Heine,

try this one:

Sub SheetInsert()

Dim strNameSheet As String
Dim boolFound As Boolean
Dim MySheets As Worksheet

Sheets.Add
boolFound = False
strNameSheet = "Raabalance"
For Each MySheets In Worksheets
If MySheets.Name = strNameSheet Then boolFound = True
Next
If boolFound Then
MsgBox ("this sheet already exists")
Else
ActiveSheet.Name = strNameSheet
End If

End Sub

best regards
Gert




  #11   Report Post  
Posted to microsoft.public.excel.worksheet.functions
external usenet poster
 
Posts: 11
Default Add sheets using macro

Sorry Heine, it was my faulth,
it depends on the moment when you add the new sheet.
I've replaced the line Sheets.Add and now it only adds a sheet when
"Raabalance" isn't in use allready

Sub SheetInsert()

Dim strNameSheet As String
Dim boolFound As Boolean
Dim MySheets As Worksheet

boolFound = False
strNameSheet = "Raabalance"
For Each MySheets In Worksheets
If MySheets.Name = strNameSheet Then boolFound = True
Next
If boolFound Then
MsgBox ("this sheet already exists")
Else
Sheets.Add
ActiveSheet.Name = strNameSheet
End If
End Sub


hopes this works as you wanna have it
best regards
Gert

  #12   Report Post  
Posted to microsoft.public.excel.worksheet.functions
external usenet poster
 
Posts: 49
Default Add sheets using macro

That works like a charm, Gert. Thanks so much for taking your time to
help. Wish I were better at writing these codes:-)


Best Regards

Heine
Gert wrote:
Sorry Heine, it was my faulth,
it depends on the moment when you add the new sheet.
I've replaced the line Sheets.Add and now it only adds a sheet when
"Raabalance" isn't in use allready

Sub SheetInsert()

Dim strNameSheet As String
Dim boolFound As Boolean
Dim MySheets As Worksheet

boolFound = False
strNameSheet = "Raabalance"
For Each MySheets In Worksheets
If MySheets.Name = strNameSheet Then boolFound = True
Next
If boolFound Then
MsgBox ("this sheet already exists")
Else
Sheets.Add
ActiveSheet.Name = strNameSheet
End If
End Sub


hopes this works as you wanna have it
best regards
Gert


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
error when running cut & paste macro Otto Moehrbach Excel Worksheet Functions 4 August 9th 06 01:49 PM
Microsoft Visual Basic: Compile error: Sum or Function not defined Dmitry Excel Worksheet Functions 12 April 3rd 06 07:28 AM
Macro that stores all sheets as tab-delimited text files [email protected] Excel Discussion (Misc queries) 2 February 14th 06 04:02 PM
Closing File Error jcliquidtension Excel Discussion (Misc queries) 4 October 20th 05 12:22 PM
Macro to link Sheets to main workbook raven_guy Excel Discussion (Misc queries) 0 June 24th 05 12:36 PM


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