Excel VBA 文本框字体大小

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19694701/
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 17:05:04  来源:igfitidea点击:

Excel VBA Textbox font size

excelvba

提问by Ian Gough

I have a userform to display my terms and conditions which is picked up directly from a cell. Upon Activation I call TandC.Text = Worksheets("Master").Range("L41")which works perfectly when I run the form directly -- correct font size, multiple lines, word wrap, etc. However, when I run the form from another userform, the text box text suddenly displays the text in a tiny unreadable font. Even when I put a test button on my form to set the font size to 28, it doesn't work when the form is kicked off from another form; however, it does change it when it's running on its own.

我有一个用户表单来显示我的条款和条件,它是直接从一个单元格中提取的。激活后我调用TandC.Text = Worksheets("Master").Range("L41")它在我直接运行表单时完美运行 - 正确的字体大小,多行,自动换行等。 但是,当我从另一个用户表单运行表单时,文本框文本突然以不可读的微小文本显示文本字体。即使我在表单上放置一个测试按钮将字体大小设置为 28,当表单从另一个表单启动时它也不起作用;但是,当它自己运行时,它确实会改变它。

It appears to be an issue when Multilineis turned on, I turn it off and the font is the correct size but the text spans for miles to the right. I turn it back on and again I have a tiny unreadable font.

Multiline打开时似乎是一个问题,我将其关闭并且字体大小正确,但文本向右跨越数英里。我重新打开它,我再次看到一个很小的无法阅读的字体。

It's driving me insane, can anyone offer any advice on this?

这让我发疯了,有人可以就此提供任何建议吗?

See below my code, kicking the form off from the originating form is just a simple UF.Show.

请参阅下面我的代码,从原始表单中删除表单只是一个简单的UF.Show.

Mine is really similar other then in my initialize are i have some code that centralizes the form on the screen if you have multiple monitors.

我的非常相似,然后在我的初始化中,如果您有多个显示器,我有一些代码可以将表单集中在屏幕上。

Private Sub CommandButton2_Click()
TandC.WordWrap = True
TandC.Font.Size = 8
TandC.MultiLine = True

End Sub

Private Sub UserForm_Initialize()

Dim TopOffset As Integer
Dim LeftOffset As Integer
TopOffset = (Application.UsableHeight / 2) - (Me.Height / 2)
LeftOffset = (Application.UsableWidth / 2) - (Me.Width / 2)
Me.Top = Application.Top + TopOffset
Me.Left = Application.Left + LeftOffset

TandC.Text = Worksheets("MasterData").Range("L21")
TandC.WordWrap = True
TandC.MultiLine = True
TandC.Font.Size = 8

End Sub

回答by Portland Runner

Try turning on autosizefor your text box. Looks like multi-line is trying to fit all text into your text box by adjusting the font size rather than adjusting the box size. You can also try setting the box height.

尝试打开autosize文本框。看起来多行试图通过调整字体大小而不是调整框大小来将所有文本放入文本框中。您也可以尝试设置框高度。

TextBox1.AutoSize = True


You can try this too (from MSDN):

你也可以试试这个(来自MSDN):

textBox1.Height = textBox1.PreferredHeight


I tried to duplicate your problem by calling one form from another form and even used a button to dynamically update the text size. I haven't been able to observe your issue. Here is my test code:

我试图通过从另一个表单调用一个表单来复制您的问题,甚至使用一个按钮来动态更新文本大小。我一直没能观察到你的问题。这是我的测试代码:

Behind userform1

在 userform1 后面

Private Sub CommandButton1_Click()
    TextBox1.Font.Size = 11
End Sub

Private Sub UserForm_Initialize()
    TextBox1.Text = Range("A1")
    TextBox1.WordWrap = True
    TextBox1.MultiLine = True
    TextBox1.Font.Size = 28
End Sub

Behind userform2

在 userform2 后面

Private Sub CommandButton1_Click()
  UserForm1.Show
End Sub


EDIT

编辑

I found this postwhich talkes about others having the same issue in Excel 2013. One person wrote that toggling the wordwrap back and forth in the getfocus event solve the issue. I don't have 2013 so I can't test it unfortunately.

我发现这篇文章谈到了其他人在 Excel 2013 中遇到同样的问题。有人写道,在 getfocus 事件中来回切换自动换行可以解决这个问题。我没有 2013,所以很遗憾我无法测试它。

You can try it inside the initialize event or try an activate event like this:

您可以在 initialize 事件中尝试它或尝试像这样的 activate 事件:

Private Sub UserForm_Activate()
    TandC.WordWrap = False
    TandC.WordWrap = True
End Sub

回答by Martin Williams

My program, using visual basic within Excel, works out data then presents it in a textbox. It works perfectly in Excel 2010 but shows only unreadable pixel-high output in Excel 2013 regardless of how the text size is set in the textbox properties section.

我的程序在 Excel 中使用 Visual Basic,计算出数据,然后将其显示在文本框中。它在 Excel 2010 中完美运行,但在 Excel 2013 中仅显示不可读的像素高输出,无论文本框属性部分中的文本大小如何设置。

Using the above comments I found that turning the multiline property off and on immediately before the data is displayed served as a workaround for this infuriating problem.

使用上面的评论,我发现在显示数据之前立即关闭和打开多行属性可以解决这个令人恼火的问题。

My data was collected in a string variable called 'answer', so the final lines of my program read as follows:

我的数据是在一个名为“answer”的字符串变量中收集的,所以我的程序的最后几行如下:

TextBox3.Multiline = False
TextBos3.Multiline = True
TextBox3.Value = answer

End Sub

回答by Tylor Hess

