I need to insert or delete some rows depending on what a variable states.
Sheet1 has a list of data. With sheet2 which is formatted, i want to copy that data so sheet2 is just a template and sheet1 is like a user form.
What my code does up until the for loop is get the number of rows in sheet 1 which only contains data and also the number of rows in sheet2 which contains data.
If the user adds some more data to sheet1 then i need to insert some more rows at the end the data in sheet2 and if the user deletes some rows in sheet1 the rows are deleted from sheet2.
I can get the number of rows on each so now how many to insert or delete but that’s where i’ve come unstuck. How would I insert/delete the correct amount of rows. Also i wanted to alternate the rows colours between white and grey.
I did think that it might be an idea to delete all the rows on sheet2 then insert the same amount of rows that are in sheet1 using the alternating row colours but then again i did see something about using mod in the conditional formatting.
Can anyone please help?
Option Explicit
Private Sub Worksheet_Change(ByVal Target As Range)
Dim listRows As Integer, ganttRows As Integer, listRange As Range, ganttRange As Range
Dim i As Integer
Set listRange = Columns("B:B")
Set ganttRange = Worksheets("Sheet2").Columns("B:B")
listRows = Application.WorksheetFunction.CountA(listRange)
ganttRows = Application.WorksheetFunction.CountA(ganttRange)
Worksheets("Sheet2").Range("A1") = ganttRows - listRows
For i = 1 To ganttRows - listRows
'LastRowColA = Range("A65536").End(xlUp).Row
Next i
If Target.Row Mod 2 = 0 Then
Target.EntireRow.Interior.ColorIndex = 20
End If
End Sub
I didn’t test this, because I didn’t have sample data, but try this out. You may need to change some of the cell referencing to fit your needs.
UPDATE
Just re-read this line
My solution is based on if the user insert / deletes rows at the bottom of the worksheet. If the user inserts / deletes rows in the middle, you are better off copy the entire range from sheet1 and onto a cleared out sheet2.