Results 1 to 6 of 6

Thread: Worksheet Duplication Code Help

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Member
    Join Date
    Jun 2012
    Posts
    80
    Rep Power
    13

    Worksheet Duplication Code Help

    Hi Guys,

    I have an excel with a macro, which duplicates an invoice template on the first sheet to new worksheets. Also, these newly generated invoices are numbered automatically in a sequence. I have a new file which requires the same procedure. But the cell, range etc are different. Could some one help in re-coding the below code to sui my needs. The invoice number in the old file was 13-01-1000 (YY-MM-1000). So when a new invoice was generated the sequence would be 13-01-1000, 13-01-1001, and so on.

    The new invoicing sequence will be "TWS/0902/20161000", where TWS/0902/2016 will be constant and the sequential numbers will be form 1000, 1001, 1002 etc. TWS/0902/20161000, TWS/0902/20161001, etc will be the sequence.

    The previous cell range for the invoice number was 'J6' (as mentioned in the below code). New invoice template has the invoice number in cell 'J13'

    Code:
    Sub Create_New_Invoice()
      
      Dim InvNo As String
      
      Application.ScreenUpdating = False
      EEvents = True
      With wksFormat
        With .Range("J6")
          InvNo = .Value
          InvNo = Left(InvNo, 6) & Format(Right(InvNo, 4) + 1, "0000")
          .Value = InvNo
        End With
        .Range("J7").Value = Date
        .Copy After:=Worksheets(Sheets.Count)
        On Error Resume Next
        ActiveSheet.Name = InvNo
        ActiveSheet.Buttons("cmdAdd").Delete
        On Error GoTo 0
      End With
      Application.ScreenUpdating = True
      EEvents = False
    
    End Sub

    Also, with the generation of a new invoice on a new worksheet, the invoice number automatically becomes the name of the worksheet based on the below code.
    Can the same be done for the new invoicing criteria in cell 'J13' as mentioned above. Wherein, the name of the worksheet changes to 1000, 1001, etc instead of the whole TWS/0902/20161000.

    Code:
    Sub NameSheets()
    Dim ws As Worksheet
    For Each ws In ThisWorkbook.Sheets
    If ws.Range("J6") <> "" Then
    ws.Name = ws.Range("J6").Value
    End If
    Next ws
    End Sub

    Thanks

  2. #2
    Administrator Admin's Avatar
    Join Date
    Mar 2011
    Posts
    1,123
    Rep Power
    10
    Hi

    I hope this will do

    Code:
    Sub Create_New_Invoice()
      
        Dim InvNo As String
        
        Application.ScreenUpdating = False
        EEvents = True
      
        Const ConstInvNo    As String = "TWS/0902/2016"
      
        With wksFormat
            With .Range("J13")
                InvNo = .Value
                InvNo = ConstInvNo & Format(Right(InvNo, 4) + 1, "0000")
                .Value = InvNo
            End With
            .Range("J7").Value = Date
            .Copy After:=Worksheets(Sheets.Count)
            On Error Resume Next
            ActiveSheet.Name = Right(InvNo, 4)
            ActiveSheet.Buttons("cmdAdd").Delete
            On Error GoTo 0
        End With
        Application.ScreenUpdating = True
        EEvents = False
    
    End Sub
    Cheers !

    Excel Range to BBCode Table
    Use Social Networking Tools If You Like the Answers !

    Message to Cross Posters

    @ Home - Office 2010/2013/2016 on Win 10 (64 bit); @ Work - Office 2016 on Win 10 (64 bit)

  3. #3
    Member
    Join Date
    Jun 2012
    Posts
    80
    Rep Power
    13
    The above code doesn't seem to work for me. I get the following error:

    Run-time error '424': Object Required. Once I click debug, With
    Code:
    .Range("K13")
    shows highlighted in yellow. (I changed the invoice no. cell to K13 from J13.

    Please help.

  4. #4
    Administrator Admin's Avatar
    Join Date
    Mar 2011
    Posts
    1,123
    Rep Power
    10
    You have to declare the variable wksFormat
    Cheers !

    Excel Range to BBCode Table
    Use Social Networking Tools If You Like the Answers !

    Message to Cross Posters

    @ Home - Office 2010/2013/2016 on Win 10 (64 bit); @ Work - Office 2016 on Win 10 (64 bit)

  5. #5
    Member
    Join Date
    Jun 2012
    Posts
    80
    Rep Power
    13
    Quote Originally Posted by Admin View Post
    You have to declare the variable wksFormat
    Could you please explain this with details, as I am not familiar with codes.

  6. #6
    Administrator Admin's Avatar
    Join Date
    Mar 2011
    Posts
    1,123
    Rep Power
    10
    Declare this variable

    Code:
    set wksFormat = Worksheets("YourSheetName")
    just above the
    Code:
    With wksFormat
    line.

    obviously replace the "yoursheetname" with actual sheet name
    Cheers !

    Excel Range to BBCode Table
    Use Social Networking Tools If You Like the Answers !

    Message to Cross Posters

    @ Home - Office 2010/2013/2016 on Win 10 (64 bit); @ Work - Office 2016 on Win 10 (64 bit)

Similar Threads

  1. Replies: 8
    Last Post: 01-07-2014, 06:06 PM
  2. Replies: 3
    Last Post: 10-03-2013, 07:33 PM
  3. Replies: 2
    Last Post: 08-23-2013, 01:11 PM
  4. Replies: 7
    Last Post: 05-17-2013, 10:38 PM
  5. Print Nth Worksheet To Mth Worksheet using VBA
    By Ryan_Bernal in forum Excel Help
    Replies: 2
    Last Post: 02-28-2013, 06:57 PM

Posting Permissions

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