Had the same issue and this fixed it.

有同样的问题,这解决了它。

Private Sub TextBox1_Change()
    Me.txt_Body.WordWrap = False
    Me.txt_Body.WordWrap = True
End Sub

回答by Chris

This has been plaguing me ever since I upgraded to 2013 earlier this year. After many attempts and utilizing what is in this trail along with other investigations, I found a solution that works. The problem is that "focus" needs to be set as well. So the final coding that worked for me is

自从我今年早些时候升级到 2013 年以来,这一直困扰着我。经过多次尝试并利用这条线索中的内容以及其他调查,我找到了一个有效的解决方案。问题是还需要设置“焦点”。所以对我有用的最终编码是

TandC.TextBox1.SetFocus
TandC.TextBox1.MultiLine = False
TandC.TextBox1.MultiLine = True
TandC.TextBox1.WordWrap = False
TandC.TextBox1.WordWrap = True

Thanks everyone. :)

谢谢大家。:)

回答by Bryan Shepherd

I've been struggling with this for the last few hours, but finally found a solution. But first, I found that clicking inside the textbox is what was triggering the font size enlargement. To test it, I repeatedly clicked in the textbox, then clicked on a different cell; I did this 4 or 5 times and quickly realized this was the culprit. The font size increases in the standard MS Office increments (8, 9, 10, 11, 12, 14, etc.).

过去几个小时我一直在努力解决这个问题,但终于找到了解决方案。但首先,我发现在文本框内单击是触发字体大小放大的原因。为了测试它,我反复单击文本框,然后单击不同的单元格;我这样做了 4 到 5 次,很快就意识到这是罪魁祸首。字体大小以标准 MS Office 增量(8、9、10、11、12、14 等)增加。

I also found that the only thing that resets the font size to what it should be was resizing the textbox itself. For some reason, that forces it to reload the font size set in the properties menu, regardless of what is in the VBA code. Once I figured that out, all I had to do was write some simple code that changes the height of the textbox by a pixel or two, then the next line of code changed the height back to its original value. Here's my code:

我还发现唯一将字体大小重置为应有的大小是调整文本框本身的大小。出于某种原因,这会强制它重新加载在属性菜单中设置的字体大小,而不管 VBA 代码中的内容是什么。一旦我弄清楚了,我所要做的就是编写一些简单的代码,将文本框的高度更改一两个像素,然后下一行代码将高度更改回其原始值。这是我的代码:

Private Sub Textbox1_Change()
Textbox1.Font.Size = 10
Textbox1.Height = 20
Textbox1.Height = 18
End Sub

Bam. Problem solved. This code should run every time the value in the textbox's linked cell changes. Hope this helps someone else!

砰。问题解决了。每次文本框的链接单元格中的值发生更改时,都应运行此代码。希望这对其他人有帮助!

回答by cssyphus

For anyone coming to this question from Google and wondering how to change the font size of a textbox or label control, since only the Font family appears to be configurable

对于从 Google 提出这个问题并想知道如何更改文本框或标签控件的字体大小的任何人,因为似乎只有 Font 系列是可配置的

There is a small button with ...just to the right of the font family name - click it.

...字体系列名称右侧有一个小按钮- 单击它。

Screengrab of Font property for a label control

标签控件的 Font 属性的屏幕截图

Otherwise, you can always use code:

否则,您始终可以使用代码:

TextBox1.Font.Size = 18
Label1.Font.Size = 32

回答by Annie Hearts

I had the same problem. In my case none of the solutions above worked. But when I changed the top position of the textbox (e.g. Top 42 instead of Top 45) the font appeared correctly.

我有同样的问题。就我而言,上述解决方案均无效。但是当我更改文本框的顶部位置(例如前 42 位而不是前 45 位)时,字体显示正确。

回答by Meneer

I solved the problem in a different way:

我以不同的方式解决了这个问题:

I created two TextBoxes and filled them with the same content. While running the code I observed which of the two Textboxes showed the unreadable font. On runtime I turned Visibility of that textbox to False.

我创建了两个文本框并用相同的内容填充它们。在运行代码时,我观察到两个文本框中的哪一个显示了不可读的字体。在运行时,我将该文本框的可见性设置为 False。

It may take some trial and error, because the position of the Textboxes on the userform seems to matter.

这可能需要一些试验和错误,因为用户表单上文本框的位置似乎很重要。

Another option is to create a second (identical) textbox and position the one with the wrong fontsize outside the userform.

另一种选择是创建第二个(相同的)文本框并将字体大小错误的文本框放置在用户窗体之外。

Be sure to put the same content in both textboxes and make all attributes identical: enterkeybehavior, multiline, wordwrap

确保在两个文本框中放置相同的内容并使所有属性相同:enterkeybehavior、multiline、wordwrap

回答by Vasil

Just an addendum to what was said here for Word users facing the same problem.

只是此处针对面临相同问题的 Word 用户所说内容的补充。

I was experiencing the same issue and it seems the property TextBox.WordWrapwas the problem.

我遇到了同样的问题,似乎属性TextBox.WordWrap是问题所在。

Here is how you can solve this for Word users:

以下是为 Word 用户解决此问题的方法:

  1. Turn off the WordWrap property of the Text Box in the graphic interface/
  2. Add something like this:

    Private Sub TextBox9_Change()
        TextBox9.WordWrap = True
    End Sub
    
  1. 关闭图形界面中Text Box的WordWrap属性/
  2. 添加如下内容:

    Private Sub TextBox9_Change()
        TextBox9.WordWrap = True
    End Sub
    

It is a workaround, but does not necessarily solve the problem.

这是一种解决方法,但不一定能解决问题。