Consulting

Results 1 to 3 of 3

Thread: Insert Row based on partial cell value

  1. #1
    VBAX Regular
    Joined
    Mar 2007
    Posts
    9
    Location

    Insert Row based on partial cell value

    How can you insert rows in a column based on a partial cell value.

    For example, I have several cost codes that I'd like to insert a row between departments. The third digit in the number indicates the department.

    10101
    10105
    10103
    10300 << (Insert Row Here)
    10308
    10309
    10960 << (Insert Row Here)
    10990

    Thanks for your help.

  2. #2
    Moderator VBAX Master geekgirlau's Avatar
    Joined
    Aug 2004
    Location
    Melbourne, Australia
    Posts
    1,464
    Location
    [vba]Sub SplitCodes()
    Dim rng As Range
    Dim strDept As String


    strDept = Mid(Range("A1").Value, 3, 1)

    ' work through the used range in a single column
    ' (change the starting point as required)
    For Each rng In Range(Range("A1"), Range("A1").End(xlDown))
    If Mid(rng.Value, 3, 1) <> strDept Then
    strDept = Mid(rng.Value, 3, 1)
    rng.EntireRow.Insert
    End If
    Next rng
    End Sub[/vba]

  3. #3
    Moderator VBAX Wizard lucas's Avatar
    Joined
    Jun 2004
    Location
    Tulsa, Oklahoma
    Posts
    7,323
    Location
    another clean solution....nice one Anne
    Steve
    "Nearly all men can stand adversity, but if you want to test a man's character, give him power."
    -Abraham Lincoln

Posting Permissions

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