Loop to Cut and Paste Between Two String Occurrences

bmacias

Board Regular
Joined
Sep 11, 2002
Messages
217
Hope I can explain myself clearly on this one.

Starting on cell A2, I'm trying to create a loop that checks the cell value and if it equals "Texas" select the range of cells between the next occurrence of the word "Texas", cut the range and move it column "H". The loop would continue would then select the cells between the 2nd and 3rd Instance of the word "Texas" and move that text to column "H". The values of the cells in between varies but they will not go past column "F". Please see attached pic to see if I'm making sense. My actual database is thousands and thousand rows long, hence my reaching out for help.

Thank you in advance.

Ben
 

Attachments

  • Screen Pic.png
    Screen Pic.png
    56 KB · Views: 9

Excel Facts

Using Function Arguments with nested formulas
If writing INDEX in Func. Arguments, type MATCH(. Use the mouse to click inside MATCH in the formula bar. Dialog switches to MATCH.
Hi bmacias,

Try this:

VBA Code:
Option Explicit
Sub Macro1()

    Dim wsSrc As Worksheet
    Dim lngLastRow As Long
    Dim rng As Range
    
    Application.ScreenUpdating = False
    
    Set wsSrc = ThisWorkbook.Sheets("Sheet1") 'Sheet name with the data. Change to suit if necessary.
    
    With wsSrc
        On Error Resume Next
            .ShowAllData
        On Error GoTo 0
        .Columns.EntireColumn.Hidden = False
        .Rows.EntireRow.Hidden = False
        lngLastRow = .Range("A:F").Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
        Range(.Cells(2, 1), .Cells(lngLastRow, 6)).Copy Destination:=.Range("H2")
        With wsSrc.Columns("H") 'https://www.mrexcel.com/board/threads/replace-a-word-with-nothing-vba.1149029
            .Replace What:="Texas", _
                            Replacement:="", _
                            LookAt:=xlPart, _
                            SearchOrder:=xlByRows, _
                            MatchCase:=False, _
                            SearchFormat:=False, _
                            ReplaceFormat:=False
            
        End With
    End With
    
    Application.ScreenUpdating = True

End Sub

Regards,

Robert
 
Upvote 1
Solution

Forum statistics

Threads
1,220,965
Messages
6,157,119
Members
451,398
Latest member
rjsteward

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