ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   align text in the center of a range of cells (https://www.excelbanter.com/excel-programming/279951-align-text-center-range-cells.html)

nikolaosk[_9_]

align text in the center of a range of cells
 
hi there,


i have a task to check whether a user has align some text in the range
cells from A1:F1.

so there is some text in cell A1.
then the user selects all the cells from A1:f1 and merges them and
then aligns the text center.
that is easy.

i wrote the above sub and thought would work fine but the problem is
that when someone selects the cells a1 to c1 and centers the text in
that range my sub still gives me a correct result

it should not. how can i write a sub that will give me a correct result
only when the user aligns the given text in the range from cells A1:f1
and only in that range?

please help!!


Sub center()

Set app = Application.Workbooks("budget").Worksheets("money" )

If app.Range("A1:F1").HorizontalAlignment < xlCenter Then


MsgBox "error"
Exit Sub

End If


MsgBox ("ok")
End Sub



------------------------------------------------
~~ Message posted from http://www.ExcelTip.com/
~~ View and post usenet messages directly from http://www.ExcelForum.com/


Tom Ogilvy

align text in the center of a range of cells
 
if Range("A1").MergeArea.Address = "$A$1:$F$1" then
if Range("A1").app.Range("A1:F1").HorizontalAlignment < xlCenter then

End if
else
messagebox "Wrong merge area"
End if

Why not just have your code perform the action of merging and centering.

--
Regards,
Tom Ogilvy

nikolaosk wrote in message
...
hi there,


i have a task to check whether a user has align some text in the range
cells from A1:F1.

so there is some text in cell A1.
then the user selects all the cells from A1:f1 and merges them and
then aligns the text center.
that is easy.

i wrote the above sub and thought would work fine but the problem is
that when someone selects the cells a1 to c1 and centers the text in
that range my sub still gives me a correct result

it should not. how can i write a sub that will give me a correct result
only when the user aligns the given text in the range from cells A1:f1
and only in that range?

please help!!


Sub center()

Set app = Application.Workbooks("budget").Worksheets("money" )

If app.Range("A1:F1").HorizontalAlignment < xlCenter Then


MsgBox "error"
Exit Sub

End If


MsgBox ("ok")
End Sub



------------------------------------------------
~~ Message posted from http://www.ExcelTip.com/
~~ View and post usenet messages directly from http://www.ExcelForum.com/




Iain Sheetware

align text in the center of a range of cells
 
Hi

What you need to do is check which cells are merged first. Try this:

If range("a1").mergearea.address="$a$1:$f$1" then
' the correct range is merged. check if text is centred
else
' the merged range has been changed. tell the user
msgbox "You have changed the merged range."
end if

Hope that helps.

Iain



nikolaosk wrote in message ...
hi there,


i have a task to check whether a user has align some text in the range
cells from A1:F1.

so there is some text in cell A1.
then the user selects all the cells from A1:f1 and merges them and
then aligns the text center.
that is easy.

i wrote the above sub and thought would work fine but the problem is
that when someone selects the cells a1 to c1 and centers the text in
that range my sub still gives me a correct result

it should not. how can i write a sub that will give me a correct result
only when the user aligns the given text in the range from cells A1:f1
and only in that range?

please help!!


Sub center()

Set app = Application.Workbooks("budget").Worksheets("money" )

If app.Range("A1:F1").HorizontalAlignment < xlCenter Then


MsgBox "error"
Exit Sub

End If


MsgBox ("ok")
End Sub



------------------------------------------------
~~ Message posted from http://www.ExcelTip.com/
~~ View and post usenet messages directly from http://www.ExcelForum.com/


Tom Ogilvy

align text in the center of a range of cells
 
your test will always be false:

? range("A1").MergeArea.Address
$A$1:$F$1
? range("a1").mergearea.address="$a$1:$f$1"
False

Addresses are returned in uppercase.

--
Regards,
Tom Ogilvy

Iain Sheetware wrote in message
om...
Hi

What you need to do is check which cells are merged first. Try this:

If range("a1").mergearea.address="$a$1:$f$1" then
' the correct range is merged. check if text is centred
else
' the merged range has been changed. tell the user
msgbox "You have changed the merged range."
end if

Hope that helps.

Iain



nikolaosk wrote in message

...
hi there,


i have a task to check whether a user has align some text in the range
cells from A1:F1.

so there is some text in cell A1.
then the user selects all the cells from A1:f1 and merges them and
then aligns the text center.
that is easy.

i wrote the above sub and thought would work fine but the problem is
that when someone selects the cells a1 to c1 and centers the text in
that range my sub still gives me a correct result

it should not. how can i write a sub that will give me a correct result
only when the user aligns the given text in the range from cells A1:f1
and only in that range?

please help!!


Sub center()

Set app = Application.Workbooks("budget").Worksheets("money" )

If app.Range("A1:F1").HorizontalAlignment < xlCenter Then


MsgBox "error"
Exit Sub

End If


MsgBox ("ok")
End Sub



------------------------------------------------
~~ Message posted from http://www.ExcelTip.com/
~~ View and post usenet messages directly from

http://www.ExcelForum.com/



Iain Sheetware

align text in the center of a range of cells
 
Yes and no. If you have Option Compare Text at the top of the
appropriate module, you will find that "a1"="A1" returns TRUE. But I
agree that in general it is best to test for the exact case you
expect.

"Tom Ogilvy" wrote in message ...
your test will always be false:

? range("A1").MergeArea.Address
$A$1:$F$1
? range("a1").mergearea.address="$a$1:$f$1"
False

Addresses are returned in uppercase.

--
Regards,
Tom Ogilvy

Iain Sheetware wrote in message
om...
Hi

What you need to do is check which cells are merged first. Try this:

If range("a1").mergearea.address="$a$1:$f$1" then
' the correct range is merged. check if text is centred
else
' the merged range has been changed. tell the user
msgbox "You have changed the merged range."
end if

Hope that helps.

Iain



nikolaosk wrote in message

...
hi there,


i have a task to check whether a user has align some text in the range
cells from A1:F1.

so there is some text in cell A1.
then the user selects all the cells from A1:f1 and merges them and
then aligns the text center.
that is easy.

i wrote the above sub and thought would work fine but the problem is
that when someone selects the cells a1 to c1 and centers the text in
that range my sub still gives me a correct result

it should not. how can i write a sub that will give me a correct result
only when the user aligns the given text in the range from cells A1:f1
and only in that range?

please help!!


Sub center()

Set app = Application.Workbooks("budget").Worksheets("money" )

If app.Range("A1:F1").HorizontalAlignment < xlCenter Then


MsgBox "error"
Exit Sub

End If


MsgBox ("ok")
End Sub



------------------------------------------------
~~ Message posted from http://www.ExcelTip.com/
~~ View and post usenet messages directly from

http://www.ExcelForum.com/



All times are GMT +1. The time now is 10:34 PM.

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