Add ComboBox in an Interactive UserForm in VBA - excel

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

Related

Search option listing results in listbox without duplicates and populating values to textboxes and checkboxes on doubleclick

I'm new to VBA and what I have so far is mashup from various tutorials and websearches. But so far it works how I want. Now I'd like to add search option and after hours of websearch I cannot find right solution.
Basically I'm trying to create userform which shows data from sheet in listbox, when I double-click item on listbox it shows values of defined cell in textboxes and checkboxes. Got that. I'd like to add search option to list results in same cleared listbox without duplicates (if searched value appears multiple times in same row list it only once in listbox) and when I double-click on an item in listbox it will show details of that record in userform textboxes with checkbox values. All works fine with CheckBox1 changing colours aswell.
What I tried so far.
My listbox populates with all data and on double click shows everything properly.
Sub All_Data()
Dim sh As Worksheet
Set sh = ThisWorkbook.Sheets("TABLE")
Dim last_Row As Long
last_Row = Application.WorksheetFunction.CountA(sh.Range("A:A"))
With Me.ListBox1
.ColumnHeads = False
.ColumnCount = 12
.ColumnWidths = "0,70,60,60,0,0,0,0,0,0,120,0"
.List = Range(Cells(1, 1), Cells(last_Row, .ColumnCount)).Value
.RemoveItem 0
End With
End Sub
Private Sub ListBox1_DblClick(ByVal Cancel As MSForms.ReturnBoolean)
Me.TextBox1.Value = Me.ListBox1.List(Me.ListBox1.ListIndex, 0)
Me.TextBox2.Value = Me.ListBox1.List(Me.ListBox1.ListIndex, 1)
Me.TextBox3.Value = Me.ListBox1.List(Me.ListBox1.ListIndex, 2)
Me.TextBox4.Value = Me.ListBox1.List(Me.ListBox1.ListIndex, 3)
Me.TextBox5.Value = Me.ListBox1.List(Me.ListBox1.ListIndex, 4)
Me.TextBox6.Value = Me.ListBox1.List(Me.ListBox1.ListIndex, 5)
Me.TextBox7.Value = Me.ListBox1.List(Me.ListBox1.ListIndex, 6)
Me.ComboBox1.Value = Me.ListBox1.List(Me.ListBox1.ListIndex, 7)
Me.Checkbox1.Value = Me.ListBox1.List(Me.ListBox1.ListIndex, 8)
Me.Checkbox2.Value = Me.ListBox1.List(Me.ListBox1.ListIndex, 9)
Me.CheckBox3.Value = Me.ListBox1.List(Me.ListBox1.ListIndex, 10)
Me.CheckBox4.Value = Me.ListBox1.List(Me.ListBox1.ListIndex, 11)
End Sub
Private Sub CheckBox1_Change()
If CheckBox1.Value Then
CheckBox1.ForeColor = &H8000&
Else
CheckBox1.ForeColor = &HC0&
End If
End Sub
Private Sub Workbook_Open()
UserForm1.Show
End Sub
Checkbox values are changing properly from red to green and vice versa.
cbgreen cbred
Then I tried to add search option and it kind of works, but checkbox values being greyed out despite showing right true value (but still works with all data listed).
cbgrey
Private Sub searchButton_Click()
Dim sh As Worksheet
Set sh = ThisWorkbook.Sheets("TABLE")
Dim i As Long
Dim x As Long
Dim p As Integer, k As Integer
Me.searchTextBox = LCase(Me.searchTextBox)
If Me.searchTextBox = "" Then
Call All_Data
Exit Sub
End If
With Me.ListBox1
.Clear
.ColumnHeads = False
.ColumnCount = 12
.ColumnWidths = "0,70,60,60,0,,0,0,0,0,120,0"
For i = 2 To sh.Range("A" & Rows.Count).End(xlUp).Row
For x = 1 To Len(sh.Cells(i, 2))
p = Me.searchTextBox.TextLength
For k = 2 To .ColumnCount - 4
If LCase(Mid(sh.Cells(i, k), x, p)) = Me.searchTextBox And Me.searchTextBox <> "" Then
.AddItem
.List(.ListCount - 1, 0) = sh.Cells(i, 1).Value
.List(.ListCount - 1, 1) = sh.Cells(i, 2).Value
.List(.ListCount - 1, 2) = sh.Cells(i, 3).Value
.List(.ListCount - 1, 3) = sh.Cells(i, 4).Value
.List(.ListCount - 1, 4) = sh.Cells(i, 5).Value
.List(.ListCount - 1, 5) = sh.Cells(i, 6).Value
.List(.ListCount - 1, 6) = sh.Cells(i, 7).Value
.List(.ListCount - 1, 7) = sh.Cells(i, 8).Value
.List(.ListCount - 1, 8) = sh.Cells(i, 9).Value
.List(.ListCount - 1, 9) = sh.Cells(i, 10).Value
.List(.ListCount - 1, 10) = sh.Cells(i, 11).Value
.List(.ListCount - 1, 11) = sh.Cells(i, 12).Value
End If
Next k
Next x
Next i
RemoveDuplicates Listbox1
End With
End Sub
Sub RemoveDuplicates(aListBox As MSForms.ListBox)
Dim i As Long, j As Long
With aListBox
For i = .ListCount - 1 To 1 Step -1
For j = 0 To i - 1
If (.List(i, 0) = .List(j, 0)) Or (.List(i, 0) = vbNullString) Then
.RemoveItem i
Exit For
End If
Next j
Next i
End With
End Sub
Is there another way to search, list results in listbox without duplicates and on double-click to populate values to textboxes and ComboBox1 not greyed out (changing colour)?
Thank you for any suggestions and help.
Avoid the duplicates by exiting the column loop at the first match.
Option Explicit
Const COLWIDTHS = "0,70,60,60,0,,0,0,0,0,120,0"
Private Sub searchButton_Click()
Dim sh As Worksheet, data
Dim s As String, lastrow As Long
Dim i As Long, j As Long, k As Long, n As Long
s = Trim(LCase(Me.searchTextBox)) ' search term
If Len(s) = 0 Then
Call All_Data
Exit Sub
End If
With Me.ListBox1
.Clear
.ColumnHeads = False
.ColumnCount = 12
.ColumnWidths = COLWIDTHS
End With
Set sh = ThisWorkbook.Sheets("TABLE")
With sh
lastrow = .Cells(.Rows.Count, "A").End(xlUp).Row
' use array to speed up search
data = .Range("A1:L" & lastrow).Value2 ' 12 columns
End With
'search
n = 0 ' list index
s = "*" & s & "*" ' like
For i = 2 To lastrow
For j = 2 To 8 ' search col B to H
If LCase(data(i, j)) Like s Then
With Me.ListBox1
.AddItem
For k = 1 To 12 ' Col A to L
.List(n, k - 1) = data(i, k)
Next
n = n + 1
End With
Exit For
End If
Next
Next
End Sub
Private Sub ListBox1_DblClick(ByVal Cancel As MSForms.ReturnBoolean)
Dim n As Long, i As Long, c As Control
With Me.ListBox1
i = .ListIndex
If i < 0 Then Exit Sub
For n = 1 To 7 ' A to G
Set c = Me.Controls("TextBox" & n)
c.Value = .List(i, n - 1)
Next
Me.ComboBox1.Value = .List(i, 7) ' col H
For n = 9 To 12 ' col I - L
Set c = Me.Controls("CheckBox" & n - 8)
If .List(i, n - 1) = 1 Then
c.Value = True
c.ForeColor = &H8000&
Else
c.Value = False
c.ForeColor = &HC0&
End If
Next
End With
End Sub
Sub All_Data()
Dim sh As Worksheet, last_Row As Long
Set sh = ThisWorkbook.Sheets("TABLE")
With sh
last_Row = .Cells(.Rows.Count, "A").End(xlUp).Row
End With
With Me.ListBox1
.ColumnHeads = False
.ColumnCount = 12
.ColumnWidths = COLWIDTHS
.List = sh.Range(sh.Cells(2, 1), sh.Cells(last_Row, .ColumnCount)).Value
End With
End Sub
' repeat for checkboxes 2,3 4
Private Sub CheckBox1_Change()
If CheckBox1.Value Then
CheckBox1.ForeColor = &H8000&
Else
CheckBox1.ForeColor = &HC0&
End If
End Sub
Private Sub Workbook_Open()
UserForm1.Show
End Sub
Private Sub UserForm_Activate()
All_Data
End Sub

