+ Reply to Thread
Results 1 to 3 of 3

create new workbook, copy data, rename sheet.

Hybrid View

simeonmein create new workbook, copy... 10-09-2013, 02:52 PM
patel45 Re: create new workbook, copy... 10-10-2013, 01:52 AM
HaHoBe Re: create new workbook, copy... 10-10-2013, 02:00 AM
  1. #1
    Registered User
    Join Date
    09-11-2013
    Location
    zastron,south africa
    MS-Off Ver
    Excel 2010
    Posts
    48

    create new workbook, copy data, rename sheet.

    Hi all
    My aim is to click a commandbutton in a userform with 4 textboxes.
    Then a new workbook must be created, the data must be pasted and the sheet must be named after textbox2. After this the sheet must contain the values of the userform textboxes values, then it must save the workbook and name it from textbox1 or activesheet range for textbox1.

    This is my current code:

    Private Sub CommandButton1_Click()
    
        Dim ws As Worksheet, i As Long, lRow As Long, FName As String, FPath As String, NewBook As Workbook
        FPath = "c:\pos"
        FName = "sup" & TextBox1.Value
            Set ws = Sheets("supdata")
            Set NewBook = Workbooks.add
            ThisWorkbook.Sheets("supdata").Copy before:=NewBook.Sheets(1)
        If Dir(FPath & "\" & FName) <> "" Then
            MsgBox "file " & FPath & "\" & FName & " exists"
    
        End If
         
                
          '____error handling______
    
    
    If TextBox1.Value = "" Or nullstring Then
    MsgBox "Complete All Information!", vbCritical, "Error..."
    GoTo error1
    End If
          
          
          
          '____error handling end______
       
        If MsgBox("Add " & TextBox1.Value & " to the database?", vbYesNo, "Confirm add") = vbYes Then
       
          NewBook.Cells(lRow, "A") = TextBox1.Value
          NewBook.Cells(lRow, "B") = TextBox2.Value
          NewBook.Cells(lRow, "C") = TextBox3.Value
          NewBook.Cells(lRow, "D") = TextBox4.Value
          
        
        MsgBox TextBox1.Value & " has been added "
        
        TextBox1.Value = nullstring
        TextBox2.Value = nullstring
        TextBox3.Value = nullstring
        TextBox4.Value = nullstring
        
        NEWACC.Hide
        
            
        Else
        End If
        
    
        
        ADDSTOCK.Show vbModeless
        
    error1:
    End Sub
    Any help will be appriciated, thanx
    Last edited by arlu1201; 10-10-2013 at 01:01 AM. Reason: Corrected code tags.
    Creating POS

  2. #2
    Forum Expert
    Join Date
    07-15-2012
    Location
    Leghorn, Italy
    MS-Off Ver
    Excel 2010
    Posts
    3,431

    Re: create new workbook, copy data, rename sheet.

    Set ws = Sheets("supdata")
    Set NewBook = Workbooks.add
    ThisWorkbook.Sheets("supdata").Copy before:=NewBook.Sheets(1)
    you created a new Workbook, now ThisWorkbook is NewBook
    the correct code is
    Set ws = ThisWorkbook.Sheets("supdata")
    Set NewBook = Workbooks.add
    ws.Copy before:=NewBook.Sheets(1)
    If solved remember to mark Thread as solved

  3. #3
    Forum Guru HaHoBe's Avatar
    Join Date
    02-19-2005
    Location
    Hamburg, Germany
    MS-Off Ver
    work: 2016 on Win10 (notebook), private: 365 on Win11 (desktop), 2019 on Win11 (notebook)
    Posts
    8,198

    Re: create new workbook, copy data, rename sheet.

    Hi, patel45,

    you´re sure about
    you created a new Workbook, now ThisWorkbook is NewBook
    ThisWorkbook can only be the workbook with code while ActiveWorkbook can be any active workbook (either ThisWorkbook when the code starts or any other workbook).

    @simeonmein
    If TextBox1.Value = "" Or nullstring Then
    MsgBox "Complete All Information!", vbCritical, "Error..."
    GoTo error1
    End If
    I´d guessed to read it like
    If TextBox1.Value = "" Then
      MsgBox "Complete All Information!", vbCritical, "Error..."
      GoTo error1
    End If
    vbNullString and "" should be very very similar to each other but if you want to compare values from a textbox you would need to mention this in both parts of the comparision (you can´t check one textbox for two conditions unless you use Select Case).

    Ciao,
    Holger
    Use Code-Tags for showing your code: [code] Your Code here [/code]
    Please mark your question Solved if there has been offered a solution that works fine for you

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. [SOLVED] A button to create Sheet2, copy there data from Sheet1 and rename the sheet?
    By mintymike in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 12-17-2012, 10:34 AM
  2. Replies: 2
    Last Post: 09-04-2012, 01:19 AM
  3. [SOLVED] Code to create new sheet and rename, regardless of any other sheets in workbook
    By csh8428 in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 07-19-2012, 02:22 PM
  4. Replies: 0
    Last Post: 04-28-2012, 06:06 PM
  5. Create copy of sheet & rename
    By Vetequk in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 11-20-2010, 08:40 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1