how insert and delete rows based on two cells values

abdelfattah

Well-known Member
Joined
May 3, 2019
Messages
1,494
Office Version
  1. 2019
  2. 2010
Platform
  1. Windows
hello
i need help from experts . what I want add a new rows based on two cells L2,M2 .L2=NAME , M2= ROWS NUMBERS so if I write the name and the rows numbers . it should look at to COL C to match the name and insert the rows based on L2,M2 , and if the L2,M2 are empty then should delete all of empty rows for all names and if the L2= empty and the M2 is rows numbers then insert rows for all names .
note: it should insert row with the same borders and formulas
this the orginal data
MATCH.xlsm
ABCDEFGHIJKLMNO
1NAMEDEBITCREDITBALANCEnamerow
21,000.00200.00800.00
3ITEMDATENAMEINVOICE NONOTEWAY OF PAID DEBITCREDITBALANCE
418/7/2021ALI1INV-1000NO PAID-10001,000.00
528/10/2021ALI1INV-1000PAIDCASH200.00800.00
6
7
8NAMEDEBITCREDITBALANCE
93,200.00100.003,100.00
10ITEMDATENAMEINVOICE NONOTEWAY OF PAID DEBITCREDITBALANCE
1118/8/2021ALI2INV-1001NO PAID-20002,000.00
1228/11/2021ALI2INV-1001PAIDCASH100.001,900.00
1338/13/2021ALI2INV-1003NO PAID-12003,100.00
14
15NAMEDEBITCREDITBALANCE
163,800.001,000.002,800.00
17ITEMDATENAMEINVOICE NONOTEWAY OF PAID DEBITCREDITBALANCE
1817/16/2021ALI3INV-1002PAIDCASH3000500.002,500.00
1927/16/2021ALI3INV-1002PAIDBANK500.002,000.00
2037/16/2021ALI3INV-1004NO PAID-8002,800.00
result
Cell Formulas
RangeFormula
B2:C2B2=SUM(G4:G5)
D2,I18,D16,I11,D9,I4D2=B2-C2
I5,I19:I20,I12:I13I5=I4+G5-H5
B16:C16,B9:C9B9=SUM(G11:G13)
 

Excel Facts

Select a hidden cell
Somehide hide payroll data in column G? Press F5. Type G1. Enter. Look in formula bar while you arrow down through G.
so the end result of entering "ALI1" in L2 and 3 in M2 should be the below?
Book1
ABCDEFGHIJKLM
1NAMEDEBITCREDITBALANCEALI13
21,000200800
3ITEMDATENAMEINVOICE NONOTEWAY OF PAIDDEBITCREDITBALANCE
418/7/2021ALI1INV-1000NO PAID-1,0001,000
528/10/2021ALI1INV-1000PAIDCASH200800
63ALI1800
74ALI1800
85ALI1800
Sheet5
Cell Formulas
RangeFormula
B2:C2B2=SUM(G4:G8)
D2,I4D2=+B2-C2
I5:I8I5=+I4+G5-H5
 
Upvote 0
Ok, I have a partial solution that you can test on a copy of your workbook. This solution will ONLY do the following:

  1. If L2 has a name and M2 has a number, it will insert the number of rows in M2 for the name in L2
  2. If L2 does NOT have a name and M2 has a number, it will insert the number of rows for ALL names

You will need to change the formula in column I to "=SUM(G$5:G5)-SUM(H$5:H5)" for each row for every name so that the anchored row is the first row of data for the name. This is needed so that the formula can be copied in the event that a Name only has one row. It will keep the same running total that you currently calculate using a different formula in the first row than in rows 2 thru n.

Lastly can you confirm what constitutes an empty row? Can I key on the Date column and if there is no value in that column for a given row, the row is considered empty?