How can I edit/update an existing record in my database using a listbox in Excel userform?

I am creating an Excel userform in which users can add, search, and update records. I was able to create a button command that searches the database (a single sheet in my workbook) and populates a listbox with the search results. Because my database has more than 10 columns which I wanted to be visible in the listbox, I used an array to populate the listbox rather than AddItem which limited me to 10 or fewer columns. (the search code is below)
Private Sub Search_Click()
''''''''''''Validation
If Trim(SearchTextBox.Value) = "" And Me.Visible Then
MsgBox "Please enter a search value.", vbCritical, "Error"
Exit Sub
End If
ReDim arrs(0 To 17, 1 To 1)
With Worksheets("Sheet1")
ListBox.Clear
ListBox.ColumnCount = 18
ListBox.ColumnHeads = True
ListBox.Font.Size = 10
ListBox.ColumnWidths = "80,80,150,130,90,90,80,80,80,80,80,60,70,150,150,150,150,180"
If .FilterMode Then .ShowAllData
Set k = .Range("K2:K" & Cells(Rows.Count, "K").End(xlUp).Row).Find(What:="*" & SearchTextBox.Text & "*", LookIn:=xlValues, lookat:=xlWhole)
If Not k Is Nothing Then
adrs = k.Address
Do
M = M + 1
ReDim Preserve arrs(0 To 17, 1 To M)
For j = 0 To 17
arrs(j, M) = .Cells(k.Row, j + 1).Value
Next j
Set k = .Range("K2:K" & Cells(Rows.Count, "K").End(xlUp).Row).FindNext(k)
Loop While Not k Is Nothing And k.Address <> adrs
ListBox.Column = arrs
Else
' If you get here, no matches were found
MsgBox "No matches were found based on the search criteria.", vbInformation
End If
End With
End Sub
I also added code so that when I double click on a record in the listbox, it populates the corresponding textbox in the userform.
Private Sub ListBox_DblClick(ByVal Cancel As MSForms.ReturnBoolean)
TextBox1.Text = ListBox.Column(0)
If TextBox1.Text = ListBox.Column(0) Then
TextBox1.Text = ListBox.Column(0)
TextBox2.Text = ListBox.Column(1)
TextBox3.Text = ListBox.Column(2)
TextBox4.Text = ListBox.Column(3)
TextBox5.Text = ListBox.Column(4)
TextBox6.Text = ListBox.Column(5)
TextBox7.Text = ListBox.Column(6)
TextBox8.Text = ListBox.Column(7)
TextBox9.Text = ListBox.Column(8)
TextBox10.Text = ListBox.Column(9)
TextBox11.Text = ListBox.Column(10)
TextBox12.Text = ListBox.Column(11)
TextBox13.Text = ListBox.Column(12)
TextBox14.Text = ListBox.Column(13)
TextBox15.Text = ListBox.Column(14)
TextBox16.Text = ListBox.Column(15)
TextBox17.Text = ListBox.Column(16)
TextBox18.Text = ListBox.Column(17)
End If
End Sub
After double clicking on a search result from the listbox, I want users to be able to edit any information in those textboxes and click a command button to update that entry/record in the database itself. However, I am having some problems with creating this function. I used the following code, and although it doesn't return an error message, it doesn't change the entry in the database.
Dim X As Long
Dim Y As Long
X = Sheets("Sheet1").Range("A" & Rows.Count).End(xlUp).Row
For Y = 2 To X
If Sheets("Sheet1").Cells(Y, 11).Value = SearchTextBox.Text Then
Sheets("Sheet1").Cells(Y, 1).Value = TextBox1
Sheets("Sheet1").Cells(Y, 2).Value = TextBox2
Sheets("Sheet1").Cells(Y, 3).Value = TextBox3
Sheets("Sheet1").Cells(Y, 4).Value = TextBox4
Sheets("Sheet1").Cells(Y, 5).Value = TextBox5
Sheets("Sheet1").Cells(Y, 6).Value = TextBox6
Sheets("Sheet1").Cells(Y, 7).Value = TextBox7
Sheets("Sheet1").Cells(Y, 8).Value = TextBox8
Sheets("Sheet1").Cells(Y, 9).Value = TextBox9
Sheets("Sheet1").Cells(Y, 10).Value = TextBox10
Sheets("Sheet1").Cells(Y, 11).Value = TextBox11
Sheets("Sheet1").Cells(Y, 12).Value = TextBox12
Sheets("Sheet1").Cells(Y, 13).Value = TextBox13
Sheets("Sheet1").Cells(Y, 14).Value = TextBox14
Sheets("Sheet1").Cells(Y, 15).Value = TextBox15
Sheets("Sheet1").Cells(Y, 16).Value = TextBox16
Sheets("Sheet1").Cells(Y, 17).Value = TextBox17
Sheets("Sheet1").Cells(Y, 18).Value = TextBox18
End If
Next Y
Additionally, the term that I am searching with is not unique, so there are multiple records/rows in the database with the same search term. How can I create this code in a way that I when I click on the update button, information from the userform (which has been populated by doubleclicking the record in the listbox) is updated in the excel sheet but not for all records with the same search term?
Thank you so much for any help!
Add a Label to your UserForm to hold the row number from where the text box values came. Use the first column (width zero so hidden) of the listbox to hold the row number of the filtered rows. Set the label to column 0 of the double clicked row.
Option Explicit
Private Sub Update_Click()
Dim r As Long, n As Long
' record showing
r = Val(Label1.Caption)
If r < 1 Then
Exit Sub
End If
With Sheets("Sheet1")
For n = 1 To 18
.Cells(r, n).Value2 = Me.Controls("TextBox" & n)
Next
End With
End Sub
Private Sub ListBox_DblClick(ByVal Cancel As MSForms.ReturnBoolean)
Dim n As Long
With ListBox
For n = 1 To ListBox.ColumnCount - 1
Debug.Print n, .Column(n)
Me.Controls("TextBox" & n).Text = .Column(n)
Next
Label1.Caption = .Column(0)
End With
End Sub
Private Sub Search_Click()
Const COLS = 18
Dim s
s = Trim(SearchTextBox.Value)
If s = "" And Me.Visible Then
MsgBox "Please enter a search value.", vbCritical, "Error"
Exit Sub
Else
s = "*" & s & "*"
End If
Dim rngFnd As Range, rngSearch As Range, first As String
Dim arr, lastrow As Long, i As Long, j As Long
' search sheet
With Worksheets("Sheet1")
If .FilterMode Then .ShowAllData
lastrow = .Cells(.Rows.Count, "K").End(xlUp).Row
Set rngSearch = .Range("K1:K" & lastrow)
i = WorksheetFunction.CountIf(rngSearch, s)
If i > 0 Then
ReDim arr(0 To COLS, 1 To i)
Set rngFnd = rngSearch.Find(What:=s, LookIn:=xlValues, lookat:=xlWhole)
If Not rngFnd Is Nothing Then
i = 0
first = rngFnd.Address
Do
i = i + 1
arr(0, i) = rngFnd.Row
For j = 1 To COLS
arr(j, i) = .Cells(rngFnd.Row, j).Value
Next j
Set rngFnd = rngSearch.FindNext(rngFnd)
Loop While rngFnd.Address <> first
End If
Else
'If you get here, no matches were found
MsgBox "No matches were found based on the search criteria. " & s, vbExclamation
Exit Sub
End If
End With
' format listbox
With ListBox
.Clear
.ColumnCount = COLS + 1
.ColumnHeads = True
.Font.Size = 10
.ColumnWidths = "0,80,80,150,130,90,90,80,80,80,80,80,60,70,150,150,150,150,180"
.Column = arr
End With
End Sub

