Hello there,
Try the following code:
Dim rfound As Range 'declare variable
On Error Resume Next
With Sheets("YourWkshtNameHere") 'insert you worksheet name to search
'set rfound equal to the found cell in column B whose value matches the value
'selected from the combobox on the userform, starting looking after cell B1
'to change the column to look in change the 2's in the following line of code
'to the index number for the column you wish to search (i.e. A=1 B=2)
Set rfound = .Columns(2).Find(What:=Me.ComboBox1.Value, After:=.Cells(1, 2), LookIn:=xlValues, LookAt:= _
xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=False _
, SearchFormat:=False)
On Error GoTo 0
If rfound Is Nothing Then 'if the value select is not found then
MsgBox "Not found" 'provide message box saying so
Else 'if the value was found then
'select the row beneath the found cell and insert a row
'therfore inserting a row below the found cell
.Rows(rfound.Row + 1 & ":" & rfound.Row + 1).Insert shift:=(xlDown)
'set the value in the newly inserted row equal to the textbox on the userform's value
Range("C" & rfound.Row + 1) = Me.TextBox1.Value
Application.Goto rfound 'go to the found cell
End If
End With
Unload Me 'close the userform
to insert the code:
1. Open Visual Basic
2. Select your userform and then right click on the submit command button and select view code.
3. In between the Private Sub NameofYourcommandbutton_Click() and End Sub copy and paste the above code.
4. Anything that appears in green is a comment meant to help you understand. You'll need to read through it so that you can adust the code to fit your needs.
5. Close out of Visual Basic.
Let me know if this works for you!
Thanks!
Bookmarks