+ Reply to Thread
Results 1 to 6 of 6

Insert row with certain text based on value in a cell in Column A

Hybrid View

  1. #1
    Registered User
    Join Date
    03-06-2004
    Posts
    53

    Insert row with certain text based on value in a cell in Column A

    So here;s what i am working on, and this is eating up my lunches. I have about 7000 or so rows of data in excel. There is a pattern in data. Starting from stae in some cell and right under that is 'heading B' and 2-3-4 rows down is 'heading C' and so on. For every state there are blocks of information. The problem i am facing is that i want to be able to insert a row with text "SECTION BREAK", when the macro finds keyword "Last Revision".

    There are about 200-300 repetitions of this keyword within the sheet and manually going through and inserting rows with text "section break" one by one is very time consuming, Any way excel can do it for me? All the "Last Revision" keywords are located in Column A, but different cells. One could be in cell A2, and the next one in cell A29 and the next one in cell A33 etc etc.

    Any help will be apreciated.

  2. #2
    Forum Expert
    Join Date
    01-12-2007
    Location
    New Jersey
    Posts
    2,127

    Re: Insert row with certain text based on value in a cell in Column A

    Does this work for you?

    Sub SectionBreaks()
        Dim LastRow As Long
        Dim s As String
        
        s = "Last Revision"
        LastRow = Cells(Rows.Count, 1).End(xlUp).Row
        
        With Application
            .ScreenUpdating = False
            
            For i = LastRow To 2 Step -1
                If Cells(i, 1) = s Then
                    Rows(i + 1).Insert
                    Cells(i + 1, 1) = "SECTION BREAK"
                End If
            Next i
            .ScreenUpdating = True
        End With
                
    End Sub

  3. #3
    Registered User
    Join Date
    03-06-2004
    Posts
    53

    Re: Insert row with certain text based on value in a cell in Column A

    Quote Originally Posted by BigBas View Post
    Does this work for you?

    Sub SectionBreaks()
        Dim LastRow As Long
        Dim s As String
        
        s = "Last Revision"
        LastRow = Cells(Rows.Count, 1).End(xlUp).Row
        
        With Application
            .ScreenUpdating = False
            
            For i = LastRow To 2 Step -1
                If Cells(i, 1) = s Then
                    Rows(i + 1).Insert
                    Cells(i + 1, 1) = "SECTION BREAK"
                End If
            Next i
            .ScreenUpdating = True
        End With
                
    End Sub
    Well, i saved as module and ran it, it didn't do anythhing.

  4. #4
    Forum Expert
    Join Date
    01-12-2007
    Location
    New Jersey
    Posts
    2,127

    Re: Insert row with certain text based on value in a cell in Column A

    Can you post a copy of your workbook in XLS format so I can determine the differences? It worked on my test book.

  5. #5
    Forum Expert
    Join Date
    01-12-2007
    Location
    New Jersey
    Posts
    2,127

    Re: Insert row with certain text based on value in a cell in Column A

    Unless, I misunderstood. Is the text Last Revision included in a cell along with other text in that same cell? If so, try the following revised code.

    Sub SectionBreaks()
        Dim LastRow As Long
        Dim s As String
        
        s = "*Last Revision*"
        LastRow = Cells(Rows.Count, 1).End(xlUp).Row
        
        With Application
            .ScreenUpdating = False
            
            For i = LastRow To 2 Step -1
                If Cells(i, 1) Like s Then
                    Rows(i + 1).Insert
                    Cells(i + 1, 1) = "SECTION BREAK"
                End If
            Next i
            .ScreenUpdating = True
        End With
                
    End Sub

  6. #6
    Forum Expert
    Join Date
    11-29-2010
    Location
    Ukraine
    MS-Off Ver
    Excel 2019
    Posts
    4,168

    Re: Insert row with certain text based on value in a cell in Column A

    hi, bholabhala, please check attachments, run code "test"

+ 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