complicated to insert row by userform based on matching combobox before TOTAL row

Ali M

Active Member
Joined
Oct 10, 2021
Messages
290
Office Version
  1. 2019
  2. 2013
Platform
  1. Windows
Hi experts ,
I search for way to insert new row before TOTAL row for each separated range based on match name for column I .
if the selected name from combobox1 is matched with name in column I for separated range , then should insert new row before TOTAL row and recalculation based on previous row and change sum TOTAL values for columns J,K,l ,but in reality I don't have any formulas whether in Total row or column L , just to understand who calculate before insert row and how cold be after insert row .
so when fill data on userform for textbox1=column I, textbox2= column J, textbox3=column K , as to column H should populate date(today)

so this is when I fill userform
AS1.PNG



and see the highlighted as in result
insert row (version 1).xlsb
HIJKL
1NAME
2ALI
3DATEACCOUNT NAMEDEBIT CREDITBALANCE
420/06/2023CASH BALANCE200,000.00200,000.00
521/06/2023CASH PR 15,000.0030,500.00184,500.00
622/06/2023CASH DM9,500.0037,000.00157,000.00
722/06/2023EXPENSE ADMIN65,000.0092,000.00
823/06/2023EXPENSE PR1,200.0090,800.00
9TOTAL224,500.00133,700.0090,800.00
10
11NAME
12OMAR
13ITEMACCOUNT NAMEDEBIT CREDITBALANCE
1420/06/2023PURCHASE15,000.0015,000.00
1521/06/2023STOCK1100,000.00115,000.00
1622/06/2023PURCHASE RETURNS4,000.00111,000.00
1722/06/2023PURCHASE LOW 6,200.00117,200.00
1822/06/2023EXPENSE PR37,000.00154,200.00
19TOTAL158,200.004,000.00154,200.00
20
21NAME
22MAHMOUD
23ITEMACCOUNT NAMEDEBITCREDITBALANCE
2422/06/2023SALES201,000.00-201,000.00
2522/06/2023SALES RETURNS3,500.00-197,500.00
SE
Cell Formulas
RangeFormula
L4,L24,L19,L14,L9L4=J4-K4
L25,L15:L18,L5:L8L5=L4+J5-K5
J9J9=SUM(J4:J7)
K9,J19:K19K9=SUM(K4:K8)

thanks in advance
 
before insert row I want fill blank row before TOTAL row if I have already blank row
Sorry for the late reply.
I don't understand what you meant. Can you explain in more detail?
 
Upvote 0

Excel Facts

Easy bullets in Excel
If you have a numeric keypad, press Alt+7 on numeric keypad to type a bullet in Excel.
thanks for come back for me .
take this picture
there is empty row for range
insert row.xlsm
HIJKL
1NAME
2ALI
3DATEACCOUNT NAMEDEBIT CREDITBALANCE
420/06/2023CASH BALANCE200,000.00200,000.00
521/06/2023CASH PR 15,000.0030,500.00184,500.00
622/06/2023CASH DM9,500.0037,000.00157,000.00
722/06/2023EXPENSE ADMIN65,000.0092,000.00
8
9TOTAL224,500.00132,500.0092,000.00
ورقة1
Cell Formulas
RangeFormula
L4,L9L4=J4-K4
L5:L7L5=L4+J5-K5
J9J9=SUM(J4:J7)
K9K9=SUM(K4:K8)


and when fill by userform
shouldn't insert new row before fill data in empty row if there is empty row
1.PNG

another process

when there is no empty row , then the original code does it
before
insert row.xlsm
HIJKL
1NAME
2ALI
3DATEACCOUNT NAMEDEBIT CREDITBALANCE
420/06/2023CASH BALANCE200,000.00200,000.00
521/06/2023CASH PR 15,000.0030,500.00184,500.00
622/06/2023CASH DM9,500.0037,000.00157,000.00
722/06/2023EXPENSE ADMIN65,000.0092,000.00
820/07/2023EXPENSE PR1,200.0090,800.00
9TOTAL224,500.00133,700.0090,800.00
ورقة1
Cell Formulas
RangeFormula
L4,L9L4=J4-K4
L5:L8L5=L4+J5-K5
J9J9=SUM(J4:J7)
K9K9=SUM(K4:K8)


after it
2.PNG
 
Upvote 0
If it's only 1 row then try this:
VBA Code:
Private Sub CommandButton1_Click()
Dim i As Long, q As Long, n As Long
Dim va

On Error GoTo Skip:

