ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Dealing with merged cells (https://www.excelbanter.com/excel-programming/391131-dealing-merged-cells.html)

Chrisso

Dealing with merged cells
 
I have a range name for a cell of MYNAME.

MYNAME started off life just being a single cell and I could do all
sorts of wonderful things to it concisely:

Range("MYNAME").ClearContents
Range("MYNAME").Locked = True

I have now made MYNAME a merged cell that spans two columns (A1:B1).
Now I find for my code to work I have to select the merged cells first
then perform my operations:

Range("MYNAME").Select
Selection.ClearContents
Selection.Locked = True

Is there a way to deal with merged cells so that I can perform the
above operations with just one line of code to make the code easier to
read and follow?

Chrisso


Barb Reinhardt

Dealing with merged cells
 
I'd probably do it this way

Sub Test()

With Range("MYNAME").MergeArea
.ClearContents
.Locked = True
End With

End Sub

HTH,
Barb Reinhardt
"Chrisso" wrote:

I have a range name for a cell of MYNAME.

MYNAME started off life just being a single cell and I could do all
sorts of wonderful things to it concisely:

Range("MYNAME").ClearContents
Range("MYNAME").Locked = True

I have now made MYNAME a merged cell that spans two columns (A1:B1).
Now I find for my code to work I have to select the merged cells first
then perform my operations:

Range("MYNAME").Select
Selection.ClearContents
Selection.Locked = True

Is there a way to deal with merged cells so that I can perform the
above operations with just one line of code to make the code easier to
read and follow?

Chrisso



Tom Ogilvy

Dealing with merged cells
 
I is unclear if the definition of myname is 1 cell or 2, but if it is
actually defined to be two cells or could be two cells, then I would alter it
as such:


Sub Test()

With Range("MYNAME")(1).MergeArea
.ClearContents
.Locked = True
End With

End Sub

--
Regards,
Tom Ogilvy


"Barb Reinhardt" wrote:

I'd probably do it this way

Sub Test()

With Range("MYNAME").MergeArea
.ClearContents
.Locked = True
End With

End Sub

HTH,
Barb Reinhardt
"Chrisso" wrote:

I have a range name for a cell of MYNAME.

MYNAME started off life just being a single cell and I could do all
sorts of wonderful things to it concisely:

Range("MYNAME").ClearContents
Range("MYNAME").Locked = True

I have now made MYNAME a merged cell that spans two columns (A1:B1).
Now I find for my code to work I have to select the merged cells first
then perform my operations:

Range("MYNAME").Select
Selection.ClearContents
Selection.Locked = True

Is there a way to deal with merged cells so that I can perform the
above operations with just one line of code to make the code easier to
read and follow?

Chrisso



Barb Reinhardt

Dealing with merged cells
 
Tom,

What's the difference between

Range("MYNAME")(1).MergeArea

and

Range("MYNAME").MergeArea

Thanks,
Barb

"Tom Ogilvy" wrote:

I is unclear if the definition of myname is 1 cell or 2, but if it is
actually defined to be two cells or could be two cells, then I would alter it
as such:


Sub Test()

With Range("MYNAME")(1).MergeArea
.ClearContents
.Locked = True
End With

End Sub

--
Regards,
Tom Ogilvy


"Barb Reinhardt" wrote:

I'd probably do it this way

Sub Test()

With Range("MYNAME").MergeArea
.ClearContents
.Locked = True
End With

End Sub

HTH,
Barb Reinhardt
"Chrisso" wrote:

I have a range name for a cell of MYNAME.

MYNAME started off life just being a single cell and I could do all
sorts of wonderful things to it concisely:

Range("MYNAME").ClearContents
Range("MYNAME").Locked = True

I have now made MYNAME a merged cell that spans two columns (A1:B1).
Now I find for my code to work I have to select the merged cells first
then perform my operations:

Range("MYNAME").Select
Selection.ClearContents
Selection.Locked = True

Is there a way to deal with merged cells so that I can perform the
above operations with just one line of code to make the code easier to
read and follow?

Chrisso



Tom Ogilvy

Dealing with merged cells
 
If:
Name: MyName
Refersto: Sheet1!$A$1:$B$2

then
Range("MYNAME").MergeArea

raises an error

Range("MYNAME")(1).MergeArea

doesn't.

--
Regards,
Tom Ogilvy


"Barb Reinhardt" wrote:

Tom,

What's the difference between

Range("MYNAME")(1).MergeArea

and

Range("MYNAME").MergeArea

Thanks,
Barb

"Tom Ogilvy" wrote:

I is unclear if the definition of myname is 1 cell or 2, but if it is
actually defined to be two cells or could be two cells, then I would alter it
as such:


Sub Test()

With Range("MYNAME")(1).MergeArea
.ClearContents
.Locked = True
End With

End Sub

--
Regards,
Tom Ogilvy


"Barb Reinhardt" wrote:

I'd probably do it this way

Sub Test()

With Range("MYNAME").MergeArea
.ClearContents
.Locked = True
End With

End Sub

HTH,
Barb Reinhardt
"Chrisso" wrote:

I have a range name for a cell of MYNAME.

MYNAME started off life just being a single cell and I could do all
sorts of wonderful things to it concisely:

Range("MYNAME").ClearContents
Range("MYNAME").Locked = True

I have now made MYNAME a merged cell that spans two columns (A1:B1).
Now I find for my code to work I have to select the merged cells first
then perform my operations:

Range("MYNAME").Select
Selection.ClearContents
Selection.Locked = True

Is there a way to deal with merged cells so that I can perform the
above operations with just one line of code to make the code easier to
read and follow?

Chrisso




All times are GMT +1. The time now is 05:08 AM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com