Hi All,
I have some code which takes some data and then matches it to a specific column and then inputs that data on the next available row.
What I want to happen is that when the data gets to 8 rows, I would like it go back to the first row in that column and overwrite the data back to the 8 rows and then start again.
Here is the code I have so far:
Private Sub Worksheet_Change(ByVal Target As Range)
If Not Intersect(Target, Target.Worksheet.Range("A2")) Is Nothing Then CopyRng
End Sub
Sub CopyRng()
Dim WS1 As Worksheet
Dim Rng As Range
Dim Col As Long
On Error Resume Next 'Without this macro crashes if there's no match
Set WS3 = Sheets("Sheet3")
Set Rng = WS3.Range("A6") 'column header
With WS3
'Finds the column to copy:
Col = Application.WorksheetFunction.Match(WS3.Range("A6").Value, .Rows("1:1"), False)
'Writes the values to the last empty cell from the bottom of the column:
.Cells(.Rows.Count, Col).End(xlUp).Offset(1, 0).Resize(Rng.Rows.Count).Value = Range("A2").Value
End With
End Sub
So this does what I want but instead of finding the next available row in that column, I would like to determine if there are already 8 lines of data and if there are overwrite the old data in line 1 and then continue.
Thanks
Matt
P.S I can't find how to add the code in the excel format like other posts
I have some code which takes some data and then matches it to a specific column and then inputs that data on the next available row.
What I want to happen is that when the data gets to 8 rows, I would like it go back to the first row in that column and overwrite the data back to the 8 rows and then start again.
Here is the code I have so far:
Private Sub Worksheet_Change(ByVal Target As Range)
If Not Intersect(Target, Target.Worksheet.Range("A2")) Is Nothing Then CopyRng
End Sub
Sub CopyRng()
Dim WS1 As Worksheet
Dim Rng As Range
Dim Col As Long
On Error Resume Next 'Without this macro crashes if there's no match
Set WS3 = Sheets("Sheet3")
Set Rng = WS3.Range("A6") 'column header
With WS3
'Finds the column to copy:
Col = Application.WorksheetFunction.Match(WS3.Range("A6").Value, .Rows("1:1"), False)
'Writes the values to the last empty cell from the bottom of the column:
.Cells(.Rows.Count, Col).End(xlUp).Offset(1, 0).Resize(Rng.Rows.Count).Value = Range("A2").Value
End With
End Sub
So this does what I want but instead of finding the next available row in that column, I would like to determine if there are already 8 lines of data and if there are overwrite the old data in line 1 and then continue.
Thanks
Matt
P.S I can't find how to add the code in the excel format like other posts