Insert two columns by select item from combobox on userform

Omar M

New Member
Joined
Jan 11, 2024
Messages
46
Office Version
  1. 2019
Platform
  1. Windows
Hi experts,
I would insert two columns for new account name with the same formatting and borders.
here is original data in sheet before add item in combobox

AC
ABCDEFGHIJKLMNOPQRSTUVWXYZAAABACAD
1DATECONSTRAIN NODESCRIPTIONBALANCING CASETOTALBANKSAFEPURCHASESELLINGCAPITALPURCHASE RETURNINGSELLING RETURNINGRECEIVABLECREDITOR
2DEBITCREDITDEBITCREDITDEBITCREDITDEBITCREDITDEBITCREDITDEBITCREDITDEBITCREDITDEBITCREDITDEBITCREDITDEBITCREDIT
311/08/20241DEPOSIT CAPITALBALANCED2000200020002000
411/08/20242BUYING GOODSBALANCED35003500150020003500
511/08/20243SELLING GOODSNOT BALANCED10000150001000015000
611/08/20244SELLING GOODSBALANCED3000300030003000
711/08/20245INCREASE CAPITALBALANCED15000150001500015000
812/08/20246DECREASE CAPITALBALANCED3000300030003000
Sheet1


add new account name from combobox1
CM.PNG



the result will be in columns Y,Z

AC
ABCDEFGHIJKLMNOPQRSTUVWXYZAAABACAD
1DATECONSTRAIN NODESCRIPTIONBALANCING CASETOTALBANKSAFEPURCHASESELLINGCAPITALPURCHASE RETURNINGSELLING RETURNINGRECEIVABLECREDITOREXPENSES
2DEBITCREDITDEBITCREDITDEBITCREDITDEBITCREDITDEBITCREDITDEBITCREDITDEBITCREDITDEBITCREDITDEBITCREDITDEBITCREDITDEBITCREDIT
311/08/20241DEPOSIT CAPITALBALANCED2000200020002000
411/08/20242BUYING GOODSBALANCED35003500150020003500
511/08/20243SELLING GOODSNOT BALANCED10000150001000015000
611/08/20244SELLING GOODSBALANCED3000300030003000
711/08/20245INCREASE CAPITALBALANCED15000150001500015000
812/08/20246DECREASE CAPITALBALANCED3000300030003000
Sheet1


but if I select the same account to try adding again and it's already existed , then should shows message" the account name is already existed, you can't repeat it" , and don't add it.
every time add new account name as long I need it.
thanks
 

Excel Facts

Workdays for a market open Mon, Wed, Friday?
Yes! Use "0101011" for the weekend argument in NETWORKDAYS.INTL or WORKDAY.INTL. The 7 digits start on Monday. 1 means it is a weekend.
Try this:
VBA Code:
Private Sub CommandButton1_Click()
  Dim f As Range
  Dim lc As Long
  
  With ComboBox1
    If .Value = "" Then
      MsgBox "Enter Account name"
      Exit Sub
    End If
    
    Set f = Range("1:1").Find(.Value, , xlValues, xlWhole, , , False)
    If Not f Is Nothing Then
      MsgBox "The account name is already existed"
    Else
      lc = Cells(2, Columns.Count).End(1).Column + 1
      Cells(1, lc).Value = .Value
      Cells(2, lc).Value = "DEBIT"
      Cells(2, lc + 1).Value = "CREDIT"
      Range("E1:F2").Copy
      Cells(1, lc).PasteSpecial xlPasteFormats
    End If
  End With
End Sub
 
Upvote 0
Perfect !
just I want fixing borders from row 3 , it doesn't make borders from row3 until last row contains date in column A.
may you do it ,please?
 
Upvote 0
it doesn't make borders from row3 until last row contains date in column A.
may you do it ,please?
Try:
VBA Code:
Private Sub CommandButton1_Click()
  Dim f As Range
  Dim lc As Long
  
  Application.ScreenUpdating = False
  With ComboBox1
    If .Value = "" Then
      MsgBox "Enter Account name"
      Exit Sub
    End If
    
    Set f = Range("1:1").Find(.Value, , xlValues, xlWhole, , , False)
    If Not f Is Nothing Then
      MsgBox "The account name is already existed"
    Else
      lc = Cells(2, Columns.Count).End(1).Column + 1
      Cells(1, lc).Value = .Value
      Cells(2, lc).Value = "DEBIT"
      Cells(2, lc + 1).Value = "CREDIT"
      Range("E1:F2").Copy
      Cells(1, lc).PasteSpecial xlPasteFormats
      With Cells(3, lc).Resize(Range("A" & Rows.Count).End(3).Row - 2, 2).Borders
        .LineStyle = xlContinuous
        .Color = Range("A1").Borders.Color
      End With
    End If
  End With
  Application.ScreenUpdating = True
End Sub

😇
 
Upvote 0
Solution

Forum statistics

Threads
1,221,418
Messages
6,159,791
Members
451,589
Latest member
Harold14

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