Using text values in Combobox

I would like to also use text values to populate textbox one to four right now only number values will fetch data in the textbox, I know I am overlooking a pretty simple thing but can't seem to find out what?
Private Sub ComboBox1_Change()
Dim i As Long, LastRow As Long, ws As Worksheet
Set ws = Sheets("Sheet1")
LastRow = ws.Range("A" & Rows.Count).End(xlUp).Row
For i = 2 To LastRow
If Val(Me.ComboBox1.Value) = ws.Cells(i, "A") Then
MsgBox Me.ComboBox1.Value
Me.TextBox1 = ws.Cells(i, "B").Value
Me.TextBox2 = ws.Cells(i, "C").Value
Me.TextBox3 = ws.Cells(i, "D").Value
Me.TextBox4 = ws.Cells(i, "E").Value
End If
Next i
End Sub
I fixed it myself with the possibility to reverse it.
FYI: sheet is renamed to "Control"
Private Sub UserForm_Initialize()
With Sheets("Control")
Me.ComboBox1.List = .Range("a2", .Range("a" & Rows.Count).End(xlUp)).Value
End With
End Sub
Private Sub ComboBox1_Change()
Dim i As Long
For i = 1 To 4
Me("textbox" & i).Value = ""
Next
If Me.ComboBox1.ListIndex = -1 Then Exit Sub
With Sheets("control")
For i = 1 To 4
Me("textbox" & i).Value = .Cells(Me.ComboBox1.ListIndex + 2, i + 1).Value
Next
End With
End Sub
Private Sub CommandButton2_Click()
If Me.ComboBox1.ListIndex = -1 Then Exit Sub
Sheets("control").Cells(Me.ComboBox1.ListIndex + 2, "b").Resize(, 4).Value = _
Array(Me.TextBox1.Value, Me.TextBox2.Value, Me.TextBox3.Value, Me.TextBox4.Value)
End Sub

