Help with copied data

TheWennerWoman

Active Member
Joined
Aug 1, 2019
Messages
271
Office Version
  1. 365
Platform
  1. Windows
Book1.xlsm
ABCDEF
1postnocode1code2vxsourceflag
21C5678103000100ZV0001
31X0000114544428ZV0001
41X0000117300020ZV0001
51X0000119900019ZV0001
61X0000121200020ZV0001
71X0000125566613ZV0001
82C567810300090ZV0002
92X0000214544417ZV0002
102X0000217300019ZV0002
112X0000219900027ZV0002
122X0000221200016ZV0002
132X0000225566611ZV0002end
143C567810300080ZV0003
153X0000314544415ZV0003
163X0000317300010ZV0003
173X0000319900032ZV0003
183X0000321200014ZV0003
193X000032556669ZV0003
204C567810300070ZV0004
214X0000414544413ZV0004
224X0000417300027ZV0004
234X0000419900010ZV0004
244X0000421200012ZV0004
255X000042556668ZV0004end
265C567810300060ZV0005
275X0000514544411ZV0005
285X0000517300021ZV0005
295X0000519900010ZV0005
305X0000521200010ZV0005
315X000052556668ZV0005
Sheet2


I have the following bit of excellent code from @Peter_SSs which works perfectly on this
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("A" & Rows.Count).End(xlUp).Row
  With Sheets("journal")
    fr = 2
    Set rend = .Columns("F").Find(What:="end")
    Do
      i = i + 1
      'Sheets.Add(After:=Sheets(Sheets.Count)).Name = "New " & i
      .Rows(fr & ":" & rend.Row).Copy Destination:=Worksheets("journal_" & i).Range("A1")
' I think I need to do something at this point
      If rend.Row = lrD Then Exit Do
      fr = rend.Row + 1
      Set rend = .Columns("F").Find(What:="end", After:=rend)
      If rend.Row < fr Then Set rend = Range("F" & lrD)
    Loop Until rend.Row < fr
  End With
  Application.ScreenUpdating = True
End Sub

My challenge now is column A. If postno is 1 consistent throughout column A file then do nothing. In my example, the first new bit of code has postno = 3. In that example, I need it reset to 1.

So in the above example, A14:A19 would be 1, A20:A24 would be 2, A25:A31 would be 1. So basically, reset column A after each "end" in column F.

This file is huge so I probably need to use an array.

Any pointers welcome.
 
Last edited:

Excel Facts

How to find 2nd largest value in a column?
MAX finds the largest value. =LARGE(A:A,2) will find the second largest. =SMALL(A:A,3) will find the third smallest
The code you posted generates errors. The code below works properly to create the sheets and copy the data.
VBA Code:
Sub New_Sheets_v2()
    Application.ScreenUpdating = False
    Dim rend As Range, sAddr As String, fr As Long, i As Long
    fr = 2
    Set rend = Sheets("journal").Range("F:F").Find("end", LookIn:=xlValues, LookAt:=xlWhole)
    If Not rend Is Nothing Then
        sAddr = rend.Address
        Do
            i = i + 1
            Sheets.Add(after:=Sheets(Sheets.Count)).Name = "journal_ " & i
            Sheets("journal").Rows(fr & ":" & rend.Row).Copy Range("A1")
            fr = rend.Row + 1
            Set rend = Sheets("journal").Range("F:F").Find("end", after:=rend, LookIn:=xlValues, LookAt:=xlWhole)
        Loop While rend.Address <> sAddr
        sAddr = ""
    End If
    Application.ScreenUpdating = True
End Sub
However, I don't follow what you are trying to do when you say :
My challenge now is column A. If postno is 1 throughout the file then do nothing. In my example, the first new bit of code has postno = 3. In that example, I need it reset to 1.
So in the above example, A14:A19 would be 1, A20:A24 would be 2, A25:A31 would be 1.
Please post a copy of the sheet with the expected result.
 
Upvote 0

Forum statistics

Threads
1,216,037
Messages
6,128,442
Members
449,453
Latest member
jayeshw

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