Add value into userform Combobox & also on worksheet Table

ipbr21054

Well-known Member
Joined
Nov 16, 2010
Messages
5,668
Office Version
  1. 2007
Platform
  1. Windows
Morning.
I have a worksheet called INV
I open the userform shown below which then allows the user to select the drop down & select a value.
In this example, ComboBox1 is what we need to look at, its the one labelled "ENTER KEY TYPE BELOW"
The value in question isnt at present in the drop down list.
However the value is shown in TextBox3 labeled "VALUE FROM WORKSHEET"

The code will be added to CommanButton1 "COPY & ADD NEW VALUE"

It should work like this,
Copy the value in TextBox3 to ComboBox1
Also add this value to my worksheet Table
The sheet which has the table is called INFO
It is Table38 =MCKEYS
Thats it.

Next time the user opens ComboBox1 this new value will be present

Currently table is Row 1 Header & Values are Row 2 - 12
 

Attachments

  • EaseUS_2024_10_ 3_10_02_59.jpg
    EaseUS_2024_10_ 3_10_02_59.jpg
    42 KB · Views: 10

Excel Facts

Best way to learn Power Query?
Read M is for (Data) Monkey book by Ken Puls and Miguel Escobar. It is the complete guide to Power Query.
Hi @ipbr21054

I added a couple of validations, you didn't ask for it, but that way the solution is more elegant.

VBA Code:
Private Sub CommandButton1_Click()
  Dim tbl As ListObject
  Dim newRow As ListRow
  Dim f As Range
  
  Set tbl = Sheets("INFO").ListObjects("Table38")
  
  If TextBox3.Value = "" Then
    MsgBox "Enter new value"
    Exit Sub
  End If
  
  Set f = tbl.Range.Find(TextBox3.Value, , xlValues, xlWhole, , , False)
  If Not f Is Nothing Then
    MsgBox "The value already exists"
    Exit Sub
  End If
  
  Set newRow = tbl.ListRows.Add
  newRow.Range(1) = TextBox3.Value
  
  ComboBox1.List = tbl.Range.Value
End Sub

😇
 
Upvote 0
Hi,
I run that code but i get a message Run time Error 70, Permission Denied.
When i debug it i see this line below in Yellow.
VBA Code:
 ComboBox1.List = tbl.Range.Value

But then when i go to check the Table the value is added ?

See photos
 

Attachments

  • EaseUS_2024_10_ 5_12_39_39.jpg
    EaseUS_2024_10_ 5_12_39_39.jpg
    59.9 KB · Views: 1
  • EaseUS_2024_10_ 5_12_41_52.jpg
    EaseUS_2024_10_ 5_12_41_52.jpg
    27.7 KB · Views: 1
Upvote 0
I run that code but i get a message Run time Error 70, Permission Denied.
You are probably loading combobox1 with the "Rowsource" property.
In the future, try to put all the surroundings you have here. All your code, the properties you are using in each control, textbox, combobox, etc. Since we can't guess what you have.
🧙‍♂️

Then try:

VBA Code:
Private Sub CommandButton1_Click()
  Dim tbl As ListObject
  Dim newRow As ListRow
  Dim f As Range
  
  Set tbl = Sheets("INFO").ListObjects("Table38")
  
  If TextBox3.Value = "" Then
    MsgBox "Enter new value"
    Exit Sub
  End If
  
  Set f = tbl.Range.Find(TextBox3.Value, , xlValues, xlWhole, , , False)
  If Not f Is Nothing Then
    MsgBox "The value already exists"
    Exit Sub
  End If
  
  Set newRow = tbl.ListRows.Add
  newRow.Range(1) = TextBox3.Value
  ComboBox1.RowSource = ""
  ComboBox1.Clear
  ComboBox1.List = tbl.Range.Value
End Sub
 
Upvote 0
Solution

Forum statistics

Threads
1,222,903
Messages
6,168,939
Members
452,227
Latest member
sam1121

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