VBA Code:
Sub InsDelRows()
    Dim arr As New Collection, a
    Dim n As Variant
    Dim Name As String
    Dim NoRows As Long
    Dim FstRow As Long
    Dim InsRow As Long
    Dim FstRowColC As Long
    Dim LstRowColC As Long
    
    Name = Range("L2").Value
    NoRows = Abs(Round(Range("M2").Value, 0))
        
    If Name <> "" And NoRows > 0 Then
        FstRow = WorksheetFunction.Match(Name, Range("C:C"), 0)
        If Cells(FstRow + 1, 3).Value <> Name Then
            InsRow = FstRow
        Else
            InsRow = Cells(FstRow, 3).End(xlDown).Row
        End If
        Rows(InsRow & ":" & InsRow).Copy
        Rows(InsRow & ":" & InsRow + NoRows - 1).Insert Shift:=xlDown
        Application.CutCopyMode = False
        If FstRow = InsRow Then
            Cells(InsRow + 1, 1) = 2
        End If
        Range(Cells(FstRow, 1), Cells(FstRow + 1, 1)).AutoFill Destination:=Range(Cells(FstRow, 1), Cells(InsRow + NoRows, 1))
        Range(Cells(InsRow + 1, 2), Cells(InsRow + NoRows, 2)).ClearContents
        Range(Cells(InsRow + 1, 4), Cells(InsRow + NoRows, 8)).ClearContents
    Else
        FstRowColC = WorksheetFunction.Match("Name", Range("C:C"), 0) + 1
        LstRowColC = Range("C" & Rows.Count).End(xlUp).Row
        
        On Error Resume Next
        n = Range(Cells(FstRowColC, 3), Cells(LstRowColC, 3)).Value
        For Each a In n
            If a <> "NAME" And a <> "CREDIT" Then arr.Add a, a
        Next
        On Error GoTo 0
    End If
        
    If Name = "" And NoRows > 0 Then
        For i = 1 To arr.Count
            Name = arr(i)
            FstRow = WorksheetFunction.Match(Name, Range("C:C"), 0)
            If Cells(FstRow + 1, 3).Value <> Name Then
                InsRow = FstRow
            Else
                InsRow = Cells(FstRow, 3).End(xlDown).Row
            End If
            Rows(InsRow & ":" & InsRow).Copy
            Rows(InsRow & ":" & InsRow + NoRows - 1).Insert Shift:=xlDown
            Application.CutCopyMode = False
            If FstRow = InsRow Then
                Cells(InsRow + 1, 1) = 2
            End If
            Range(Cells(FstRow, 1), Cells(FstRow + 1, 1)).AutoFill Destination:=Range(Cells(FstRow, 1), Cells(InsRow + NoRows, 1))
            Range(Cells(InsRow + 1, 2), Cells(InsRow + NoRows, 2)).ClearContents
            Range(Cells(InsRow + 1, 4), Cells(InsRow + NoRows, 8)).ClearContents
        Next
    End If
End Sub
 
Upvote 0
thanks for your code, but unfortunately it doesn't work at all and no error.

about the formula I can't change it when insert a new rows the formula .it should depend on last row contains the formula .about column date it's. empty
 
Upvote 0
I need some more information to help understand why it isn't working for you.
  1. What version of Excel do you have?
  2. Where did you place the code?
  3. How are you calling the code?
  4. Is the sheet that it is supposed to update the active worksheet when you call the code?
As for the formula change, both formulas give the same result (see below). The difference is that mine will work when there is only one row to begin with and the formulas have to be copied down to more rows where yours would need more tweaks to get there.

Solutions 20210717 Mr Excel - abdelfattah.xlsm
ABCDEFGHIJ
21NAMEDEBITCREDITBALANCE
22YOUR FxMY Fx
23ITEMDATENAMEINVOICE NONOTEWAY OF PAIDDEBITCREDITBALANCEBALANCE
2417/16/2021ALI3INV-1002PAIDCASH3,0005002,5002,500
2527/16/2021ALI3INV-1002PAIDBANK5002,0002,000
2637/16/2021ALI3INV-1004NO PAID-8002,8002,800
Sheet5
Cell Formulas
RangeFormula
I24I24=+G24-H24
J24:J26J24=SUM(G$24:G24)-SUM(H$24:H24)
I25:I26I25=+I24+G25-H25
 
Upvote 0
I run in another PC .it give Unable to get the Match property of the WorksheetFunction class in this line
VBA Code:
FstRow = WorksheetFunction.Match(Name, Range("C:C"), 0)
 
Upvote 0
Hi,​
you should better use Application.Match with a Variant variable and test its result with IsError or IsNumeric VBA function …​
 
Upvote 0
@Crystalyzer thanks the name was wrong in L2 . it was containing the space that's why I said nothing happens , but how apply condition if L2,M2 are empty then should delete empty rows from all ranges ?
 
Upvote 0
I've revamped the original code so that you do not have to change your formulas. I've also completed deleting all blank rows for all ranges when L2 and M2 are empty and took @Marc L 's advice and changed the Worksheetfunction.match to Application.Match. I've inserted messaging as well to let you know when actions are taken and what they were.

Please test on a copy of your workbook.

VBA Code:
Sub InsDelRows()
   
    Dim Name As String
    Dim InsMsg, DelMsg As String
    Dim NoRows, FstRow, InsRow, DelRow, EndDelRow As Long
   
    Name = Range("L2").Value
    NoRows = Abs(Round(Range("M2").Value, 0))
       
    'Single name inserting rows
    If Name <> "" And NoRows > 0 Then
        Call InsertRows(Name, NoRows)
        MsgBox NoRows & " rows inserted for " & Name & ",", vbOKOnly, "Information"
    End If
   
    'All names Insert blank rows
    If Name = "" And NoRows > 0 Then
        Set arr = CreateUniqueNamesArr()
        For i = 1 To arr.Count
            Name = arr(i)
            Call InsertRows(Name, NoRows)
            If InsMsg = "" Then
                InsMsg = "Inserted " & NoRows & " rows for the following names: " & vbCrLf & Name
            Else
                InsMsg = InsMsg & ", " & Name
                If i <> arr.Count Then
                    InsMsg = InsMsg & "."
                Else
                    InsMsg = InsMsg & ","
                End If
            End If
        Next
        MsgBox InsMsg, vbOKOnly, "Information"
    End If
   
    'Single name delete blank rows
    If Name <> "" And NoRows = 0 Then
        If Application.Match(Name, Range("C:C"), 0) <> 0 Then
            MsgBox DeleteBlankRows(Name), vbOKOnly, "Information"
        Else
            MsgBox Name & " not found.  No rows deleted.", vbOKOnly, "Information"
        End If
    End If
  
    'All names delete blank rows
    If Name = "" And NoRows = 0 Then
        Set arr = CreateUniqueNamesArr()
        For i = 1 To arr.Count
            Name = arr(i)
            If DelMsg = "" Then
                DelMsg = DeleteBlankRows(Name)
            Else
                DelMsg = DelMsg & vbCrLf & DeleteBlankRows(Name)
            End If
        Next
        MsgBox DelMsg, vbOKOnly, "Information"
    End If

End Sub

