Hi all,
I am fairly new to VBA code so please excuse any mistakes I am making here.
My goal is to find a string (in this example TestStart) and copy everything between that and a second string (TestEnd). These strings are repeatable in the data, so I would like to copy each section between these two strings to a new sheet.
I had been able to find various examples of code online that I have used and this allows me to successfully copy the data between the first TestStart and first TestEnd to a new sheet (called NewSheet in this example). When the code then tries to copy over the data between the second time TestStart and TestEnd are found, it tries to create the another sheet called NewSheet. Which obviously exists.
Can anyone advise how I can copy the repeatable data to the same sheet?
Data Example -
So from the data above, I would want to have everything copied over to NewSheet apart from CCC.
Code Example -
Sub CopyTEST()
Dim rownum As Long
Dim colnum As Long
Dim startrow As Long
Dim endrow As Long
Dim lastrow As Long
rownum = 1
colnum = 1
lastrow = Worksheets(1).Range("A65536").End(xlUp).Row
With ActiveWorkbook.Worksheets(1).Range("a1:a" & lastrow)
For rownum = 1 To lastrow
Do
If .Cells(rownum, 1).Value = "TestStart" Then
startrow = rownum
End If
rownum = rownum + 1
If (rownum > lastrow) Then Exit For
Loop Until .Cells(rownum, 1).Value = "TestEnd"
endrow = rownum
rownum = rownum + 1
Worksheets(1).Range(startrow & ":" & endrow).copy
Sheets.Add(After:=Sheets("TestSheet")).Name = "NewSheet"
Sheets("NewSheet").Select
Range("A1").Select
ActiveSheet.Paste
Next rownum
End With
End Sub
Regards,
Gavin
I am fairly new to VBA code so please excuse any mistakes I am making here.
My goal is to find a string (in this example TestStart) and copy everything between that and a second string (TestEnd). These strings are repeatable in the data, so I would like to copy each section between these two strings to a new sheet.
I had been able to find various examples of code online that I have used and this allows me to successfully copy the data between the first TestStart and first TestEnd to a new sheet (called NewSheet in this example). When the code then tries to copy over the data between the second time TestStart and TestEnd are found, it tries to create the another sheet called NewSheet. Which obviously exists.
Can anyone advise how I can copy the repeatable data to the same sheet?
Data Example -
TestStart |
AAA |
BBB |
TestEnd |
CCC |
TestStart |
DDD |
EEE |
FFF |
TestEnd |
So from the data above, I would want to have everything copied over to NewSheet apart from CCC.
Code Example -
Sub CopyTEST()
Dim rownum As Long
Dim colnum As Long
Dim startrow As Long
Dim endrow As Long
Dim lastrow As Long
rownum = 1
colnum = 1
lastrow = Worksheets(1).Range("A65536").End(xlUp).Row
With ActiveWorkbook.Worksheets(1).Range("a1:a" & lastrow)
For rownum = 1 To lastrow
Do
If .Cells(rownum, 1).Value = "TestStart" Then
startrow = rownum
End If
rownum = rownum + 1
If (rownum > lastrow) Then Exit For
Loop Until .Cells(rownum, 1).Value = "TestEnd"
endrow = rownum
rownum = rownum + 1
Worksheets(1).Range(startrow & ":" & endrow).copy
Sheets.Add(After:=Sheets("TestSheet")).Name = "NewSheet"
Sheets("NewSheet").Select
Range("A1").Select
ActiveSheet.Paste
Next rownum
End With
End Sub
Regards,
Gavin