The code needs to detect the word "EXECSDATE" in Sheet1 Column B and copy the line together with the lines below it to Sheet2 until it reaches another word "EXECSDATE." Since I have 5 "EXECSDATE" in Sheet1 there should be a total of 5 Sheets separated.
I already have tried running my code but it shows some error and can't do what it must.
Sub Fails()
Dim mFind As Range
Dim Compteur As Integer
Dim IdSheet As Integer
Dim ErrorBool As Boolean
debut:
Set mFind = Columns("B").Find("EXECSDATE")
Set mfind2 = Columns("B").Find("EXECSDATE")
If mFind Is Nothing Then
MsgBox "There is no cell found with the text 'EXECSDATE'" _
& " in column A of the active sheet."
Exit Sub
End If
firstaddress = mFind.Address
IdSheet = 1
Compteur = 0
Do
Set mfind2 = Columns("B").FindNext(mFind)
If mfind2 Is Nothing Then
Compteur = 0
Else:
If mFind.Row < mfind2.Row Then
Compteur = mfind2.Row
End If
If mFind.Row > mfind2.Row Then
ErrorBool = True
End If
If ErrorBool = True Then
Range(mFind, Cells(mFind.Row + 1, "B")).EntireRow.Cut
End If
End If
Range("B" & mFind.Row + 1 & ":B" & Compteur - 1).EntireRow.Cut
If mFind Is Nothing Then
Else: IdSheet = IdSheet + 1
End If
Sheets("Sheet" & IdSheet & "").Select
Range("B" & Rows.Count).End(xlUp).Offset(1, 0).Select
ActiveSheet.Paste
line:
Sheets("Sheet1").Select
Range(mFind, Cells(mFind.Row, "B")).EntireRow.Delete
Set mFind = Columns("B").Find("EXECSDATE")
Set mfind2 = Columns("B").Find("EXECSDATE")
If mFind Is Nothing Then Exit Sub
Set mFind = Columns("B").FindNext(mFind)
Loop While mFind.Address <> firstaddress
End Sub
Error Message:
This selection isn't valid. Make sure the copy and paste areas don't overlap unless they are the same size and shape.
Here is the first EXECSDATE word (which is supposed to go to Sheet1):
Here is the second EXECSDATE word (which is supposed to go to Sheet2):