+ Reply to Thread
Results 1 to 6 of 6

Look for specific text string, insert row below, insert text to that row

Hybrid View

  1. #1
    Forum Guru
    Join Date
    07-25-2011
    Location
    Florida
    MS-Off Ver
    Excel 2003
    Posts
    9,652

    Re: Look for specific text string, insert row below, insert text to that row

    This uses the .Find method

    Sub Routers()
        
        Dim Found As Range, FirstFound As String, counter As Long
            
        Application.ScreenUpdating = False
            
        Set Found = Range("A:A").Find(What:="primary ""igp""", _
                                      LookIn:=xlValues, _
                                      LookAt:=xlPart, _
                                      SearchOrder:=xlByRows, _
                                      SearchDirection:=xlNext, _
                                      MatchCase:=False)
                    
        If Not Found Is Nothing Then
            
            FirstFound = Found.Address
            
            Do
                If InStr(1, Found.Offset(1), "Adaptive", 1) = 0 Then
                    Found.Offset(1).Insert Shift:=xlShiftDown
                    counter = counter + 1
                End If
                
                Set Found = Range("A:A").FindNext(After:=Found)
                
            Loop Until Found.Address = FirstFound
            
        End If
        
        Application.ScreenUpdating = True
        
        MsgBox counter & " rows inserted. ", vbInformation, "Adaptive Inserts Complete"
        
    End Sub
    Surround your VBA code with CODE tags e.g.;
    [CODE]your VBA code here[/CODE]
    The # button in the forum editor will apply CODE tags around your selected text.

  2. #2
    Registered User
    Join Date
    01-13-2014
    Location
    Pensacola, USA
    MS-Off Ver
    Excel 2010
    Posts
    20

    Re: Look for specific text string, insert row below, insert text to that row

    Quote Originally Posted by AlphaFrog View Post
    This uses the .Find method

    Sub Routers()
        
        Dim Found As Range, FirstFound As String, counter As Long
            
        Application.ScreenUpdating = False
            
        Set Found = Range("A:A").Find(What:="primary ""igp""", _
                                      LookIn:=xlValues, _
                                      LookAt:=xlPart, _
                                      SearchOrder:=xlByRows, _
                                      SearchDirection:=xlNext, _
                                      MatchCase:=False)
                    
        If Not Found Is Nothing Then
            
            FirstFound = Found.Address
            
            Do
                If InStr(1, Found.Offset(1), "Adaptive", 1) = 0 Then
                    Found.Offset(1).Insert Shift:=xlShiftDown
                    counter = counter + 1
                End If
                
                Set Found = Range("A:A").FindNext(After:=Found)
                
            Loop Until Found.Address = FirstFound
            
        End If
        
        Application.ScreenUpdating = True
        
        MsgBox counter & " rows inserted. ", vbInformation, "Adaptive Inserts Complete"
        
    End Sub
    AlphaFrog,

    This is very close, but when I run the code it finds primary "igp" and inserts blank row, but doesn't put the word adaptive, it does however leave the rows alone that already have adaptive. Can you have a second look?

  3. #3
    Forum Guru
    Join Date
    07-25-2011
    Location
    Florida
    MS-Off Ver
    Excel 2003
    Posts
    9,652

    Re: Look for specific text string, insert row below, insert text to that row

    Quote Originally Posted by jwats View Post
    AlphaFrog,

    This is very close, but when I run the code it finds primary "igp" and inserts blank row, but doesn't put the word adaptive, it does however leave the rows alone that already have adaptive. Can you have a second look?
    I missed that part. Sorry.

    Sub Routers()
        
        Dim Found As Range, FirstFound As String, counter As Long
            
        Application.ScreenUpdating = False
            
        Set Found = Range("A:A").Find(What:="primary ""igp""", _
                                      LookIn:=xlValues, _
                                      LookAt:=xlPart, _
                                      SearchOrder:=xlByRows, _
                                      SearchDirection:=xlNext, _
                                      MatchCase:=False)
                    
        If Not Found Is Nothing Then
            
            FirstFound = Found.Address
            
            Do
                If InStr(1, Found.Offset(1), "Adaptive", 1) = 0 Then
                    Found.Offset(1).Insert Shift:=xlShiftDown
                    Found.Offset(1).Value = "adaptive"
                    counter = counter + 1
                End If
                
                Set Found = Range("A:A").FindNext(After:=Found)
                
            Loop Until Found.Address = FirstFound
            
        End If
        
        Application.ScreenUpdating = True
        
        MsgBox counter & " rows inserted. ", vbInformation, "Adaptive Inserts Complete"
        
    End Sub

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Macro that will insert a simple text string in top cell of text file
    By Davek11 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 10-25-2014, 07:31 PM
  2. Insert text before the last item in a string
    By jwac in forum Excel Formulas & Functions
    Replies: 1
    Last Post: 04-26-2013, 02:33 PM
  3. String If text insert date
    By trent241 in forum Excel General
    Replies: 4
    Last Post: 05-12-2012, 12:47 PM
  4. Excel 2007 : Insert Text into string
    By skidz in forum Excel General
    Replies: 5
    Last Post: 11-12-2010, 12:56 PM
  5. insert spaces into text string
    By mmwltd in forum Excel General
    Replies: 6
    Last Post: 04-27-2010, 01:05 PM
  6. [SOLVED] How do I insert content of a2 into text string in b2 and so on?
    By Allenc in forum Excel Formulas & Functions
    Replies: 1
    Last Post: 08-05-2006, 07:30 PM
  7. Insert characters in a text string
    By jamae918 in forum Excel Formulas & Functions
    Replies: 1
    Last Post: 03-28-2005, 05:06 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1