VBA UserForm Dynamic Resize Event with Multipages - excel

Good Morning!
I started off by reading this :
http://www.andypope.info/vba/resizeform.htm
And while this was very informative, I was wondering if someone one would be able to point me in the right direction to help being able to solve my inquiry. I want to dynamically set height & width values of a userform depending on what multipage selected (presumably by click event). Would it be something like this?
Sub pageX_click
height.value = 23
width.value = 50
End Sub
I assume it might be more complicated than that, but if someone would be willing to point me in the right direction I can tinker till i find the correct solution.
Other question- due to the differing sizes, would I need to statically set which pages is opened each time? That way i dont get random size issues/errors?

Here is the solution- it was literally as simple as I thought
Private Sub MultiPage1_Change()
If MultiPage1.Value = 0 Then
ToolBoxForm.Height = 560.25
ToolBoxForm.Width = 652.5
End If
'lather, rinse, repeat for each page
End Sub

Related

Change of a textbox.name in a form by code

I have a Form in an Excel VBA project that has many textboxes with names I'd need to change. For instance, names in the projects are: Fees01TBX; Fees02TBX; Fees03TBX, and so on. There are 40 textboxes like these, and I need to change the names into a sequence I can manage better. For instance, I'd need to have them with these names: Expenses01; Expenses02, Expenses03, and so on.
I tried to change it using some code like (just for one textbox):
Sub ChgName()
MyForm.Fees01TBX.Name = "Expenses01"
'Doing this for each one would not be a problem, I can array the sequence.
End Sub
I think it should possible to change the name of a textbox in a form by code, but how?
Make sure you ticked Trust Access to the VBA Project Object Model in security settings:
Sub Rename()
With ThisWorkbook.VBProject.VBComponents("MyForm")
.Designer.Controls("Fees01TBX").Name = "Expenses01"
End With
End Sub
But if Fees01TBX had code you have to change this as well, but for 40 TextBoxes you could do it manually

Text inside ActiveX textbox 'stretches' with move & size with cells

On my worksheet there are ActiveX textboxes, which I have sized so they fit the cell they are in.
I have set object positioning to move and size with cells.
When I change the size of the cells, the textboxes resize as intended, but the text inside the box 'stretches' instead of remaining the same.
Well you obviously run into one of the numerous ActiveX bugs. I can only recommend to stay far away from ActiveX, as they are well known to cause odd issues and numerous bugs.
As solution I suggest to ask Microsoft to fix the bug and/or in the meanwhile use the following workaround after you resized columns.
There is a workaround, that could fix the odd stretch looking bug:
You just need to resize the TextBox with VBA like TextBox1.Width = TextBox1.Width and everything looks smooth again.
To fix all TextBoxes just loop through all of them and reset their width:
Option Explicit
Public Sub FixOddTextBoxesAfterColumnResize()
Dim obj As OLEObject
For Each obj In ActiveSheet.OLEObjects
If obj.progID = "Forms.TextBox.1" Then
obj.Width = obj.Width
End If
Next obj
End Sub

How to bring top a specific textbox

I have a userform that contains a lot of objects.I am trying to widen some of the textboxs depending on text lenght and upon exit return textbox to it's original size.My problem is when i change the width,textbox next to it stays on front.I tried to find something on object properties that would help but nothing worked.Thanks for the help beforehand.
You need to change the Z order:
Textbox1.ZOrder msoBringToFront
for example.

Name property of dynamically created textboxes

First a little background information:
I am working on a project in Excel. When you open the file, a userform pops up and asks for input in multiple textboxes. It then puts the input in certain cells in the Excel document. Then another userform pops up, and asks for more input, which is inserted in cells as before.
Next, it requests numerical input in textboxes, and this data inserts the respective number of rows under each category in the Excel document.
Now for the problem.
The next thing I want to do is have a userform which dynamically creates a certain number of labels and textboxes based on the numbers inputted by the user in the previous userform. I have already figured out how to bring the variables from one userform to another; now I am having issues with the naming of the dynamically created textboxes. Well, really I know how to name them, but can't figure out how to use the textboxes' .Text attributes as input to cells in the Excel document. I have spent upwards of six hours searching the internet for an explanation of how to do this that I can apply to my project, so now I'm looking for personalized help.
Here's the options I have considered:
Arrays
Controls.Add("txtBoxName" & i) in a For...Next loop
I have actually tried the For...Next loop with Controls.Add,
and had absolutely no luck.
The problem with both of these, while Arrays may be more convenient in the long run, is that (at least as I see it) with both you have to name the textboxes and then call the names, which is a syntax I cannot find anywhere.
Can anyone help? I have no problem posting the relevant code if need be.
Also, if I have overlooked something, feel free to point me in the right direction. I may ask for help understanding it, though! ;)
Thanks,
Dudebird47
Simple example of a button added at run time with code to handle its click event:
Option Explicit
Private WithEvents btn As MSForms.CommandButton
Private Sub btn_Click()
MsgBox "You clicked me"
End Sub
Private Sub UserForm_Initialize()
Set btn = Me.Controls.Add("Forms.CommandButton.1", "some_button", True)
With btn
.Top = 5
.Left = 5
.Width = 75
.Height = 50
.Caption = "click me"
End With
End Sub

Losing ActiveCell.Value when closing TextBox from which value copied

So I'm using VBA UserForm in Excel to capture values. Problem is that after assigning value to ActiveCell and after value initially appearing in cell, The value disappears when I Close TextBox. I Think I Understand why this happens, just Don't know how to preserve value after closing TextBox. Thanks in Advance.
Paul L
Thanks astander
Thanks for responding but i figured out how to avoid problem . A solution is to avoid having to close TextBox indefinately. Doing this by using modal parameter of Show method as shown below
UserForm1.Show (vbModeless)
Not most elegant solution maybe but it works for now. Anyway I am still at Tinkering stage of working with VBA. Trying to teach myself using book and online resources.Thanks again
Paul L
Is your textbox bound to a cell? If so, just avoiud that and drop the textbox value into the cell manually.
I found the easiest way to do this is with variables. Eg
Sub textmove()
Dim textdata
textdata = txtbox.text
Range("A1").value = textdata
End Sub
Not sure if this helps, but I have always found it easier to do this in two stages (get the data, then assign the data to a cell). Im sure there is a more correct and efficient way to do it, however this has never failed me!

Resources