insert row after last duplicated item based on selected combobox on userform

Ali M

Active Member
Joined
Oct 10, 2021
Messages
348
Office Version
  1. 2019
  2. 2013
Platform
  1. Windows
Hello
I want copying data from userform to sheet("daily") by insert new row after last row contains duplicate item in column b and match with selected combobox ,
then should copy combobox1,textbox1,textbox2= columns B,C,D ,as to column A should populate date(today).
1.PNG


the result as I highlighted
ase.xlsm
ABCD
1DATEACCOUNT NAMEDEBIT CREDIT
201/03/2023CASH BALANCE200,000.00
303/03/2023CASH PR 2,000.00
403/03/2023CASH PR 15,000.00
509/03/2023CASH PR 500.00
613/03/2023CASH PR 10,000.00
713/03/2023CASH PR 15,000.00
814/03/2023CASH PR 500.00
919/03/2023CASH PR 500.00
1019/03/2023CASH PR 2,000.00
1117/06/2023CASH PR 12,000.00
1205/03/2023CASH DM2,000.00
1310/03/2023CASH DM500.00
1415/03/2023CASH DM5,000.00
1515/03/2023CASH DM2,000.00
1615/03/2023CASH DM10,000.00
1718/03/2023CASH DM25,000.00
1819/03/2023CASH DM2,000.00
1921/03/2023EXPENSE PR15,000.00
2021/03/2023EXPENSE PR20,000.00
2123/03/2023EXPENSE PR2,000.00
2224/03/2023SALES200000
2326/03/2023SALES1000
2425/03/2023PURCHASE10000
DAILY
 

Excel Facts

Last used cell?
Press Ctrl+End to move to what Excel thinks is the last used cell.
Don't mention what to do if the account name doesn't exist.

With the following macro it sends you a msg that the account does not exist.
If you want to add the record, it will add it to the end of the data, just remove this line: "Exit Sub" from the macro.

Note: Check your data, some account names have spaces to the right. If you don't correct it, the macro won't find them.

VBA Code:
Private Sub CommandButton1_Click()
  Dim f As Range
  Dim sh As Worksheet
  Dim lr As Long
  
  Set sh = Sheets("DAILY")
  
  If ComboBox1.ListIndex = -1 Or (TextBox1.Value = "" And TextBox2.Value = "") Then
    MsgBox "You must complete the data."
    Exit Sub
  End If
  
  Set f = sh.Range("B:B").Find(ComboBox1.Value, , xlValues, xlWhole, xlByRows, xlPrevious, False)
  If f Is Nothing Then
    MsgBox "ACCOUNT NAME not exists"
    lr = sh.Range("B" & Rows.Count).End(3).Row + 1
    Exit Sub
  Else
    lr = f.Row + 1
    sh.Rows(lr).EntireRow.Insert
  End If
  
  sh.Range("A2:D2").Copy
  sh.Range("A" & lr).PasteSpecial xlPasteFormats
  sh.Range("C2").Copy
  sh.Range("D" & lr).PasteSpecial xlPasteFormats
  Application.CutCopyMode = False
  sh.Range("D" & lr).Font.Color = vbBlack
  sh.Range("A" & lr).Value = Date
  sh.Range("B" & lr).Value = ComboBox1.Value
  sh.Range("C" & lr).Value = TextBox1.Value
  sh.Range("D" & lr).Value = TextBox2.Value
End Sub


--------------
Let me know the result and I'll get back to you as soon as I can.
Cordially
Dante Amor
--------------​
 
Upvote 1
Solution
Don't mention what to do if the account name doesn't exist.
well the items in combobox depends on what is existed in column B , so just I select not write in combobox. but thanks for this notice .
Note: Check your data, some account names have spaces to the right. If you don't correct it, the macro won't find them.
understood;)
thank you so much for your code .(y)
 
Upvote 0

Forum statistics

Threads
1,224,818
Messages
6,181,152
Members
453,021
Latest member
Justyna P

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