How to increase the size of a checkbox? - excel

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.

Related

Auto Size/Adjust Userform

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.

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

Marlett font missing from VBA object properties

I want to place a green tick mark on a userform and thought that creating a label with the caption "a" in Marlett font would do the trick. However, Marlett isn't showing up in the object properties despite definitely being installed. I can easily place the tick mark in a cell, but not in a label. Is there a way to enable it for userforms?
There is an answer to the same problem in the c# section - How do I set button font to Marlett, but I'm not sure if it can be applied to VBA as well.
It's as simple as putting:
Label1.Font.Name = "Marlett"
Label1.Caption = "a"
(important: this code only seems to work if you set the font before setting the caption; setting the caption first seems to prevent setting the font from having an effect - thanks to #N.N.Thoughts for the info)
in the Userform_Initialize event of your userform. Replace Label1 with the name of the actual control
edit: with the addition of code to set the font size and colour:
Label1.Font.Size = "32"
Label1.ForeColor = RGB(0, 255, 0)

In Excel, VBA - How can we lock resizing of a rectangle

I have an excel sheet that contains two rectangles and text in other cells.
I need to allow users to only edit the text in the rectangle. They should not be able to change the size of the object.
Applying lock on the rectangle locks the object as well as the text.
Does anyone know how I can achieve this?
Why not create two objects, one being a rectangle that is locked, and one being a text box that is not locked? This is really simplistic, but a possible answer.
Another idea would be to have the rectangle equal a set cell, and let them enter their text in the cell and it would transfer over even when the rectangle is locked.
As far as I am aware Excel does not accommodate Events for shapes and so there is no simple way of detecting a change in a shape size and then resizing the shape.
It is possible to emulate what you are asking for by using a workaround.
Imagine you have two rectangles on your spreadsheet called 'Rectangle 1 and 'Rectangle 2'. When a user finishes updating the text in any given box they must then click the spreadsheet to move out of 'edit' mode for the shape. You can detect this using the Workbook_SheetSelectionChange event.
The following module allows you to set the size of the rectangles as constants and will resize the rectangles accordingly:
Const Rect1Height As Integer = 50
Const Rect1Width As Integer = 200
Const Rect2Height As Integer = 50
Const Rect2Width As Integer = 200
Sub SetRectangleSize()
Dim Rect1 As Shape
Dim Rect2 As Shape
Set Rect1 = ActiveSheet.Shapes("Rectangle 1")
Set Rect2 = ActiveSheet.Shapes("Rectangle 2")
Rect1.Height = Rect1Height
Rect1.Width = Rect1Width
Rect2.Height = Rect1Height
Rect2.Width = Rect1Width
End Sub
Now all you need to do is to call this sub from a workbook level event:
Private Sub Workbook_SheetSelectionChange(ByVal Sh As Object, ByVal Target As Range)
SetRectangleSize
End Sub
Each time a user updates the text in one of the rectangles they will click back on the spreadsheet and the event is fired, resulting in the rectangles being sized according to the constant height and width parameters that you have defined.

Resources