How to recalculate prices in ListBox1 after deleting

I have created a possapp for my bar. Everything works fine but i have to recalculate the listbox when delete 1 or more items.
This is my code for the delete button
Private Sub CommandButton84_Click()
Dim ItemTarget&, s, i%
s = 0
ItemTarget = ListBox1.ListCount
If ItemTarget > 0 Then
Me.ListBox1.RemoveItem ItemTarget - 1
For i = 0 To Me.ListBox1.ListCount - 1
s = s + Val(Me.ListBox1.List(i, 1))
Next
Me.TextBox1 = s
Else
MsgBox "De lijst is reeds leeg", vbInformation, "Café De Zoete Inval"
End If
Me.TextBox4 = Me.ListBox1.ListCount
End Sub
Private Sub CommandButton100_Click()
Dim LItem As Long
Dim IRange As Integer
Dim sht As Worksheet
Dim LastRow As Long
Dim rows As Integer
rows = 0
Set sht = ActiveSheet
For LItem = 0 To ListBox1.ListCount - 1
ListBox1.ColumnCount = 2
With Worksheets("Sheet6")
.Cells(LItem + 7, 1) = ListBox1.List(LItem, 0)
.Cells(LItem + 7, 2) = ListBox1.List(LItem, 1)
.Cells(LItem + 8, 1).EntireRow.Insert
rows = rows + 1
End With
With Sheets("Histo")
LastRow = .Cells.Find("*", searchorder:=xlByRows,
searchdirection:=xlPrevious).Row
If Time < "07:00:00" Then
.Cells(LastRow + 1, 1) = Format(Date - 1, "dd-mm-yyyy")
Else
.Cells(LastRow + 1, 1) = Date
End If
.Cells(LastRow + 1, 2) = ListBox1.List(LItem, 0)
.Cells(LastRow + 1, 3) = ListBox1.List(LItem, 1)
End With
Next LItem
With ThisWorkbook.Sheets("Sheet6")
ListBox1.Clear
TextBox2.Value = ""
TextBox1.Value = 0
Range("Sheet6!B5").ClearContents
For i = 1 To rows
.Cells(7, 1).EntireRow.Delete
Next
End With
ActiveWorkbook.Save
End Sub
Private Sub CommandButton1_Click()
With ThisWorkbook.Sheets("Sheet1")
Me.ListBox1.AddItem .Range("B2").Value
Me.ListBox1.Column(1, ListBox1.ListCount - 1) = Format(Val(.Range("C2").Value),
"€#,##0.00")
Me.TextBox1.Value = CDbl(Me.TextBox1.Value) + .Range("C2").Value
Me.TextBox1.Value = Format(Me.TextBox1.Value, "#,##0.00")
End With
Me.TextBox4 = Me.ListBox1.ListCount
End Sub

