Adapt label width to text - vba

I basically programmatically generate many labels with a text read from a text file so I would like to know how I can programmatically adapt the width of a Label to its text.

Controls come in two flavors. The ActiveX version of a label has an AutoSize property. For example, with a ActiveX label control named Label1 in Sheet1
Private Sub test()
Sheet1.Label1.WordWrap = False
Sheet1.Label1.AutoSize = True
Sheet1.Label1.Caption = "This is a lot of text to put in a label"
End Sub
will automatically adjust the width to fit.

Related

VBA Hint Text in UserForm TextBox

How to add a hint text to a TextBox in a UserForm that will disappear once a user type anything in?
Add a Label element.
Type a hint text.
Set BackColor, Height, Left, Top, and Width properties to match that of the TextBox that will be added later.
Note: Setting BackColor to Window Background (from the drop-down list) will match the common background color of a text box.
Set ForeColor property (a different from TextBox text color allows to distinguish the hint text from the entered one).
Add a TextBox element.
Set BackColor, Height, Left, Top, and Width properties.
Set BackStyle property to fmBackStyleTransparent.
Add the following code to Sub TextBox_Change:
If TextBox.Value = "" Then
TextBox.BackStyle = fmBackStyleTransparent
Else
TextBox.BackStyle = fmBackStyleOpaque
End If
Here is the result:

Control Tip Text on dual monitors?

I have a small user community for an application that contains a lot of user forms and fields.
I have added ControlTipText on most of the fields as helper/reminder.
The problem occurs when the user form is opened/displayed on the secondary monitor, the yellow stuff
(ControlTipText) is popping up on the right edge of the primary monitor.
Not a huge issue, but annoying.
Anyone who has a fix for this?
Same issue, resolved by:
Creating a label with a distinctive background colour positioned over the control that you want to write tip text for
Initialise the UF with the label visible = false, e.g. Userform1.label1.visible = false
use the "mousemove" event to write code to show the label
Private Sub TextBox1_MouseMove(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single)
label1.visible = true
'position label as desired
End Sub
use the click or mousemove event on the label this time to banish the tool tip. You will have lots of labels over controls in the VB editor.
As an alternative I creatie a label with an "Information" icon that is clickable to unhide the tooltip label rather than mousemove event sometimes
As ControlTipText is a property of the Text Box it doesn't have any members which would allow you to adjust it's position.
It seems like the only way the issue can be resolved is by viewing Excel on your primary monitor. If it's useful, you can force Excel to move to the primary monitor with something like this:
With Application
.WindowState = xlNormal
.Left = 0
.Top = 0
.WindowState = xlMaximized
End With

Excel VBA changing properties of Form Control on Chart Tab

I have added an Label on my Chart tab, but when I try to change it's font all of the fields are greyed out. I have searched enough but didn't find any solution. I am attaching a screenshot of my chart tab.
I want to change the font properties of this Label 10 on the chart tab. Is there a way I can access the properties by VBA.
To change the text I recorded a macro.
ActiveChart.Shapes("Label 10").Select
Selection.Characters.Text = "Mohit"
ActiveChart.ChartArea.Select
I tried the line code:
Selection.TextFrame.Characters.Font.Size = 20
but it doesn't work. is there a way to change the color and font size of this label ?
Alternate that I am using now
Now I am using Rectangle shape instead of Label. What I wanted to acheive was to display the name of the selected button ( PV , Terrorism or SRCC ) as an indication that this button have been selected. So I clicked PV and my series changed and Rectangle displayed the name of selected Peril.
Now if I select SRCC again the same thing happens.
For the code I only have to change the text of the rectangle every-time a button is pressed as I preset the font and size of text once which remains the same. I wasn't able to change the font and size of text with a label.
ActiveChart.Shapes("Label 10").Select
With Selection
.Characters.Text = "Mohit"
.Font.Size = 20
End With
or you can avoid Select/Selection accessing your Chart object by its name:
With Charts("ChartName").Shapes("Label 10").TextFrame2.TextRange
.Text = "Mohit"
.Font.Size = 20
End With

Access VBA - Hide Labels, text box, buttons If = " "

I'm trying to hide make some labels, text boxes and buttons hidden:
If rst![RI] = "" Or IsNull(rst![RI]) Then
I have:
A Label named "Label83"
A Text box named "C1"
A Text box named "Tex4"
A Text box named "Text8"
A button named "Command18"
So whenever I'm on PM200 and If rst![RI] = "" Or IsNull(rst![RI])
Then the listed label, text boxes and button should be hidden.
Thank you in advance.
You can use as a minimum:
Me!Label83.Visible = Len(Nz(rst![RI].Value))
or, to play nice:
Me!Label83.Visible = CBool(Len(Nz(rst![RI].Value)))
For more controls, set a variable:
Dim Visible As Boolean
Visible = CBool(Len(Nz(rst![RI].Value)))
Me!Label83.Visible = Visible
Me!text8.Visible = Visible
' etc.
And do rename your controls to something meaningful, like: lblCtransfer

Word CheckBox ContentContol OnChange Event

I try to make a word document with two checkboxes where each checkbox will show/hide a part of the document with a custom style.
I plan to set value Style.Font.Hidden = True/False depending from checkbox values, but...
I found that there are 3 types of controls:
Legacy Controls - This seems old the ugly.
ActiveX Controls - I can easyly attach to checkbox onChange events, but these are also ugly and I think it's not that secure, also this is probably now working on mac.
ContentControls - This seems like the right way to do this, but I just can't attach to the right event. (Also there is some XML attachment described, but I'm not using this, this seem too complicated, I don't know.)
Can you tell me how to atach to onChange event of CheckBox ContentContol? I need the same behaviour like it's ActiveX CheckBox.
Content Controls do not have "onChange" events, so you can't get a content control to behave like the ActiveX checkbox in a simple manner. Similarly to form fields, the code for ContentControls fires when the control is entered/exited.
The only way to emulate "onChange" for a Content Control is to link the content control to a node in a CustomXMLPart in the document then work with the Document_ContentControlBeforeStoreUpdate event that triggers when the content of the node in the CustomXMLPart is going to be changed.
If, as your question indicates, this is too complex for your purposes you could use a MacroButton field that displays a font character (Symbol) that looks like a checkbox. Clicking the field would exchange that character for a different one, that looks checked. And the reverse again for the next click. Here's some sample code to get you started. If you don't like the checkboxes I chose, you can pick something else from Insert/Symbols/Symbol. Just change the character numbers and the font name.
By default a MacroButton field triggers on double click. You can change this to a single click when the document is opened in an AutoOpen macro.
Sub AutoOpen()
Application.Options.ButtonFieldClicks = 1
End Sub
Sub ToggleCheckBox()
Dim iNotChecked As Integer, iChecked As Integer
Dim rngCheck As word.Range
Dim sBkmName As String, sFontName as String
iNotChecked = 111
iChecked = 253
sBkmName = "bkmCheck"
sFontName = "Wingdings"
Set rngCheck = ActiveDocument.Bookmarks(sBkmName).Range
If Asc(rngCheck.Text) = iNotChecked Then
rngCheck.Text = Chr(iChecked)
ActiveDocument.Bookmarks.Add sBkmName, rngCheck
rngCheck.Font.Name = sFontName
ElseIf Asc(rngCheck.Text) = iChecked Then
rngCheck.Text = Chr(iNotChecked)
rngCheck.Font.Name = sFontName
ActiveDocument.Bookmarks.Add sBkmName, rngCheck
End If
End Sub