Photo Gallery Member List Search Calendars FAQ Ticket List Log Out


Adding a sheet to an existing workbook

 
Logged in as: Guest
arrSession:exec spGetSession 2,2,42106
 Active Users: There are 0 members and 0 guests.
 Users viewing this topic: none
 

 

 
  
  Printable Version
All Forums >> [Scripting] >> WSH & Client Side VBScript >> Adding a sheet to an existing workbook
  Do you like VisualBasicScript.com? Link to us and help spread the word about our forum. Thanks!
Page: [1]
Login
Message << Older Topic   Newer Topic >>
 Adding a sheet to an existing workbook - 1/16/2007 7:56:14 PM   
  mike21ap

 

Posts: 12
Score: 0
Joined: 11/14/2006
Status: offline
Original message moved by TNO
Reason : Incorrect Forum
OK.. so I have a Excel spreadsheet which has a VB UserForm with a couple of buttons... when a button is presses it triggers a VBScript... but in a new Excel window... (not what I want)

Script starts with :

Private Sub CommandButton1_Click()
Const ADS_SCOPE_SUBTREE = 2
Set objExcel = CreateObject("Excel.Application")
objExcel.Visible = True
objExcel.Workbooks.Add

.........................................................

How can I modify this so instead of opening a new Excel windows, it will just add a new sheet to my existing Excel workbook from which I invoke this script?

Thanks a lot

Mike
 
 
Post #: 1
 
 RE: Adding a sheet to an existing workbook - 1/16/2007 11:26:44 PM   
  gdewrance


Posts: 587
Score: 3
Joined: 3/16/2006
Status: offline
did you search google for VBA add new excel worksheet
then You should end up with somthing like this

You could also try using a Macro, then just copy the code

Private Sub CommandButton1_Click()
Sheets.Add
End Sub


You can also ask it for a name for your new sheet

Private Sub CommandButton1_Click()
  'Remember where we started
'Not needed if you don't want to return to where you started but want
'to stay on the New Sheet
  Dim CurrentSheetName As String
  CurrentSheetName = ActiveSheet.Name

'Add New Sheet
  Sheets.Add

'Make sure the name is valid
  On Error Resume Next

'Get the new name
   ActiveSheet.Name = InputBox("Name for new worksheet?")

'Keep asking for name if name is invalid
  Do Until Err.Number = 0
      Err.Clear
      ActiveSheet.Name = InputBox("Try Again!" _
        & vbCrLf & "Invalid Name or Name Already Exists" _
        & vbCrLf & "Please name the New Sheet")
  Loop
  On Error GoTo 0

'Go back to where you started
'Not needed if you don't want to return to where you started but want
'to stay on the New Sheet
  Sheets(CurrentSheetName).Select

End Sub

< Message edited by gdewrance -- 1/17/2007 1:34:52 AM >

(in reply to mike21ap)
 
 
Post #: 2
 
 
 
  

If you found our site useful please link to us <a href="http://www.visualbasicscript.com">VisualBasicScript.com</a>.
All Forums >> [Scripting] >> WSH & Client Side VBScript >> Adding a sheet to an existing workbook Page: [1]
Jump to:





New Messages No New Messages
Hot Topic w/ New Messages Hot Topic w/o New Messages
Locked w/ New Messages Locked w/o New Messages
 Post New Thread
 Reply to Message
 Post New Poll
 Submit Vote
 Delete My Own Post
 Delete My Own Thread
 Rate Posts