简体   繁体   中英

Insert row based on value in ActiveX combobox

In column B, I have a list of company's. I have a combobox called ComboBox1 at the top of the sheet and also a button called InsertContact1. The combobox pulls all the company's from column B and what I want to do is if the user selects Company D from the drop down and hits the insert contact button, it will find the company in column B and insert a row below - which will allow the user to enter a contact for that company in another column (manually at the moment).

Here is my code so far... I'm not too sure where I am going wrong but I am getting an error that says "Argument is not optional".

This is the code I have so far in Sheet1 (Database):

Public Function ContactAdd(SearchedCompany As String) As Long
Dim cF As Range

With Worksheets("Database").Range("B:B")
Set cF = .Find(What:=Worksheets("Database").ComboBox1, After:=.Cells(1, 1), LookIn:=xlValues, LookAt:=xlWhole, MatchCase:=False)

If Not cF Is Nothing Then
    cF.Offset(1, 0).EntireRow.Insert
    ContactAdd = cF.Offset(1, 0).Row
    Exit Function
End If
End With

ContactAdd = 0
End Function

Private Sub InsertContact1_Click()
ContactAdd
End Sub

The private sub is for the button to call the function, however this is what keeps getting an error. Where am I going wrong?

At the moment your ContactAdd function takes a string argument, and you are calling it without passing a string.

Since you are not using the string argument of the function, you can remove the SearchedCompany Argument from the function definition, and call it like you do at the moment:

Public Function ContactAdd() As Long

Otherwise you can pass an argument when you call the function:

Private Sub InsertContact1_Click()

    ContactAdd ("some string")

End Sub

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM