Auto Size/Adjust Userform - excel

I'm new with VBA Programming, I have a Userform with TextBoxes,Labels, and Checkboxes.
My problem is how to Auto size or adjust the form, Because I have hidden Textbox and Label, Now if I click the Button it will unhide the Texbox and Label and also it will auto size the Form and the other controls.
I don't know how to start this.
Actual Result
Let's assume there is hidden Label and Texbox between Team Name and Last Name now I click the update RadioButton it will unhide the hidden Label and Texbox and adjust the form and other controls.
Expected Result
Is there any way to do this?

Say, the space required by the control Tbx hidden at the level 90pt from the top is 30pt. Therefore all controls below it should be moved down by 30pt when Tbx is made visible, and the form's height also increased by that same measure. The code below would accomplish that while at the same time unhiding the control.
Dim Ctl As MSForms.Controls
For Each Ctl In Me.Controls
With Ctl
If .Top >= 90 Then
If .Visible = True Then
.Top = Top + 30
Else
.Visible = True
End If
End If
End With
Next Ctl
Me.Top = Top + 30
Two things would be different in practice.
You may have to hide the control again.
Your would create the control on the fly rather than keep it hidden.
If you plan on hiding the control again you would need an array of all affected control names and another array (or dimension) for their existing Tops. You would loop through all the names in the array and reset the top, like,
Me.Controls(Arr(0, i)).Top = Arr(1, i) + Iif(HideMe, 0, 30)
Look into creating the control on the fly. You should be able to simply create a copy of an existing control from which it inherits all properties. Any event code for that control needs to be prepared in advance, however.

Related

How to increase the size of a checkbox?

I'm trying to increase the size of a checkbox in my Userform.
In the properties tab, I can change the height and the width of the object but it doesn't change the size of the square. I add a picture to explain my issue.
Thank you.
#Portland Runner's comment is a good suggestion. For example, in the click event of the label (using WingDings 2) ...
Option Explicit
Private Sub Label1_Click()
If Label1.Caption = "Q" Then
Label1.Caption = "R"
Else
Label1.Caption = "Q"
End If
End Sub
There are 2 problems with the VBA checkbox:
The size of the square
The size of the caption text
My solutions:
Create a frame in which you put the checkbox object. A frame has the property Zoom. Set that property at whatever value you want. Then change the font size of the button to match the rest of the fonts in the form. The frame doesn't have to have a title, and you can select an invisible border for it. In that way, the user doesn't see it.
For whatever reason, the checkbox's text looks smaller than the rest of the objects, even though it is the same font size. My solution for this was to remove the checkbox's text and add a standard label object to the right.

VBA Transparent ActiveX object becomes opaque on click [duplicate]

I have a VBA/Excel that user clicks on labels (Active X - Text Label) to perform some actions. The label property is BackStyle Transparent, but when the user click, the label keep opaque, like white or whatever the BackColor property is set.
How can I keep transparent when user click on the label?
Don't use an ActiveX control for this. Any Shape can be assigned to a macro, so instead of having Click event handlers for ActiveX labels like so:
Private Sub Label2_Click()
'do stuff
End Sub
Make the handlers public, give them a meaningful name:
Public Sub BuscaPorPalavraChave()
'do stuff
End Sub
Replace the labels with TextBox shapes - make the shape fill and border transparent, right-click the shape, and select "assign macro" - then pick BuscaPorPalavraChavre. Done!
Rinse & Repeat for every label. I know, painful - but worth it!
That navigation UI looks very nice BTW =)
I came up with a different solution in case you have faced a related problem, like the transparency/opaque problem when executing a macro hovering over a transparent label but that becomes opaque when clicking on it.
The workaround consists basically in changing the visibility status when hovering in and out. It might be counterintuitive at first (Because you are making disappear the same label you are using to execute the macro) but it works really well. Let's assume we have two Active X labels overlap. Label1 is the bigger one and label2 is the smaller one, completely contained in Label1. The code I used is like:
Private Sub Label2_MouseMove(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single)
Application.ScreenUpdating = False
Label2.Visible = False
Label1.Visible = True
### Put your code in here
Application.ScreenUpdating = True
End Sub
Then as you hover out label2, and assuming the labels are correctly overlap, you will hover over Label 1, now visible because you "activated it", and the following code is executed
Private Sub Label1_MouseMove(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single)
Application.ScreenUpdating = False
Label2.Visible = True
Label1.Visible = False
### Put your code in here
Application.ScreenUpdating = True
End Sub
Please bear in mind that you need to have a figure or button in between the two Active x labels, otherwise you will get a strange result because in the exact moment you hover over Label 2, it will disappear, leaving Label 1 beneath it which will also disappear as soon as you move, makin Label 2 appear again and ... you get the idea. This will not break the excel file but will make it make strange things like blinking or delaying the mouse movement. To prevent thus. I recommend to have that shape as a safe zone.
Hope this helps.

