Create new sheets from data?

TheWennerWoman

Active Member
Joined
Aug 1, 2019
Messages
270
Office Version
  1. 365
Platform
  1. Windows
Book1.xlsm
ABCDE
1code1code2vxsourceflag
2C5678103000100ZV0001
3X0000114544428ZV0001
4X0000117300020ZV0001
5X0000119900019ZV0001
6X0000121200020ZV0001
7X0000125566613ZV0001
8C567810300090ZV0002
9X0000214544417ZV0002
10X0000217300019ZV0002
11X0000219900027ZV0002
12X0000221200016ZV0002
13X0000225566611ZV0002end
14C567810300080ZV0003
15X0000314544415ZV0003
16X0000317300010ZV0003
17X0000319900032ZV0003
18X0000321200014ZV0003
19X000032556669ZV0003
20C567810300070ZV0004
21X0000414544413ZV0004
22X0000417300027ZV0004
23X0000419900010ZV0004
24X0000421200012ZV0004
25X000042556668ZV0004end
26C567810300060ZV0005
27X0000514544411ZV0005
28X0000517300021ZV0005
29X0000519900010ZV0005
30X0000521200010ZV0005
31X000052556668ZV0005
Sheet2


This is just a small sample to illustrate what I am trying to achieve.

I have thousands of rows of data on one sheet. I need to split this data out based on whether the word "end" appears in column E. So in the above, A2:A13 would be copied to a new worksheet.

Then A14:A25 would be copied to a new sheet.

And so on. So the flow would be (i) identify first range to be copied, (ii) add a new worksheet, (iii) paste the data.

Any pointers appreciated.
 

Excel Facts

Back into an answer in Excel
Use Data, What-If Analysis, Goal Seek to find the correct input cell value to reach a desired result
I have assumed that the last row of your actual data will also have an "end" in column E.
If that is not the case then please advise what should happen to the last block of data that has no "end"

Try this with a copy of your workbook.

VBA Code:
Sub New_Sheets()
  Dim fr As Long, i As Long
  Dim rend As Range
  
  Application.ScreenUpdating = False
  With Sheets("Sheet2")
    fr = 2
    Set rend = .Columns("E").Find(What:="end")
    Do
      i = i + 1
      Sheets.Add(After:=Sheets(Sheets.Count)).Name = "New " & i
      .Rows(fr & ":" & rend.Row).Copy Destination:=Sheets(Sheets.Count).Range("A1")
      fr = rend.Row + 1
      Set rend = .Columns("E").Find(What:="end", After:=rend)
    Loop Until rend.Row < fr
  End With
  Application.ScreenUpdating = True
End Sub
 
Upvote 1
I have assumed that the last row of your actual data will also have an "end" in column E.
If that is not the case then please advise what should happen to the last block of data that has no "end"

Try this with a copy of your workbook.

VBA Code:
Sub New_Sheets()
  Dim fr As Long, i As Long
  Dim rend As Range
 
  Application.ScreenUpdating = False
  With Sheets("Sheet2")
    fr = 2
    Set rend = .Columns("E").Find(What:="end")
    Do
      i = i + 1
      Sheets.Add(After:=Sheets(Sheets.Count)).Name = "New " & i
      .Rows(fr & ":" & rend.Row).Copy Destination:=Sheets(Sheets.Count).Range("A1")
      fr = rend.Row + 1
      Set rend = .Columns("E").Find(What:="end", After:=rend)
    Loop Until rend.Row < fr
  End With
  Application.ScreenUpdating = True
End Sub
That works perfectly.

Assume that the last row of data has no "end", what amendments to this code are needed to capture the last block of data (from the last "end" to the last row)?
 
Upvote 0

Peter_SSs

I have assumed that the last row of your actual data will also have an "end" in column E.
If that is not the case then please advise what should happen to the last block of data that has no "end"
 
Upvote 0
@Tom.Jones
Was there supposed to be a suggestion or question included in your post?


Assume that the last row of data has no "end", what amendments to this code are needed to capture the last block of data (from the last "end" to the last row)?

This should work whether or not the last row has an "end"

VBA Code:
Sub New_Sheets_v2()
  Dim fr As Long, i As Long, lrD As Long
  Dim rend As Range
 
  Application.ScreenUpdating = False
  lrD = Range("D" & Rows.Count).End(xlUp).Row
  With Sheets("Sheet2")
    fr = 2
    Set rend = .Columns("E").Find(What:="end")
    Do
      i = i + 1
      Sheets.Add(After:=Sheets(Sheets.Count)).Name = "New " & i
      .Rows(fr & ":" & rend.Row).Copy Destination:=Sheets(Sheets.Count).Range("A1")
      If rend.Row = lrD Then Exit Do
      fr = rend.Row + 1
      Set rend = .Columns("E").Find(What:="end", After:=rend)
      If rend.Row < fr Then Set rend = Range("E" & lrD)
    Loop Until rend.Row < fr
  End With
  Application.ScreenUpdating = True
End Sub
 
Upvote 1
Solution
This should work whether or not the last row has an "end"

VBA Code:
Sub New_Sheets_v2()
  Dim fr As Long, i As Long, lrD As Long
  Dim rend As Range
 
  Application.ScreenUpdating = False
  lrD = Range("D" & Rows.Count).End(xlUp).Row
  With Sheets("Sheet2")
    fr = 2
    Set rend = .Columns("E").Find(What:="end")
    Do
      i = i + 1
      Sheets.Add(After:=Sheets(Sheets.Count)).Name = "New " & i
      .Rows(fr & ":" & rend.Row).Copy Destination:=Sheets(Sheets.Count).Range("A1")
      If rend.Row = lrD Then Exit Do
      fr = rend.Row + 1
      Set rend = .Columns("E").Find(What:="end", After:=rend)
      If rend.Row < fr Then Set rend = Range("E" & lrD)
    Loop Until rend.Row < fr
  End With
  Application.ScreenUpdating = True
End Sub
That's very impressive and works perfectly - thank you very much :)
 
Upvote 0
You're welcome. Thanks for the follow-up. :)
 
Upvote 0

Forum statistics

Threads
1,215,219
Messages
6,123,680
Members
449,116
Latest member
HypnoFant

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top