Hi Excel Users!
I have a UserForm with a TextBox and two buttons.
You insert a text string in the textbox, click the button, and this is what it does :
It finds the string in the A column of Sheet1.
It copies the whole row containing the matched string from Sheet1 to Sheet2.
It deletes the whole row from Sheet1.
Result : Cut from Sheet1 and paste to Sheet2
See the code at the end of the post.
I would like to know how to adapt that code to use a multiline TextBox, so I can do the same procedure for a large amount of strings (one per line). I know how to tweak the properties of the textbox to make it multiline, but I don't know how to turn the code into a loop that does the same, one by one, to all of them. I am going to paste a large amount of strings (~500) so it has to be ready for that.
I would be very gald if someone could give me a hand.
Thanks!
I have a UserForm with a TextBox and two buttons.
You insert a text string in the textbox, click the button, and this is what it does :
It finds the string in the A column of Sheet1.
It copies the whole row containing the matched string from Sheet1 to Sheet2.
It deletes the whole row from Sheet1.
Result : Cut from Sheet1 and paste to Sheet2
See the code at the end of the post.
I would like to know how to adapt that code to use a multiline TextBox, so I can do the same procedure for a large amount of strings (one per line). I know how to tweak the properties of the textbox to make it multiline, but I don't know how to turn the code into a loop that does the same, one by one, to all of them. I am going to paste a large amount of strings (~500) so it has to be ready for that.
Code:
Private Sub CommandButton1_Click()
Dim myString As String
Dim foundCell As Range
myString = Trim(UserForm2.TextBox1.Value)
If myString = vbNullString Then
Exit Sub
End If
On Error GoTo ErrorOut
' Find and copy entire row.
With Sheets("Sheet1").Range("A:A")
Set foundCell = .Find(What:=myString, After:=.Cells(1, 1), LookAt:=xlWhole)
foundCell.EntireRow.Copy
End With
' Paste copied cell.
Sheets("Sheet2").Cells(Rows.Count, 1).End(xlUp).Offset(1, 0).PasteSpecial
' Delete entire row of found range.
foundCell.EntireRow.Delete
UserForm2.Label2.Caption = "Record " & myString & " moved from Sheet1 to Sheet2"
Exit Sub
ErrorOut:
UserForm2.Label2.Caption = "Record " & myString & " doesn't exist in Sheet1 col A"
On Error GoTo 0
End Sub
Thanks!