VBA code to reset command button sizes and place them at specific named ranges

I'm using Excel 2010 - I have a number of named Command Buttons and Toggle Buttons that tend to move around or get set to a height and width of 0 so I want to add vba code to reset the command buttons and toggle buttons sizes and locate them at a specific named range.
For example names cb1, cb2, cb3, cb4 to be set at range names cbrn1, cbrn2, cbrn3, cbrn4 respectively with a width of 20 and height of 20 and tb1, tb2, tb2, tb4 to be set at range names tbrn1, tbrn2, tbrn3, tbrn4 with a width of 10 and height of 10. How would I do this by using an if then loop?
Your help would be more than appreciated :-)
I am not sure exactally on your question. But I am just trying to be helpful. You are trying to set the sizes in code? you can make a default size function size(20,20) for the typeof(control) If I had a small code clip I could be more helpful. you said 2010 so I am assuming .net even though VBA usually refers to pre-.net ie vb 4,5,6
For each obj as Control in Me.Controls
Select case typeof obj
if typeof (obj) is ComboBox then
obj.size = (20,20)
elseif typeof(obj) is ToggleButton then
obj.size = (0,0)
end if
next
tried to use spaces there to make it more readable. This code is not exact. but if I am correct on the type of question you are asking this "pseudo-code" should help it did not format correct hope it does this time if not. should still get the idea

How do I make an Excel ActiveX label to be transparent ... at runtime?

I want to put a transparent label on top of a sheet in Excel, so that I can take advantage of the MouseMove event of the label to "draw cells" (aka change their fill color and so on) by mouse click / drag / etc. - since I can't do that on the cells per se.
Now everything works just fine, except that I can't make the label transparent at runtime (aka in VBA) ... while by doing exactly the same thing in Design Mode works as expected. Specifically, I have the code (more or less):
Dim MapLabel As OLEObject
On Error Resume Next
Sheet2.OLEObjects("MapLabel").Delete
Set MapLabel = Sheet2.OLEObjects.Add("Forms.Label.1")
MapLabel.name = "MapLabel"
MapLabel.Placement = xlMoveAndSize
MapLabel.Object.Caption = ""
' Problem line below
MapLabel.Object.BackStyle = fmBackStyleTransparent
' Problem line above
MapLabel.Left = Sheet2.cells(2, 6).Left
MapLabel.Top = Sheet2.cells(2, 6).Top
MapLabel.Width = Sheet2.cells(2,6).Width * 10
MapLabel.Height = Sheet2.cells(2,6).Height * 10
So, in words, I first delete the label named 'MapLabel', then recreate it (the above code goes into a "init" Sub). All the code lines except the one marked produce the desired result. The marked one does set the BackStyle property of the label to fmBackStyleTransparent ... but it doesn't actually make the label transparent. This is frustrating, because it's the same approach that works flawlessly at design time!
Do you have a solution to this? I read about solving similar problems by declaring the label as MsForms.Label or as Control, but the sheet object doesn't have those properties, plus, there are far more label properties which can be set using the OLEObject than with the help of MsForms.Label or Control.
All you need to do after this line:
MapLabel.Object.BackStyle = fmBackStyleTransparent
put this line:
ActiveSheet.Shapes(MapLabel.Name).Fill.Transparency = 1
I hope I helped.
P.S. If you need explanation i will edit my answer.
I had the same problem as you but in Word. The solution for me was to do the following:
In design mode:
Right click on the object
Navigate to Switch to automatic form/Image > Wrapping > In front of the text
Add an empty picture to your label

How to fix an Excel listbox that can't scroll the last element into view