I am getting a error because my value is Text and it is looking for a Numeric Value how do I fix this?

I am not a VBA programmer and I need help.
In my spreadsheet my Location (ComboBox1) is text and I can not change it to be numeric. How do I fix it so it will use the Text. I guess I need to use the IsText function but I can not figure out how.
My code is pasted below. The first line after GetData() is where it stops.
Dim Loc As Integer, i As Integer, j As Integer, flag As Boolean
Sub GetData()
If IsNumeric(UserForm1.ComboBox1.Value) Then
flag = False
i = 0
id = UserForm1.ComboBox1.Value
Do While Cells(i + 1, 1).Value <> ""
If Cells(i + 1, 1).Value = id Then
flag = True
For j = 2 To 3
UserForm1.Controls("TextBox" & j).Value = Cells(i + 1, j).Value
Next j
End If
i = i + 1
Loop
If flag = False Then
For j = 2 To 3
UserForm1.Controls("TextBox" & j).Value = ""
Next j
End If
Else
ClearForm
End If
End Sub
Sub ClearForm()
For j = 1 To 3
UserForm1.Controls("TextBox" & j).Value = ""
Next j
End Sub
Sub EditAdd()
Dim emptyRow As Long
If UserForm1.ComboBox1.Value <> "" Then
flag = False
i = 0
id = UserForm1.ComboBox1.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 3
Cells(i + 1, j).Value = UserForm1.Controls("TextBox" & j).Value
Next j
End If
i = i + 1
Loop
If flag = False Then
For j = 1 To 3
Cells(emptyRow, j).Value = UserForm1.Controls("TextBox" & j).Value
Next j
End If
End If
End Sub

Resources