Consulting

Results 1 to 7 of 7

Thread: Urgent: VBA code to automatically generate new worksheet

  1. #1

    Urgent: VBA code to automatically generate new worksheet

    Hello, guys. I am a newcomer to VBA. I would like to have some help from you guys. Please help!

    I need a set of VBA code that generate worksheet according to a template and fill in a cell in each new worksheet according the cell values in a list.

    In an excel workbook, I have a well developed-template to download stock data using Reuters add-on. In cell A1, it is the ticker of the stock.

    I hope to have the following function. I hope to select some cells which contained the tickers. Then I run the macro. It generates the spreadsheets and rename them to the tickers. It fills in cell A1 with the tickers. That's it. The sequence of the worksheets should follow the sequence in the master list.

    Thanks a lot!

  2. #2
    I forgot to mention. The selection in the master list is vertical and continuous. For example, if I select A1 to A10. Then the macro copies some templates, named "template" for simplicity. It changes the names of the new spreadsheets to the cell values one by one. And also changes the cell A1 accordingly.

  3. #3
    Moderator VBAX Sage SamT's Avatar
    Joined
    Oct 2006
    Location
    Near Columbia
    Posts
    7,814
    Location
    Sub SamT()
    'For help see: http://www.vbaexpress.com/forum/show...-new-worksheet
    
    Dim Cel As Range
    Dim NewSheet As Worksheet
    
    'Application.ScreenUpdating = False 'Uncomment after testing
    For Each Cel In Selection
      Sheets("Template").Copy After:=ActiveSheet
      Set NewSheet = Sheets(Sheets.Count)
      With NewSheet
        .Name = Cel
        .Range("A1") = Cel
        .Activate
      End With
    Next Cel
    Cel.Parent.Activate
    Application.ScreenUpdating = True
    End Sub
    I expect the student to do their homework and find all the errrors I leeve in.


    Please take the time to read the Forum FAQ

  4. #4
    Excel displayed:

    Run-time error '9': Subscript out of range

    How do I fix it? Also, sequence of the spreadsheets is important. Thank you

  5. #5
    Moderator VBAX Wizard Aussiebear's Avatar
    Joined
    Dec 2005
    Location
    Queensland
    Posts
    5,060
    Location
    Check the worksheet name is correct
    Remember To Do the Following....
    Use [Code].... [/Code] tags when posting code to the thread.
    Mark your thread as Solved if satisfied by using the Thread Tools options.
    If posting the same issue to another forum please show the link

  6. #6
    Thanks for helping. But the code did not work as expected.

    Excel displayed Error #9 still. I named the template "Template". I guess I am right on that.

    Suppose I put the code and the selected cells in "Worksheet 1".
    A1=1
    A2=2
    A3=3
    I select only these 3 cells.

    After the code is run, there are two sheets called "Template (2)" and "1". Inside worksheet "1", cell A1 is 1. Nothing else.

    The result I hope to have: After the code is run, 3 worksheets created and named 1,2 and 3 respectively. Inside the sheets, A1= 1 or 2 or 3, respectively.

    Btw, the sequence of the worksheets is all-important. It should follow the 1-2-3 sequence. In this case, is the counter function better than the "for each cell" function?

    Here is an example of the result that I hope to have.

    Thanks a lot. I know I may be a bit demanding.
    Attached Files Attached Files

  7. #7
    Moderator VBAX Sage SamT's Avatar
    Joined
    Oct 2006
    Location
    Near Columbia
    Posts
    7,814
    Location
    I found out that Sheets.Count always returns to right most tab. This means that its Index number is the same as its positional number By using the Index number of the sheet to copy after, the loop is just as simple.

    Option Explicit
    
    Sub SamT()
         'For help see: http://www.vbaexpress.com/forum/show...-new-worksheet
         
        Dim Cel As Range
        Dim i As Long
        
        i = Sheets(ActiveSheet.Name).Index
         'Application.ScreenUpdating = False 'Uncomment after testing
        For Each Cel In Selection
            Sheets("Template").Copy After:=Sheets(i)
             i = i + 1 'Index number of new sheet
            With Sheets(i)
                .Name = Cel.Text
                .Range("A1") = Cel.Value
                .Activate
            End With
        Next Cel
        Application.ScreenUpdating = True
    End Sub
    I expect the student to do their homework and find all the errrors I leeve in.


    Please take the time to read the Forum FAQ

Posting Permissions

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