+ Reply to Thread
Results 1 to 2 of 2

Insert Row where value not found

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    11-16-2011
    Location
    Australia
    MS-Off Ver
    Excel 2010
    Posts
    405

    Question Insert Row where value not found

    Hi guys.

    In my worksheet i have 2 sheets, 1 is the report sheet titled "17-01-2012" and the other is "Data"

    The report sheet has data in a timetable style format. each of the coloured ranges are named ranges that at x number of rows in height but always 8 columns wide.
    I need a code that looks at the list on the "Data" sheet starting at cell H3, look at its value and match it to the value found on the report sheet in column A. then continue down the list onto the next value and look for it below the first one found on sheet A. If a value is not found then insert a row and rename to the value not found.

    please see the attached worksheet
    Regards Jordan
    Attached Files Attached Files

  2. #2
    Forum Expert jaslake's Avatar
    Join Date
    02-21-2009
    Location
    Atwood Lake in Mid NE Ohio...look it up.
    MS-Off Ver
    Excel 2010 2019
    Posts
    12,749

    Re: Insert Row where value not found

    Hi jordan2322
    See if this code does as you require
    Option Explicit
    Sub insert_rows()
        Dim LR As Long
        Dim Rng1 As Range
        Dim cel1 As Range
        Dim Rng2 As Range
        Dim FindString As String
        Dim FindString1 As String
    
        ActiveWorkbook.Names.Add Name:="Colours", RefersTo:="=OFFSET(Data!$H$3,0,0,(COUNTA(Data!$H:$H)-1),1)"
        LR = Sheet1.Range("A" & Rows.Count).End(xlUp).Row
    
        Set Rng1 = Sheets("Data").Range("Colours")
        For Each cel1 In Rng1
            FindString = cel1.Value
            If Trim(FindString) <> "" Then
                With Sheet1.Range("A3:A" & LR)
                    Set Rng2 = .Find(What:=FindString, _
                            After:=.Cells(.Cells.Count), _
                            LookIn:=xlValues, _
                            LookAt:=xlWhole, _
                            SearchOrder:=xlByRows, _
                            SearchDirection:=xlNext, _
                            MatchCase:=False)
                    If Not Rng2 Is Nothing Then
                        GoTo SkipMe
                    Else
                        FindString1 = cel1.Offset(1, 0).Value
                        Set Rng2 = .Find(What:=FindString1, _
                                After:=.Cells(.Cells.Count), _
                                LookIn:=xlValues, _
                                LookAt:=xlWhole, _
                                SearchOrder:=xlByRows, _
                                SearchDirection:=xlNext, _
                                MatchCase:=False)
                        If Not Rng2 Is Nothing Then
                            Application.Goto Rng2, True
                            Rng2.EntireRow.Insert
                            ActiveCell.Value = FindString
                            Range(ActiveCell.Address).Resize(1, 8).Name = FindString
                            Range(ActiveCell.Address).Resize(1, 8).Interior.ColorIndex = 0
                            LR = Sheet1.Range("A" & Rows.Count).End(xlUp).Row
                        End If
                    End If
                End With
            End If
    SkipMe:
        Next cel1
    End Sub
    John

    If you have issues with Code I've provided, I appreciate your feedback.

    In the event Code provided resolves your issue, please mark your Thread as SOLVED.

    If you're satisfied by any members response to your issue please use the star icon at the lower left of their post.

+ 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