vba 如何将 Excel 标签标题中的文本垂直居中?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6859127/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 11:41:53  来源:igfitidea点击:

How do I vertically center the text in an Excel label's caption?

excelvbalabelvertical-alignmentcaption

提问by phan

In Excel 2007, I inserted an ActiveX label onto my worksheet. I right-clicked on it and viewed Properties and managed to change the TextAlign property to 2 (frmTextAlignCenter).

在 Excel 2007 中,我在工作表中插入了一个 ActiveX 标签。我右键单击它并查看属性并设法将 TextAlign 属性更改为 2 (frmTextAlignCenter)。

This aligns the label caption's text to the center of the label (horizontally), but the text remains at the TOP of the label. How do I center the caption's text VERTICALLY so that it is in the smack middle of the label?

这会将标签标题的文本与标签的中心(水平)对齐,但文本仍位于标签的顶部。如何将标题的文本垂直居中,使其位于标签的中间?

I've searched "vertical alignment" in SO but nothing comes up for how to do this for an Excel label's caption.

我在 SO 中搜索了“垂直对齐”,但没有任何关于如何为 Excel 标签的标题执行此操作的信息。

Thanks!

谢谢!

采纳答案by jonsca

There's no way to do it directly. This posthas a clever way to accomplish it, though. Make 2 boxes, with the inner one autosized around the text, and position that inner box at the midpoint of the outer box.

没有办法直接做到这一点。 不过,这篇文章有一个聪明的方法来完成它。制作 2 个框,内框在文本周围自动调整大小,并将内框放置在外框的中点。

回答by Larv

There is a trick to do it with a single label. Add an transparent gif image of 1x1 pixel and set the PictureAlignment property to PicturePositionLeftCenter.

有一个技巧可以使用单个标签来做到这一点。添加一个 1x1 像素的透明 gif 图像并将 PictureAlignment 属性设置为 PicturePositionLeftCenter。

回答by YosiN

You will have to use 2 Labels.

您将不得不使用 2 个标签。

For Example, Call them LabelBack, LabelFront. The LabelFront should be set to Opaque and No-Border Make the height of LabelFront smaller than that of LabelBack and put it over it more or less.

例如,将它们称为 LabelBack、LabelFront。LabelFront 应该设置为 Opaque 和 No-Border 使 LabelFront 的高度小于 LabelBack 的高度,并或多或少地放在上面。

Then add the following code:

然后添加以下代码:

LabelFront.Top = (LabelBack.Top + (LabelBack.Height - LabelFront.Height) / 2) - 1

Notice, I subtracted 1to compensate the 1extra pixel within the LabelFront.

请注意,我减去1以补偿1LabelFront 内的额外像素。