With Sheets("Daily")
    
    va = .Range("i1", .Cells(.Rows.Count, "i").End(xlUp))

    If ComboBox1.ListIndex > -1 Then
    
        'find total row below the name selected in combobox1
        For i = 1 To UBound(va, 1)
            If LCase(va(i, 1)) = LCase(ComboBox1.Text) Then
                q = i
                Do
                    i = i + 1
                Loop Until va(i, 1) = "TOTAL"
            Exit For
            End If
        Next
                    
        If WorksheetFunction.CountA(.Range("H" & i - 1).Resize(, 5)) = 0 Then
            i = i - 1
            .Range("H" & i).Resize(, 5).Delete Shift:=xlUp
        End If
        
        .Range("H" & i).Resize(, 5).Insert Shift:=xlDown  'add new row in col H:L
        
        'in new row
        .Range("H" & i) = Date  'insert today
        .Range("I" & i) = TextBox1
        If Len(Trim(TextBox2)) > 0 Then .Range("J" & i) = Val(TextBox2)
        If Len(Trim(TextBox3)) > 0 Then .Range("K" & i) = Val(TextBox3)
        .Range("L" & i) = Evaluate("=(L" & i - 1 & "+ J" & i & "- K" & i & ")")
        
        'in total rows
        .Range("J" & i + 1) = Evaluate("=SUM(J" & q & ":J" & i & ")")
        .Range("K" & i + 1) = Evaluate("=SUM(K" & q & ":K" & i & ")")
        '.Range("L" & i + 1) = Evaluate("=(J" & i + 1 & "- K" & i + 1 & ")")
        .Range("L" & i + 1) = .Range("L" & i)  'balance
     
        'format new row
        .Range("H" & i - 1).Resize(, 5).Copy
        .Range("H" & i).PasteSpecial xlPasteFormats
        Application.CutCopyMode = False
    End If


End With

Exit Sub

Skip:
MsgBox "Error number " & Err.Number & " : " & Err.Description

End Sub
 
Upvote 0
sorry for delaying to answer your question !🙏🙏

yes
So, if there are multiple empty rows, what should be the result? Should we delete all empty rows except one, which will be filled with new data? Or should we just fill the first empty row with new data?
 
Upvote 0
yes should be .
Try this one:
VBA Code:
Private Sub CommandButton1_Click()
Dim i As Long, q As Long, n As Long, h As Long
Dim va

On Error GoTo Skip:

With Sheets("Daily")
    
    va = .Range("i1", .Cells(.Rows.Count, "i").End(xlUp))

    If ComboBox1.ListIndex > -1 Then
    
        'find total row below the name selected in combobox1
        For i = 1 To UBound(va, 1)
            If LCase(va(i, 1)) = LCase(ComboBox1.Text) Then
                q = i
                Do
                    If h = 0 And va(i, 1) = "" Then
                        h = i
                    End If
                    i = i + 1
                Loop Until va(i, 1) = "TOTAL"
            Exit For
            End If
        Next
             
                   
        If h > 0 Then
            xx = h
            i = i - 1
        Else
            xx = i
           .Range("H" & i).Resize(, 5).Insert Shift:=xlDown  'add new row in col H:L
        End If
        
        
        'in new row
        .Range("H" & xx) = Date  'insert today
        .Range("I" & xx) = TextBox1
        If Len(Trim(TextBox2)) > 0 Then .Range("J" & xx) = Val(TextBox2)
        If Len(Trim(TextBox3)) > 0 Then .Range("K" & xx) = Val(TextBox3)
        .Range("L" & xx) = Evaluate("=(L" & xx - 1 & "+ J" & xx & "- K" & xx & ")")
        
        'in total rows
        .Range("J" & i + 1) = Evaluate("=SUM(J" & q & ":J" & i & ")")
        .Range("K" & i + 1) = Evaluate("=SUM(K" & q & ":K" & i & ")")
        '.Range("L" & i + 1) = Evaluate("=(J" & i + 1 & "- K" & i + 1 & ")")
        .Range("L" & i + 1) = .Range("J" & i + 1) - .Range("K" & i + 1)  'balance
     
        'format new row
        .Range("H" & xx - 1).Resize(, 5).Copy
        .Range("H" & xx).PasteSpecial xlPasteFormats
        Application.CutCopyMode = False
    End If


End With

Exit Sub

Skip:
MsgBox "Error number " & Err.Number & " : " & Err.Description

End Sub
 
Upvote 1
Solution
magnificent !
I' m grateful for your time and assistance !:)
thanks again buddy.;)
 
Upvote 0

Forum statistics

Threads
1,215,160
Messages
6,123,355
Members
449,097
Latest member
thnirmitha

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