Insert New Row after finding last Occurrence Of Text Value in Col C

sspatriots

Well-known Member
Joined
Nov 22, 2011
Messages
565
Office Version
  1. 365
Platform
  1. Windows
Good morning,

I'm trying to use the code below that I found to insert a new row in a table just below the last occurrence of finding "1 AJW" in column C. This code I copied and tried to manipulate was originally set up up to insert two rows. I only need to insert 1 row. I also don't quite get what the ".Value" and ".Offset" are doing in the code or how to adjust for my situation.

Sub InsertNewJob
Dim Lst As Integer, n As Long, Txt As String, Temp As String, R As Range
Lst = Range("C" & Rows.Count).End(xlUp).Row
For n = Lst To 2 Step -1
With Range("C" & n)
Txt = ""
Select Case True

Case Left(.Value, 2) & .Offset(, 5).Value = "1 AJW"
If IsNumeric(Mid(.Value, 3, 1)) Then
Txt = "1 AJW"

End If
Case Left(.Value, 3) & .Offset(, 5).Value = "1 AJW"
End Select


If Not Txt = Temp Then
.Offset(1).Resize(2).EntireRow.Insert
Temp = Txt
End If


End With

End Sub


Thanks, SS
 

Excel Facts

Format cells as currency
Select range and press Ctrl+Shift+4 to format cells as currency. (Shift 4 is the $ sign).
One way.

VBA Code:
Sub InsertNewJob()

    Dim R As Range, rng As Range, rngFirst As Range, rngLast As Range
        
    Set rng = Range("C2", Range("C" & Rows.Count).End(xlUp))
    
    Set rngFirst = rng.Find(What:="1 AJW", Lookat:=xlWhole, MatchCase:=True)
    If Not rngFirst Is Nothing Then
        Set R = rngFirst
        Do While Not R Is Nothing
            Set rngLast = R
            Set R = rng.Find(What:="1 AJW", After:=R, Lookat:=xlWhole, MatchCase:=False, SearchDirection:=xlNext)
            If R.Address = rngFirst.Address Then
                Set R = Nothing
                Exit Do
            End If
        Loop
        rngLast.Offset(1).EntireRow.Insert Shift:=xlDown
    End If
End Sub
 
Upvote 0
That seems to work, but how do I shift the focus to go to that first cell in that row so the user can make their entry? Even better scroll that row to the top of the table at the same time.
 
Upvote 0
VBA Code:
Sub InsertNewJob()
    Dim R As Range, rng As Range, rngFirst As Range, rngLast As Range
    
    Set rng = Range("C2", Range("C" & Rows.Count).End(xlUp))
    
    Set rngFirst = rng.Find(What:="1 AJW", Lookat:=xlWhole, MatchCase:=True)
    If Not rngFirst Is Nothing Then
        Set R = rngFirst
        Do While Not R Is Nothing
            Set rngLast = R
            Set R = rng.Find(What:="1 AJW", After:=R, Lookat:=xlWhole, MatchCase:=False, SearchDirection:=xlNext)
            If R.Address = rngFirst.Address Then
                Set R = Nothing
                Exit Do
            End If
        Loop
        With rngLast.Offset(1)
            .Select
            .EntireRow.Insert Shift:=xlDown
        End With
    End If
End Sub
 
Upvote 0
Solution

Forum statistics

Threads
1,214,596
Messages
6,120,438
Members
448,966
Latest member
DannyC96

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