A killer problem I've had in excel UIs since as long as I can remember, is with listbox scrolling.
When you have more elements in a listbox that can be displayed, a scoll bar will appear. In certain conditions, however, scrolling the bar all the way to the bottom of the list and releasing it, will "jump" the bar a notch upwards, and you won't be able to see the last item in the list. This is illustrated here:
There are many forum posts presenting this issue, and the solution has always been "Set the integral height property to false, and then set it to true again." What this does is slightly resize the listbox so that it the height is rounded to the height of a single row, and then no items are left hidden.
With lstbox
.IntegralHeight = False
.Height = myHeight
.IntegralHeight = True
End With
There are certain cases, however, where this does not work. If you are:
Programatically setting the height of your listbox
NOT using simple listbox selection (fmMultiSelectSingle)
Then simply setting integral height to false and then true after or between changes to height will make an adjustment to the height of your listbox, but when you go to scroll down, the problem will remain - the last item cannot be seen.
The key to this frustrating question is that while everyone else on the internet is confirming that the 'integralHeight' solution works for them, these very special cases are frustrated wondering why it doesn't work for them. So how do they get their fix?
Something I had to discover for myself, and which cannot be found anywhere else (which is why I'm posting it here), is that this problem had the added dimension of being dependent on the selection method. While I cannot fathom how the way the scroll bar works is related to not only the height and integral height property, but also the .MultiSelect property, I have found that it is.
When the .IntegralHeight method shown above does not work, the following method somehow does:
With lstbox
.IntegralHeight = False
.Height = myHeight
.IntegralHeight = True
.MultiSelect = fmMultiSelectSingle
.MultiSelect = fmMultiSelectExtended
End With
By simply changing the .MultiSelect property to fmMultiSelectSingle, and then restoring it back to the selection style desired, the height of the listbox will be automatically adjusted by the .IntegralHeight property to a slightly different height than when these actions aren't performed - the difference results in the scroll bar working correctly:
I hope the discovery of this special case and more precise workaround saves someone the hours of frustration and experimentation I had to go through.
i know this is very old post.
but i've been through a lot to fix this problem, so i just wanna share my tip. :)
first of all,
integralheight method doesn't work when worksheet zoom level is not 100%.
it will change listbox height and width, location, etc. (even if you set object property 'doesn't move or reseize with cell')
and when you try to take it its original size and location with code to fix this, this time its last item can't be seen
my tip is simple.
there's combination between font size and listbox height.
if your font size is 6-10(arial, regular), listbox height goes well with multiples of 12.75 (btw my list box style is 1 : ListStyle, 1-fmListStyleOption. it could be different with style 0)
as long as height is same with these multiples of 12.75, there will be no problem.
in case of font size 12(arial, regular), it's multiples of 13.55
so if there's no restiction about listbox size in your project, just resizing it slightly depending on your font size gives more comfort. :)
I had to anchor the position since my ListBox was walking across the page:
With ListBox1
.IntegralHeight = False
.IntegralHeight = True
.Height = 45
.Width = 69
.Top = 0
.Left = 1255.5
End With
With lstbox
`.Height = myHeight`
`.MultiSelect = fmMultiSelectExtended`
`.MultiSelect = fmMultiSelectSingle`
End With
This worked for me. No need of setting Integral height property
In my case the solution was this method:
with listbox
.IntegralHeight = False
.Height = myHeight
.Width = myWidth
.IntegralHeight = True
.Height = myHeight
.Width = myWidth
end with
Enjoy.
found ridiculously simple way to resolve this issue. adjust your height up or down a little bit so bottom line of list box is between check boxes, then you can scroll down to last item even if IntegralHeight is set to false
Thanks Alain. Your fix worked well for me.
I found a subsequent problem related to the height of the ListBox when resized, that it varied in an unpredictable way depending on the initial height. The resized height was different again when displayed on another machine with 125% text scaling. For example, if I set a height between 358 and 370, the resized height is either 370.65 or 371.4 on my machine but on the machine with 125% text scaling, it is 360.1, 370.25 or 380.45. With such large variability, the result was that the ListBox could obscure other controls below it.
The fix was to start with the maximum height I wanted and reduce the initial height until the resized height was less than the maximum height I wanted. I do this whenever I display that ListBox.
Hmax = 372 'Target Height
H1 = Hmax
With SteelForm.Controls.Item("ListBox1")
Do
H1 = H1 - 1
.IntegralHeight = False
.Height = H1
.IntegralHeight = True
.MultiSelect = fmMultiSelectSingle
.MultiSelect = fmMultiSelectExtended
DoEvents
Loop Until .Height < Hmax
End With
What I've seen in the past on forums is just adding an extra blank row to your list box. That should do it.
Just set the Integral Height property to True

Resources