floggingmolly
Board Regular
- Joined
- Sep 14, 2019
- Messages
- 167
- Office Version
- 365
- Platform
- Windows
I have a userform and when the user selects a cell, say A3, the form populates with the data in that row. The user can update the data in the form and click the Add/Edit button. The issue is there can be rows with the same data in Column A. For example A3 might be 300001 and A4 might be the same 300001. So when they click the Edit button I need it to update the row they are on. Right now, using the code I have, it updates the first row that has 300001 so its updating the wrong row. Is there a way to make it update the row that was selected? Below is my code. Any help would be appreciated.
Code:
Sub EditAdd()
Dim emptyRow As Long
If UserForm1.TextBox1.Value <> "" Then
flag = False
i = 0
ID = UserForm1.TextBox1.Value
emptyRow = WorksheetFunction.CountA(Range("A:A")) + 2
Do While Cells(i + 2, 1).Value <> ""
If Cells(i + 2, 1).Value = ID Then
flag = True
For j = 2 To 16
Cells(i + 2, j).Value = UserForm1.Controls("TextBox" & j).Value
Next j
End If
i = i + 2
Loop
If flag = False Then
For j = 1 To 16
Cells(emptyRow, j).Value = UserForm1.Controls("TextBox" & j).Value
Next j
End If
End If
End Sub