Monday, 25 May 2015

VBA to create multiple sheets based on the cells value

Create Multiple Sheets based on the cell value through VBA

Sub Addsheet()
'Declaring the Variables
Dim i As Integer
Dim LastRow As Integer

'Finding Last Row in Sheet1
LastRow = Worksheets("Sheet1").Cells(Worksheets("Sheet1").Rows.Count, "A").End(xlUp).Row

'Using For Loop to Add Sheets by Cell Value
For i = 1 To LastRow
Sheets.Add
ActiveSheet.Name = Worksheets("Sheet1").Cells(i, 1).Value
Next i

End Sub

Lets Discuss the above code:
  1. We have defined two variable "i" and "LastRow".
  2. LastRow find the the row in "Sheet1" from Column "A". Because the list of sheets names are stored in "A" column in "Sheet1".
  3. We have used for loop to create multiple sheets untill all the sheets are created.
You can watch below video in action.



2 comments:

  1. I noticed when I run this in larger files the order of sheets is re-arranged.

    EG if my current work book has sheets "ABC" , "DEF" , "GHI". and I run this VBA output is

    "ABC" ,"NEWsheet2", "DEF" ,"NEWsheet1", "GHI". or some random order.

    I would like it to output

    "ABC" , "DEF" , "GHI" ,"NEWsheet1" ,"NEWsheet2" .... and so on.

    How can that be possible?

    ReplyDelete
  2. Hi,
    Thak you for this code!
    The code is working and I need in every new sheet to be the data fro the hole row and when we add new data in sell to create only the new sheet.
    Can you help me please
    10x

    ReplyDelete