My data should have 6 standard headers, but sometimes these are missing, can a VBA code loop, and insert blank lines, where these should be?

lynch

New Member
Joined
Apr 5, 2012
Messages
4
I was hoping someone would be genius enough to help with this, I would be very grateful.

In Colum C of my data (les that 50 rows)- there are usually 6 items for each record. These are:
BOOK
CA
OVER
SPEC
DIR
TOTAL

Sometimes, SPEC or DIR may be missing from a record- I would like a blank row inserted for these. Even better if the could be named SPEC and DIR. (Please see the results image)

Thanks for you help!
 

Attachments

  • data.PNG
    data.PNG
    32.6 KB · Views: 11
  • RESULT.PNG
    RESULT.PNG
    40 KB · Views: 11

Excel Facts

Who is Mr Spreadsheet?
Author John Walkenbach was Mr Spreadsheet until his retirement in June 2019.
@lynch Try this.

VBA Code:
Sub InsertHeeaders()

Dim LastR As Integer
Dim r As Integer

LastR = Range("C" & Rows.Count).End(xlUp).Row

If Not Range("C" & LastR) = "TOTAL" Then Exit Sub

Application.ScreenUpdating = False
r = LastR

Do While r > 3
    Select Case Range("C" & r)
        Case "TOTAL"
            If Not Range("C" & r - 1) = "DIR" Then
                Range("C" & r).EntireRow.Insert
                Range("C" & r) = "DIR"
            Else
            r = r - 1
             End If
         Case "DIR"
            If Not Range("C" & r - 1) = "SPEC" Then
                Range("C" & r).EntireRow.Insert
                Range("C" & r) = "SPEC"
            Else
                r = r - 1
            End If
         Case Else
         r = r - 1
    End Select
Loop

Application.ScreenUpdating = True
End Sub

Hope that helps.
 
Upvote 0

Forum statistics

Threads
1,223,886
Messages
6,175,191
Members
452,616
Latest member
intern444

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top