Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7989809
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T12:51:00+00:00 2026-06-04T12:51:00+00:00

I need to insert or delete some rows depending on what a variable states.

  • 0

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
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T12:51:01+00:00Added an answer on June 4, 2026 at 12:51 pm

    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.

    Private Sub Worksheet_Change(ByVal Target As Range)
    
        Dim listRows As Integer, ganttRows As Integer, listRange As Range, ganttRange As Range
        Dim wks1 As Worksheet, wks2 As Worksheet
    
        Set wks1 = Worksheets("Sheet2")
        Set wks2 = Worksheets("Sheet1")
    
        Set listRange = Intersect(wks1.UsedRange, wks1.columns("B:B").EntireColumn)
        Set ganttRange = Intersect(wks2.UsedRange, wks2.columns("B:B").EntireColumn)
    
        listRows = listRange.Rows.count
        ganttRows = ganttRange.Rows.count
    
        If listRows > ganttRows Then 'sheet 1 has more rows, need to insert
            wks1.Range(wks1.Cells(listRows - (listRows - ganttRows), 1), wks1.Cells(listRows, 1)).EntireRow.Copy 
           wks2.Cells(ganttRows, 1).offset(1).PasteSpecial xlPasteValues
        ElseIf ganttRows > listRows 'sheet 2 has more rows need to delete
            wks2.Range(wks2.Cells(ganttRows, 1), wks2.Cells(ganttRows - (ganttRows - listRows), 1)).EntireRow.Delete
        End If
    
        Dim cel As Range
        'reset range because of updates
        Set ganttRange = Intersect(wks2.UsedRange, wks2.columns("B:B").EntireColumn)
    
        For Each cel In ganttRange
            If cel.Row Mod 2 = 0 Then cel.EntireRow.Interior.ColorIndex = 20
        Next
    
    End Sub
    

    UPDATE

    Just re-read this line

    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.
    

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to insert, delete and update data using a xml file. I have
I need to delete some records from table and then insert some records to
I have a trigger for insert/update/delete. That is working fine. Also, I need the
Hi need insert data in table if the record is not already exits Ex:
Need to insert selected text on the page into textarea. There must be some
I need to insert about 1.8 million rows from a CSV file into a
I need to insert/update data by using link or image I need a code
I need to insert data from table1 into table2. However, I would like to
I need to delete many rows from sql server 2008 database, it must be
I need to get stored procedure (not standart, only custom) to delete unused rows

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.