merging ranges in VBA

G

GregJG

i am running this code,

sub page_setup

Set SourceWB = Workbooks("book1.xls")
With SourceWB.Sheets("sheet1")

' alot of other coding, but this seems to be the problem area

.Range("K2:L2").Merge
.Range("K3:L3").Merge
.Range("K4:L4").Merge
.Range("K5:L5").Merge
.Range("K6:L6").Merge
.Range("K7:L7").Merge
end with

end sub
________________________________
sub page_info

Set SourceWB = Workbooks("book1.xls")
With SourceWB.Sheets("sheet1")
.Range("K7").Value = FormatPercent(.Range("K6") / .Range("K4"), 2)

end sub

I am getting an overflow error, so I look at the sheet and it ha
merged all the cells K2:L7

i don't get the error if below is removed,
but i need them merged also.

.Range("K3:L3").Merge
.Range("K4:L4").Merge
.Range("K5:L5").Merge
.Range("K6:L6").Merge
.Range("K7:L7").Merge


I have tried variations to the code, and looked thru help but can'
find any.

can anyone help
 
D

Dave Peterson

Your code worked ok for me in xl2002--as long as I had a number in K6:)L6) and a
non-zero number in K4:)L4).

But excel has treated merged cells better in later versions.

If you're sure you have a non-zero value in K6, maybe this will work (it also
worked in xl2002).

With Sheets("sheet1")
.Range("K7").Value = FormatPercent(.Range("K6").MergeArea(1).Value _
/ .Range("K4").MergeArea(1).Value, 2)
End With

(I like to use .value.)
 
D

Dave Peterson

ps. If it doesn't work, you'll want to post back the version of excel you're
using.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top