Help with VB Code

dbomb1414

New Member
Joined
Apr 26, 2024
Messages
1
Office Version
  1. Prefer Not To Say
I have the following code, however I only want it to find the first occurrence of what they typed in the input box and cut and paste that row. How do I update the code to do that.


Sub Test2()
Dim myWord$
myWord = InputBox("What key word to copy rows", "Enter your word")
If myWord = "" Then Exit Sub

Application.ScreenUpdating = False
Dim xRow&, NextRow&, LastRow&
NextRow = Sheets("Found").Cells.Find(What:="*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row + 1
LastRow = Cells.Find(What:="*", After:=Range("A1"), SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
For xRow = 1 To LastRow
If WorksheetFunction.CountIf(Rows(xRow), "*" & myWord & "*") > 0 Then
Rows(xRow).Cut Sheets("Found").Rows(NextRow)
NextRow = NextRow + 1
End If
Next xRow
Application.ScreenUpdating = True

MsgBox "Macro is complete, " & NextRow - 2 & " rows containing" & vbCrLf & _
"''" & myWord & "''" & " were copied to Sheet2.", 64, "Done"
End Sub
 

Excel Facts

How to calculate loan payments in Excel?
Use the PMT function: =PMT(5%/12,60,-25000) is for a $25,000 loan, 5% annual interest, 60 month loan.
Maybe....

Rich (BB code):
    For xRow = 1 To LastRow
        If WorksheetFunction.CountIf(Rows(xRow), "*" & myWord & "*") > 0 Then
            Rows(xRow).Cut Sheets("Found").Rows(NextRow)
            Exit For
            NextRow = NextRow + 1
        End If
    Next xRow
 
Upvote 0
Alternatively.
VBA Code:
Sub Test2()
    Dim myWord$, FoundWord As Range, Msg As String

    myWord = InputBox("What key word to copy rows", "Enter your word")
    If myWord = "" Then Exit Sub

    Application.ScreenUpdating = False
    With Sheets("Found")
        'Set FoundWord = .UsedRange.Find(What:=myWord, LookAt:=xlWhole, SearchOrder:=xlByRows) 'whole cell
        Set FoundWord = .UsedRange.Find(What:=myWord, LookAt:=xlPart, SearchOrder:=xlByRows)

        If Not FoundWord Is Nothing Then
            FoundWord.EntireRow.Cut .Rows(.UsedRange.Rows.Count + 1)
            Msg = "Macro is complete, row " & FoundWord.Row & " containing" & vbCrLf & "''" & myWord & "''" & " was copied to end"
        Else
            Msg = "Macro is complete." & vbCrLf & "''" & myWord & "''" & " was not found."
        End If
    End With
    Application.ScreenUpdating = True

    MsgBox Msg, 64, "Done"
End Sub
 
Upvote 0

Forum statistics

Threads
1,215,545
Messages
6,125,450
Members
449,227
Latest member
Gina V

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top