I've written a macro which copys the strings of three cells left to a marked cell and pastes those to the next free line in a specific worksheet:
Sub testmacro_01()
'setting the variables
Dim x As Integer
Dim y As Integer
Dim string1 As String
Dim string2 As String
Dim string3 As String
'setting start values
x = 1
y = 1
string1 = ""
string2 = ""
string3 = ""
'checking for "m" in the "checkcolumn", if "m" then copy columns left to it:
For x = 1 To 100
If ThisWorkbook.Sheets("testsheet").Cells(x, 4).Value = "m" _
Then
string1 = ThisWorkbook.Sheets("testsheet").Cells(x, 1).Value
string2 = ThisWorkbook.Sheets("testsheet").Cells(x, 2).Value
string3 = ThisWorkbook.Sheets("testsheet").Cells(x, 3).Value
'checking for the next free line in "newsheet":
Line1:
If ThisWorkbook.Sheets("newsheet").Cells(y, 1).Value = "" _
And ThisWorkbook.Sheets("newsheet").Cells(y, 2).Value = "" _
And ThisWorkbook.Sheets("newsheet").Cells(y, 1).Value = "" _
Then
'pasting the strings into the free lines:
ThisWorkbook.Sheets("newsheet").Cells(y, 1).Value = string1
ThisWorkbook.Sheets("newsheet").Cells(y, 2).Value = string2
ThisWorkbook.Sheets("newsheet").Cells(y, 3).Value = string3
Else
'if the checked line is full the search will go down by 1 line:
y = y + 1
GoTo Line1
End If
End If
Next
End Sub
For example: This is the source sheet
(every line left to a line marked with the letter "m" in column D should be copied)
and this is the result after playing the macro.
(cells with grey background are there to test the "next free line function")
That's where I'm stuck: While this macro works and does what it should do, I feel like it is quite static and can be done more "professionally". My focus here is on the "for to" loop: How do I put a variable number which will always include all the existing lines in the textsheet into the for to loop instead of the "100"? Changing 100 to 1000 will work for most of my applications, but seems very prude.