Results 1 to 4 of 4

Thread: Creating macro to insert Excel sheets

  1. #1
    Join Date
    Aug 2009
    Posts
    76

    Creating macro to insert Excel sheets

    Even after a lot of research on the net, I can not find a solution to my problem. Here's what I do:

    - I have 1 excel file in which I have 2 sheets
    - on sheet 1, I have 3 columns: column name, a column surname and a column vehicle
    - in sheet 2, a number of boxes completed for each driver including the name, vehicle

    My idea is to create one sheet per driver, but I do not know how many sheets I will have to create because I added it gradually.

    I try to create a macro (I do not know much about macro!) to automate the following actions:
    -when I add a driver, the macro creates a new sheet (if possible with only the name of the driver).
    -This new sheet is in fact a copy of sheet 2 (information sheet)
    -To finish (yes it's not bad I know) on the new sheet that has been created, boxes full name and vehicle was already completed!

    I know it's not easy when you know nothing in macro, but I suppose that's possible with excel.

  2. #2
    Join Date
    May 2008
    Posts
    2,297

    Re: Creating macro to insert Excel sheets

    Here's what I suggest:
    Code:
    Private Sub Worksheet_Change(ByVal Target As Range)
    Dim MyRange As Range
    If Target.Column <> 4 Then Exit Sub
    
      If Target.Value <> "ok" Then Exit Sub
     
      Set MyRange = Range(Target.Offset(0, -3), Target.Offset(0, -1))
       
        TheName = Target.Offset(0, -3).Value
               Sheets(2).Copy after:=Sheets(Sheets.Count)
             
    ActiveSheet.Range("A2" ).Select
       ActiveSheet.Range(ActiveCell, ActiveCell.Offset(0, 2)).Value = MyRange .Value
    
    On  Error GoTo End
    
       ActiveSheet.Name = TheName 
    End: 
       End Sub
    Above all:
    select the macro by pressing Ctrl + C. This opens a copy of your workbook, do Alt + F11, click VBA project on sheet1. In the sheet that opens, do Ctrl + V. SAVE gives a name to your first 2 sheets in the macro. SAVE AGAIN.

    Here we consider that the 3 cell are A, B, C, and when they are fulfilled, it is "ok" D-collar following.

  3. #3
    Join Date
    Nov 2008
    Posts
    996

    Re: Creating macro to insert Excel sheets

    You can easily make a macro to add a new worksheet in your file. Simply do the following:
    Code:
    Public Sub AddWorksheet()
       ActiveWorkbook.Worksheets.Add(After:=Worksheets(Worksheets.Count))
    End Sub
    You can even add a worksheet based on the template that you have as below:

    Code:
    Public Sub AddWorksheet()
       ActiveWorkbook.Sheets.Add(After:=Worksheets(Worksheets.Count)), Type:="worksheet"
    End Sub

  4. #4
    Join Date
    Jan 2008
    Posts
    1,521

    Re: Creating macro to insert Excel sheets

    Code:
    Public Sub AddSheet()
        LastRow = Range("A65000").End(xlUp).Row
        For i = 1 To LastRow 
            myLastWorkSheet = Sheets.Count
            Sheets(myLastWorkSheet).Select
            Sheets(myLastWorkSheet).Copy After:=Sheets(myLastWorkSheet)
            Sheets(myLastWorkSheet + 1).Select = ""
        Next i
    End Sub

Similar Threads

  1. transfer data Between excel sheets
    By kvirmani in forum MS Office Support
    Replies: 1
    Last Post: 21-04-2011, 05:34 PM
  2. Select a range of sheets in Excel VBA
    By RyanInt in forum Software Development
    Replies: 6
    Last Post: 16-01-2010, 11:47 AM
  3. Transfer data between sheets in excel
    By Shanbaag in forum Windows Software
    Replies: 3
    Last Post: 16-10-2009, 10:40 AM
  4. Creating a macro for a backup button
    By Halina in forum Software Development
    Replies: 3
    Last Post: 13-05-2009, 09:38 AM
  5. Comparison of 2 sheets in MS Excel
    By Xeusion in forum Windows Software
    Replies: 3
    Last Post: 21-08-2008, 05:36 PM

Tags for this Thread

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
  •  
Page generated in 1,751,559,000.96569 seconds with 16 queries