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
 

Excel Facts

Copy PDF to Excel
Select data in PDF. Paste to Microsoft Word. Copy from Word and paste to Excel.
but in reality I don't have any formulas whether in Total row or column L ,
I'm confused, you don't have formula in Total row? so where did the numbers come from?
just to understand who calculate before insert row and how cold be after insert row
what do you mean by this?

Could you upload a sample workbook (without sensitive data) to a sharing site like dropbox.com or google drive?
And then share the link here.
 
Upvote 0
Hi Akuini ,
I'm confused, you don't have formula in Total row? so where did the numbers come from?
yes , the number come from another macro . you can run it and see it what happens.
what do you mean by this?
when I say there is no formula you need to know how calculate the inserted row so I put the formula manually to know from the basic how calculate it. so the calculation depends on previous row in column L as I did it .
here is the file
insert row.xlsm
 
Upvote 0
I've downloaded your file. I'll try to write the code tomorrow.
 
Upvote 0
I changed the sheet name to "Daily".
Try this code for commandbutton:
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
        
        .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 1
I 'm grateful for you dedication your time to help me .
thank you so much .:)
 
Upvote 0
You're welcome, glad to help & thanks for the feedback.:)
 
Upvote 0
Hi Akuini
I need help for this part of my problem !
before insert row I want fill blank row before TOTAL row if I have already blank row . and if no blank row ,then the original code should insert new row as you did it.
thanks again
 
Upvote 0

Forum statistics

Threads
1,215,883
Messages
6,127,544
Members
449,385
Latest member
KMGLarson

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