Split data across multiple sheets in excel (vba)
In this post I am going to show how to create a new sheet for each airplane using vba. The macro copies airplane and model values into each new sheet.
Before:
After:
The Code
Sub Splitdatatosheets()
Dim rng As Range
Dim rng1 As Range
Dim vrb As Boolean
Dim sht As Worksheet
Set rng = Sheets("Sheet1").Range("A4")
Set rng1 = Sheets("Sheet1").Range("A4:D4")
vrb = False
Do While rng <> ""
For Each sht In Worksheets
If sht.Name = Left(rng.Value, 31) Then
sht.Select
Range("A2").Select
Do While Selection <> ""
ActiveCell.Offset(1, 0).Activate
Loop
rng1.Copy ActiveCell
ActiveCell.Offset(1, 0).Activate
Set rng1 = rng1.Offset(1, 0)
Set rng = rng.Offset(1, 0)
vrb = True
End If
Next sht
If vrb = False Then
Sheets.Add After:=Sheets(Sheets.Count)
ActiveSheet.Name = Left(rng.Value, 31)
Sheets("Sheet1").Range("A3:B3").Copy ActiveSheet.Range("A1")
Range("A2").Select
Do While Selection <> ""
ActiveCell.Offset(1, 0).Activate
Loop
rng1.Copy ActiveCell
Set rng1 = rng1.Offset(1, 0)
Set rng = rng.Offset(1, 0)
End If
vrb = False
Loop
End SubDownload excel tutorial file
Remember to enable macros and backup your excel file because you can´t undo macros.
Split-data-across-multiple-sheets.xls
(Excel 97-2003 Workbook *.xls)








August 24th, 2010 at 8:23 pm
how would we print the sorted sheet into a new workbook rather than the same?
August 25th, 2010 at 2:21 pm
how do we set a range on the column. Say you only wanted to split the information between a4 and a10 rather than between a4 and the very end of the list.
September 1st, 2010 at 12:25 pm
Great - worked a charm.
August 8th, 2011 at 1:45 pm
“Split data across multiple sheets in excel (vba)”...
this code works well, but the prob is could not able to get the Merge cell as it is..
Thnx
August 9th, 2011 at 7:58 pm
Mashini,
I don´t have an answer right now.
I found this:
Excel often has problems copying ranges containing merged cells onto sheets with merged cells even if the merged areas are the same. The best solution is to not have merged cells. Next best is to unmerge all cells in the range that is to receive the copy before pasting in the new data.
from here
August 23rd, 2011 at 10:42 am
Hej.. works...problem is for a huge file.. doesn't. I have a 470k rows and is blocking at some point.
August 24th, 2011 at 1:01 pm
Alin,
Where does it halt and what does the row look like? You can use this contact form.
August 26th, 2011 at 4:25 pm
Awesome....
August 28th, 2011 at 5:15 pm
Thanks!
December 5th, 2011 at 8:23 am
thnak you very much, its really helpful for me
December 22nd, 2011 at 11:08 am
It is really a nice article. I have used it in one of my automation but it goes into loop and never ends. I have to abruptly log off from the system to stop it. Need urgent help on it.
January 2nd, 2012 at 1:24 pm
sujay,
can you provide an example of your sheet?
January 3rd, 2012 at 8:47 am
Hi,
I am new to Excel with VBA code.. I have 1 excel with 2 sheets ( A.xl contains 2 tabs Name and Rating ).
I want to read the datas from 2 sheets (Name and Rating) and appened the data in other excel file..
Can any one please help me to solve this...
January 17th, 2012 at 4:12 pm
Hi there!
This code is working perfectly for me besides the fact that the macro stops splitting my table into different sheets once it reaches a field name longer that 31 characters. Is there any way I can work around this?
January 19th, 2012 at 5:41 am
Nice work...
January 20th, 2012 at 7:02 pm
Jojo,
Yes, I have uploaded a new file and changed the post.
Thanks for commenting!
Peter,
Thanks!
January 22nd, 2012 at 3:24 am
Lovely! Thanks so much Oscar.