Search code examples
excelvbaautomationspreadsheet

VBA Auto Row Insert by Starting Letter


Is there a way I can use VBA to search any of the rows in column A for the letter Q and if letter Q is the first letter in that row than the Macro would automatically hit insert on that row separating it from the previous one.

I essentially want to insert a row above every time the letter Q is the first letter of a row...


Solution

  • A lot of things can be done while combining super basic tutorials.

    Find a tutorial how to loop through range, then how to get first letter in a cell and then how to insert row, then understand that while inserting/deleting it is better to loop from top to bottom:

    Sub InsertRowBeforeQ()
        For i = 20 To 1 Step -1
            If Left$(Range("A" & i), 1) = "Q" Then Range("A" & i).EntireRow.Insert
        Next i
    End Sub
    

    Before:

    enter image description here

    After:

    enter image description here