I am using this tutorial (https://www.excel-easy.com/vba/examples/interactive-userform.html) for how to create an Interactive Userform that overwrites values based on a simple condition that if ID exists, then update or edit the rows.
However, this works very well for TextBox but I am struggling to add other controls from the Toolbox. Currently, I am trying to add ComboBox in the loops such that it can add values from the ComboBox.
Private Sub ComboBox1_Change()
End Sub
Private Sub CommandButton1_Click()
EditAdd
End Sub
Private Sub CommandButton2_Click()
ClearForm
End Sub
Private Sub CommandButton3_Click()
Unload Me
End Sub
Private Sub TextBox1_Change()
GetData
End Sub
Private Sub UserForm_Click()
End Sub
Private Sub UserForm_Initialize()
TextBox1.SetFocus
ComboBox1.AddItem "One"
ComboBox1.AddItem "Two"
ComboBox1.AddItem "Three"
ComboBox1.AddItem "Four"
ComboBox1.AddItem "Five"
End Sub
Here is the module. I've tried to modify it by adding UserForm.Controls("ComboBox" & j).Value = Cells(i + 1, j).Value
in the for
loops, but I only get errors.
Dim id As Integer, i As Integer, j As Integer, flag As Boolean
Sub GetData()
If IsNumeric(UserForm.TextBox1.Value) Then
flag = False
i = 0
id = UserForm.TextBox1.Value
Do While Cells(i + 1, 1).Value <> ""
If Cells(i + 1, 1).Value = id Then
flag = True
For j = 2 To 6
UserForm.Controls("TextBox" & j).Value = Cells(i + 1, j).Value
UserForm.Controls("ComboBox" & j).Value = Cells(i + 1, j).Value
Next j
End If
i = i + 1
Loop
If flag = False Then
For j = 2 To 6
UserForm.Controls("TextBox" & j).Value = ""
UserForm.Controls("ComboBox" & j).Value = ""
Next j
End If
Else
ClearForm
End If
End Sub
Sub ClearForm()
For j = 1 To 6
UserForm.Controls("TextBox" & j).Value = ""
UserForm.Controls("ComboBox" & j).Value = ""
Next j
End Sub
Sub EditAdd()
Dim emptyRow As Long
If UserForm.TextBox1.Value <> "" Then
flag = False
i = 0
id = UserForm.TextBox1.Value
emptyRow = WorksheetFunction.CountA(Range("A:A")) + 1
Do While Cells(i + 1, 1).Value <> ""
If Cells(i + 1, 1).Value = id Then
flag = True
For j = 2 To 6
Cells(i + 1, j).Value = UserForm.Controls("TextBox" & j).Value
Cells(i + 1, j).Value = UserForm.Controls("ComboBox" & j).Value
Next j
End If
i = i + 1
Loop
If flag = False Then
For j = 1 To 6
Cells(emptyRow, j).Value = UserForm.Controls("TextBox" & j).Value
Cells(emptyRow, j).Value = UserForm.Controls("ComboBox" & j).Value
Next j
End If
End If
End Sub
How can I add ComboBox into my module such that the Userform overwrites existing values if ID exists?
I can't provide a full answer because I don't understand enough about what you are trying to achieve - hence the various comments and queries in the code. Perhaps it gives you enough to work out what you have to do.
Dim id As Integer, i As Integer, j As Integer, flag As Boolean
Sub GetData()
If IsNumeric(UserForm.TextBox1.Value) Then 'separate check required for combobox?
flag = False
i = 0
id = UserForm.TextBox1.Value
Do While Cells(i + 1, 1).Value <> ""
If UserForm.ComboBox1.Value = Cells(i + 1, j).Value Then 'not sure if this check is right and the j needs to be replaced with something
'do something
'should this be a separate flag?
End If
If Cells(i + 1, 1).Value = id Then
flag = True
For j = 2 To 6
UserForm.Controls("TextBox" & j).Value = Cells(i + 1, j).Value
Next j
End If
i = i + 1
Loop
If flag = False Then 'might need changing if separate flags required
UserForm.ComboBox1.Value = ""
For j = 2 To 6
UserForm.Controls("TextBox" & j).Value = ""
Next j
End If
Else
ClearForm
End If
End Sub