Private Sub InsertRows(ByVal fxName As String, ByVal fxNoRows As Long)
    Dim fxFstRow, fxInsRow As Long
   
    fxFstRow = Application.Match(fxName, Range("C:C"), 0)
    If Cells(fxFstRow + 1, 3).Value <> fxName Then
        fxInsRow = fxFstRow
    Else
        fxInsRow = Cells(fxFstRow, 3).End(xlDown).Row
    End If
   
    If fxFstRow = fxInsRow Then
        Rows(fxInsRow & ":" & fxInsRow).Copy
        Rows(fxInsRow & ":" & fxInsRow + fxNoRows - 1).Insert Shift:=xlDown
        Application.CutCopyMode = False
        Cells(fxInsRow + 1, 1) = 2
        Cells(fxInsRow - 2, 2).Formula = "=SUM(G" & fxInsRow & " : G" & fxInsRow + fxNoRows & ")"
        Cells(fxInsRow - 2, 3).Formula = "=SUM(H" & fxInsRow & " : H" & fxInsRow + fxNoRows & ")"
        Range(Cells(fxInsRow + 1, 9), Cells(fxInsRow + 1 + fxNoRows - 1, 9)).FormulaR1C1 = "=R[-1]C+RC[-2]-RC[-1]"
        Range(Cells(fxFstRow, 1), Cells(fxFstRow + 1, 1)).AutoFill Destination:=Range(Cells(fxFstRow, 1), Cells(fxInsRow + fxNoRows, 1))
        Range(Cells(fxInsRow + 1, 2), Cells(fxInsRow + fxNoRows, 2)).ClearContents
        Range(Cells(fxInsRow + 1, 4), Cells(fxInsRow + fxNoRows, 8)).ClearContents
    Else
        Rows(fxInsRow & ":" & fxInsRow).Copy
        Rows(fxInsRow & ":" & fxInsRow + fxNoRows - 1).Insert Shift:=xlDown
        Application.CutCopyMode = False
        Range(Cells(fxInsRow, 9), Cells(fxInsRow + 1 + fxNoRows - 1, 9)).FormulaR1C1 = "=R[-1]C+RC[-2]-RC[-1]"
        Range(Cells(fxFstRow, 1), Cells(fxFstRow + 1, 1)).AutoFill Destination:=Range(Cells(fxFstRow, 1), Cells(fxInsRow + fxNoRows, 1))
        Range(Cells(fxInsRow + 1, 2), Cells(fxInsRow + fxNoRows, 2)).ClearContents
        Range(Cells(fxInsRow + 1, 4), Cells(fxInsRow + fxNoRows, 8)).ClearContents
    End If
       
End Sub

Private Function CreateUniqueNamesArr() As Variant
    Dim ar As New Collection, a
    Dim n As Variant
    Dim FstRowColC, LstRowColC As Long
   
    FstRowColC = Application.Match("Name", Range("C:C"), 0) + 1
    LstRowColC = Range("C" & Rows.Count).End(xlUp).Row
   
    On Error Resume Next
    n = Range(Cells(FstRowColC, 3), Cells(LstRowColC, 3)).Value
    For Each a In n
        If a <> "NAME" And a <> "CREDIT" Then ar.Add a, a
    Next
    On Error GoTo 0
    Set CreateUniqueNamesArr = ar
End Function

Private Function DeleteBlankRows(fxName As String) As String
    Dim fxFstRow, fxDelRow, fxResult As Long
   
    fxResult = 1
   
    fxFstRow = Application.Match(fxName, Range("C:C"), 0)
    If fxFstRow <> 0 Then
        If LCase(Cells(fxFstRow + 1, 3).Value) <> LCase(fxName) Then
            fxResult = 1
        Else
            fxDelRow = Cells(fxFstRow, 3).End(xlDown).Row
            i = fxFstRow
            Do While i <= fxDelRow
                If Not IsDate(Cells(i, 2)) Then
                    Range(i & ":" & i).EntireRow.Delete
                    fxDelRow = fxDelRow - 1
                    fxResult = 0
                Else
                    i = i + 1
                End If
            Loop
        End If
    End If

    Select Case fxResult
        Case 0
            DeleteBlankRows = "All blank rows deleted for " & fxName & "."
        Case 1
            DeleteBlankRows = "No blank rows exist for " & fxName & "."
    End Select
End Function
 
Upvote 0
Solution

Forum statistics

Threads
1,223,880
Messages
6,175,155
Members
452,615
Latest member
bogeys2birdies

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top