Search code examples
excelvbarollupblank-line

How to automatically insert a blank row after a group of data


I have created a sample table below that is similar-enough to my table in excel that it should serve to illustrate the question. I want to simply add a row after each distinct datum in column1 (simplest way, using excel, thanks).

_

CURRENT TABLE:

column1   |   column2   |  column3
----------------------------------
  A       |     small   |  blue
  A       |     small   |  orange
  A       |     small   |  yellow
  B       |     med     |  yellow
  B       |     med     |  blue
  C       |     large   |  green
  D       |     large   |  green
  D       |     small   |  pink

_

DESIRED TABLE

Note: the blank row after each distinct column1

column1   |   column2   |  column3
----------------------------------
  A       |     small   |  blue
  A       |     small   |  orange
  A       |     small   |  yellow

  B       |     med     |  yellow
  B       |     med     |  blue

  C       |     large   |  green

  D       |     large   |  green
  D       |     small   |  pink

Solution

  • This does exactly what you are asking, checks the rows, and inserts a blank empty row at each change in column A:

    sub AddBlankRows()
    '
    dim iRow as integer, iCol as integer
    dim oRng as range
    
    set oRng=range("a1")
    
    irow=oRng.row
    icol=oRng.column
    
    do 
    '
    if cells(irow+1, iCol)<>cells(irow,iCol) then
        cells(irow+1,iCol).entirerow.insert shift:=xldown
        irow=irow+2
    else
        irow=irow+1
    end if
    '
    loop while not cells (irow,iCol).text=""
    '
    end sub
    

    I hope that gets you started, let us know!

    Philip