Insert Row only above a row with a number in column 1

ktmo

New Member
Joined
Jun 5, 2006
Messages
10
We have a calculation sheet that exports a list of test treatments to Excel from another database. We then run a macro on it to insert formulas to calculate certain things for our lab work. There can be anywhere between 1 and 25 treatments usually.

When the data exports to excel, the treatment number is listed in column 1 (A). There might be multiple lines of data per treatment, but the treatment number is only printed once on the first line of that treatment.

When there are multiple treatments in the list, it is difficult to distinguish them from one another because there are no spaces between the different treatments. See below: (well, in the post it aligns everything to the left, but the numbers and the chems are in different columns)

1 chem 1
chem 2
chem 3
2 chem 1
chem 3
3 chem 2
chem 3
4 chem 3

Is there a way to insert a blank row above each row with a number in the first column? I think it would involve searching down until finding a populated cell and inserting a row. Somehow it needs to only do it for numbers though, because there is some text that prints out at the end of the treatment list. I've seen similar problems solved on the board but I can't figure out how to put them together to do this.
Help?
 

Excel Facts

Which came first: VisiCalc or Lotus 1-2-3?
Dan Bricklin and Bob Frankston debuted VisiCalc in 1979 as a Visible Calculator. Lotus 1-2-3 debuted in the early 1980's, from Mitch Kapor.
Try this

Code:
Sub AddBlanks()
Dim LR As Long, i As Long
LR = Range("B" & Rows.Count).End(xlUp).Row
For i = LR To 2 Step -1
    If Range("A" & i).Value <> "" Then Rows(i).Insert
Next i
End Sub
 
Upvote 0
Ok, that added rows above each number, but it also added rows above any row with anything in column 1 (A), not just numbers.

THe numbers ALWAYS start with 1 on ROW 18.

Is there anyway to run this only on or below ROW 18?
 
Upvote 0
Try this

Code:
Sub AddBlanks()
Dim LR As Long, i As Long
LR = Range("B" & Rows.Count).End(xlUp).Row
For i = LR To 19 Step -1
    If Range("A" & i).Value <> "" Then Rows(i).Insert
Next i
End Sub
 
Upvote 0
ktmo,

Before the macro:


Excel Workbook
AB
11chem 1
2chem 2
3chem 3
42chem 1
5chem 3
63chem 2
7chem 3
84chem 3
Sheet1




After the macro:


Excel Workbook
AB
1
21chem 1
3chem 2
4chem 3
5
62chem 1
7chem 3
8
93chem 2
10chem 3
11
124chem 3
Sheet1



Please TEST this FIRST in a COPY of your workbook (always make a backup copy before trying new code, you never know what you might lose).

Press and hold down the 'ALT' key, and press the 'F11' key.

On the 'Insert' menu, click 'Module'.

Copy the below code, and paste it into the Module (on the right pane).


Code:
Option Explicit
Sub InsertRow()
    Dim LR As Long, Ctr As Long
    Application.ScreenUpdating = False
    With ActiveSheet
        LR = .Cells(Rows.Count, 2).End(xlUp).Row
        For Ctr = LR To 1 Step -1
            If .Cells(Ctr, 1).Value = 0 Then
                'Do nothing
            ElseIf IsNumeric(.Cells(Ctr, 1).Value) Then
                .Cells(Ctr, 1).EntireRow.Insert
            End If
        Next Ctr
    End With
    Application.ScreenUpdating = True
End Sub


Then run the "InsertRow" macro.


Have a great day,
Stan
 
Upvote 0
ktmo,

Is there anyway to run this only on or below ROW 18?


Code:
Option Explicit
Sub InsertRow()
    Dim LR As Long, Ctr As Long
    Application.ScreenUpdating = False
    With ActiveSheet
        LR = .Cells(Rows.Count, 2).End(xlUp).Row
        For Ctr = LR To 19 Step -1
            If .Cells(Ctr, 1).Value = 0 Then
                'Do nothing
            ElseIf IsNumeric(.Cells(Ctr, 1).Value) Then
                .Cells(Ctr, 1).EntireRow.Insert
            End If
        Next Ctr
    End With
    Application.ScreenUpdating = True
End Sub


Have a great day,
Stan
 
Upvote 0
Thank you! You all are ROCKSTARS!

This is the last bit of a much longer macro that takes a bunch of garble and calculates what we need then makes it nice and easy to read and print.

I have learned so much about VBA through this board. Thanks!
 
Upvote 0

Forum statistics

Threads
1,215,575
Messages
6,125,631
Members
449,241
Latest member
NoniJ

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