Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Excel 2003: lines' insertion by a macro

Status
Not open for further replies.

Kimed

Programmer
May 25, 2005
104
LT
Hi,

I have a sheet with a fragment that looks like this:
Code:
          A:         B:
line1: +--BIG--+    cellB1
line2: | MERGED|    cellB2
line3: +--Cell-+    cellB3
line4:           =sum(B1:B3)
Now, if I *manually* select the whole line 3, copy it, and then do "Insert Copied Cells" on the same line, the line will be copied like this:
Code:
          A:         B:
line1: +--BIG--+    cellB1
line2: | MERGED|    cellB2
line3: +  Cell +    cellB3
line4: +-------+    cellB3
line5:           =sum(B1:B4)
The merged cell A1-3 will appropriately grow, and the formula range will include the added line as well. That's normal. If I issue following commands in a macro, though:

Rows("3:3").Select
Selection.Copy
Selection.Insert Shift:=xlDown

the result will be different:
Code:
          A:          B:
line1: +--BIG--+    cellB1
line2: | MERGED|    cellB2
line3: +--Cell-+    cellB3
line4: +--BIG--+    cellB1
line5: | MERGED|    cellB2
line6: +--Cell-+    cellB3
line7:           =sum(B4:B6)
How should I write the macro so that insertion would work the 'right' way?

Thanks
 
Code:
With Rows("3:3")
    .Copy
    .Insert Shift:=xlDown
End With

combo
 
Thanks, it worked. Weird, though, I've already tried commands like
Code:
Rows("3:3").Copy
Rows("3:3").Insert Shift:=xlDown
earlier, without WITH/END WITH. It gave me a runtime error 1004 on Insert command, so I had to do it longer way, which at least produced the desirable result... until I bumped onto a merged cell issue.

Thanks again.


 
The With ... End With structure does not change anything. Your code works with active sheet (no worksheet reference), the row culd not either be copied or pasted in existing conditions.

combo
 




FYI, merged cells wreak all sorts of coding havoc. I would avoid if at all possible.

Skip,
[glasses]Don't let the Diatribe...
talk you to death![tongue]

[glasses]Just traded in my old subtlety...
for a NUANCE![tongue]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top