Hello, i am a new user. From Portugal (sorry mistakes).
I have a Userform with a Textbox and a button. Textbox to input the item and search button to find.
I want:
To search for an item in Column "C" of Sheet1 (input on textbox),
It will find more than 5 when i click the search button.
I need to copy those entire rows to Sheet2, all 8 columns to Sheet2 that contains the item.
This is my code. It works for item "1" (only item in order) but when i search for item "2" and so on, it only copies one row to sheet2 but exists more than 5 rows.
Tks for the help.
I have a Userform with a Textbox and a button. Textbox to input the item and search button to find.
I want:
To search for an item in Column "C" of Sheet1 (input on textbox),
It will find more than 5 when i click the search button.
I need to copy those entire rows to Sheet2, all 8 columns to Sheet2 that contains the item.
This is my code. It works for item "1" (only item in order) but when i search for item "2" and so on, it only copies one row to sheet2 but exists more than 5 rows.
Tks for the help.
Code:
Private Sub CommandButton1_Click()
Dim RowNo As Long, ColNo As Integer, NRow As Long, Sheet1 As Worksheet, Sheet2 As Worksheet
Dim R As Range, vResult As Variant, lRowEnd As Long
Dim Str As String
Str = TextBox1
If Str = "" Then Exit Sub
Set Sheet1 = Sheets("Sheet1")
Set Sheet2 = Sheets("Sheet2")
Sheet2.Cells.Clear
NRow = 0
'** Test if more than 1 entry for requd string **
If WorksheetFunction.CountIf(Sheet1.Columns("C"), Str) > 1 Then
lRowEnd = Sheet1.Cells(Rows.Count, "C").End(xlUp).Row
RowNo = 2
On Error Resume Next
vResult = "*"
vResult = WorksheetFunction.Match(Str, Sheet1.Range("C" & RowNo, "C" & lRowEnd), 0)
Do While IsNumeric(vResult)
RowNo = RowNo + vResult - 1
NRow = NRow + 1
Sheet2.Cells(NRow, 1) = Sheet1.Cells(RowNo, 1)
Sheet2.Cells(NRow, 2) = Sheet1.Cells(RowNo, 2)
Sheet2.Cells(NRow, 3) = Sheet1.Cells(RowNo, 3)
Sheet2.Cells(NRow, 4) = Sheet1.Cells(RowNo, 4)
Sheet2.Cells(NRow, 5) = Sheet1.Cells(RowNo, 5)
Sheet2.Cells(NRow, 6) = Sheet1.Cells(RowNo, 6)
Sheet2.Cells(NRow, 7) = Sheet1.Cells(RowNo, 7)
Sheet2.Cells(NRow, 8) = Sheet1.Cells(RowNo, 8)
RowNo = RowNo + Val(vResult)
vResult = "*"
vResult = WorksheetFunction.Match(Str, Sheet1.Range("C" & RowNo, "C" & lRowEnd), 0)
Loop
On Error GoTo 0
End If
End Sub
Last edited by a moderator: