+ Reply to Thread
Results 1 to 3 of 3

Inserting text at the beginning of a section of data and at the end

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    07-31-2012
    Location
    Los Angeles, USA
    MS-Off Ver
    2003, 2010, 365
    Posts
    320

    Inserting text at the beginning of a section of data and at the end

    Hi,

    I have sections of data that start with an identifier (OverComp) that require a <Start> before that section begins and a <Finish> before it reaches the next (OverComp).

    The attached file explains it better as there is an example of before and after.

    Thanks for any help,

    David
    Attached Files Attached Files

  2. #2
    Forum Expert
    Join Date
    07-16-2010
    Location
    Northumberland, UK
    MS-Off Ver
    Excel 2007 (home), Excel 2010 (work)
    Posts
    3,054

    Re: Inserting text at the beginning of a section of data and at the end

    Something like this, perhaps:

    Sub AddStarts()
    
    Const sSEARCH_STRING As String = "!OverComp!"
    Const lTARGET_COL As Long = 1
    
    Const sSTART As String = "<Start>"
    Const sEND As String = "<Finish>"
    
    Dim rngFind As Range
    Dim rngNext As Range
    Dim rngLastCell As Range
    
    Set rngLastCell = Cells(Rows.Count, lTARGET_COL).End(xlUp)
    
    Set rngFind = Columns(lTARGET_COL).Find(sSEARCH_STRING, LookIn:=xlValues, lookat:=xlWhole, MatchCase:=True, after:=rngLastCell)
    
    If Not rngFind Is Nothing Then
      Do
        rngFind.EntireRow.Insert shift:=xlDown
        rngFind.Offset(-1).Value = sSTART
        Set rngLastCell = Cells(Rows.Count, lTARGET_COL).End(xlUp)
        Set rngNext = Columns(lTARGET_COL).FindNext(rngFind)
        If rngNext.Row <= rngFind.Row Then
          Set rngNext = rngLastCell.Offset(1)
        End If
        rngNext.EntireRow.Insert shift:=xlDown
        rngNext.Offset(-1).Value = sEND
        Set rngFind = rngNext
      Loop Until rngFind.Row >= rngLastCell.Row
    End If
    
    End Sub

  3. #3
    Forum Contributor
    Join Date
    07-31-2012
    Location
    Los Angeles, USA
    MS-Off Ver
    2003, 2010, 365
    Posts
    320

    Re: Inserting text at the beginning of a section of data and at the end

    Perfect Andrew, thanks for your hellp.

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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