Inserting New Row for each not empty cell - EXCEL VBA

ozonas

New Member
Joined
Aug 6, 2013
Messages
9
Hello Guys,

I have a small problem with my code - it just keeps inserting new rows without executing the if statement. So basically what I want to do is: I have a spreadsheet with lots of data in it and almost an empty column in between with a couple dates there. If there is a date in that column, I want Excel VBA to insert a new row just bellow that date. Here is my code so far:

Sub newmacro()
Dim rng As Range
Dim cell As Range
Set rng = Range("J3:J40")
For Each cell In rng
If Not IsEmpty(cell.Value) Then
ActiveCell.Offset(1, -9).Select
ActiveCell.EntireRow.Insert
ActiveCell.Offset(1, 9).Select
End If
Next
End Sub

Thank you for your help! Explaining where I went wront would be helpful as well!
 
Hello again,

So my code at the moment looks like this:
Sub newmacro()
Dim Rng As Range
For Each Rng In Range("J3:J40")
If IsDate(Rng.Value) Then
Rng.Offset(1, 0).EntireRow.Insert
Rng.Offset(0, -4).Cut Destination:=Rng.Offset(1, -4)
Rng.Offset(0, 0).Copy Destination:=Rng.Offset(0, -4)
Rng.Offset(0, 1).Copy Destination:=Rng.Offset(1, -5)
Rng.Offset(0, -8).Copy Destination:=Rng.Offset(1, -8)
Rng.Offset(0, -6).Copy Destination:=Rng.Offset(1, -6)
Rng.Offset(0, -3).Copy Destination:=Rng.Offset(1, -3)
Rng.Offset(0, -1).Copy Destination:=Rng.Offset(1, -1)
Rng.Offset(0, 2).Copy Destination:=Rng.Offset(1, 2)
Rng.Offset(0, 3).Copy Destination:=Rng.Offset(1, 3)
Rng.Offset(1, -2).Cells.Value = "Y"
End If
Next
End Sub

As you can see its mapping appropriate data and pasting it in to the empty cells located in that additional row. However, I want to colour the destination cells in red (or any other colour), so I would be aware of the new data when I will be looking at it again. And this is not working: Rng.Offset(1,0).Interior.Color = 5 . I was trying: ng.Offset(0, 3).Copy Destination:=Rng.Offset(1, 3).Interior.Color = 5

Thank you!
 
Upvote 0

Excel Facts

What did Pito Salas invent?
Pito Salas, working for Lotus, popularized what would become to be pivot tables. It was released as Lotus Improv in 1989.

Forum statistics

Threads
1,215,981
Messages
6,128,089
Members
449,418
Latest member
arm56

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