4

I have a macro which inserts a number of rows depending on user input in Excel 2007. Everything works but I still have a minor issue. I want to copy the complete formatting from the row above. It only works for some cells in the row.

Here is the code for insertion:

 Rows("B:B").Insert Shift:=xlDown, CopyOrigin:=xlFormatFromLeftOrAbove

Is it possible to change it?

best, US

user366121
  • 3,203
  • 9
  • 48
  • 69

4 Answers4

5

I am not sure exactly how you are inserting your rows, but if you insert the row below an existing one, it will by default take on the formatting of the row above it, at least it does when you use this type of syntax:

Range("B2").EntireRow.Offset(1, 0).Insert

In this example, it will insert a row below B2 and the format (say, B2's row is highlighted yellow) will be yellow as well. It might have to do with the fact that this type of inserting specifies exactly under which row to insert.

Gaijinhunter
  • 14,587
  • 4
  • 51
  • 57
1

For those that don't want to inherit formatting from parent range use .cells(). .rows(2).insert will inherit formatting while .cells(2).insert will not.

Dam Gal
  • 135
  • 2
  • 11
0
Dim j As Long, r As Range
j = InputBox("type the number of rows to be insered")

Set r = Range("A4")
Do
    Range(r.Offset(1, 0), r.Offset(j, 0)).EntireRow.Insert
    Set r = Cells(r.Row + j + 1, 1)

If r.Offset(1, 0) = "" Then Exit Do Loop
tkanzakic
  • 5,499
  • 16
  • 34
  • 41
nagma
  • 1
0

The answer is the first comment.

New code:

Rows(CStr(InsRowNumber - 1) & ":" & CStr(InsRowNumber - 1)).Copy
Rows(CStr(InsRowNumber) & ":" & CStr(InsRowNumber)).Insert Shift:=xlDown, CopyOrigin:=xlFormatFromLeftOrAbove
user366121
  • 3,203
  • 9
  • 48
  • 69
  • I suggest you edit your answer with the code you've used and then accept it after the delay. Thus, the thread will be closed (i.e. answered) and it will help further users who will find this thread >if SO helped you, please help SO. – JMax Nov 18 '11 at 15:50
  • 1
    Just want to point out that you don't have to reference a row as Rows(CStr(InsRowNumber - 1) & ":" & CStr(InsRowNumber - 1)) You can simply say Rows(InsRowNumber-1).Copy – Justin Self Nov 18 '11 at 16:35