Name property of dynamically created textboxes - excel

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

Related

Excel VBA - Save Userform Changes

Is it possible to save changes to a Label Caption in a userform in Excel VBA, so that they are permanently saved, and only changed when you enter a new change?
I have checked, that the code is changing the caption, but I cannot get it to stick, so that it is still there next time I open the userform.
Thank you in advance
Private Sub cmdSubmit_Click()
'resets participants email and name
If Me.optProg.Value = True Then
Me.NameLabelProg.Caption = Me.CB_Part.Value
Me.MailLabelProg.Caption = Me.TB_Mail.Value
ElseIf Me.optTester.Value = True Then
Me.MailLabelTest.Caption = Me.CB_Part.Value
Me.NameLabelTest.Caption = Me.TB_Mail.Value
End If
End Sub
Check out this userform example and you'll find answers to many of your questions.
Source of userform
Forgive the lengthy answer initially, but there is a bit of a risk involved with making permanent changes to UserForms through VBA.
In order to change the caption of a Label (or UserForm or any other Control permanently, you will have to "Trust access to the VBA project object model" in order to do it via VBA code. Now, while this is possible it is not usually recommended because it can seriously put a user's PC at Risk should they encounter a Macro developed for nefarious purposes.
(To clarify in case the question is raised, your end user(s) will have to make this Trust setting change on their PC's as well . . . you cannot make the change on your PC, setup the code to work and then hand the file over to another user and have it work on their PC without them making the same change.)
There are methods to do this programmatically, but, this falls into the "nefarious Macro" rabbit hole and would need to be disclosed to the end user you are making this change. . . Research at your own risk.
If you are OK with putting yourself at risk, you can do it using VBA similar to the following snippet I found here. You will have to substitute your UserForm name and Label name as appropriate. I tested it on my own UserForm and it works as expected.
Sub Change_Userform()
ThisDocument.VBProject.VBComponents("Userform1").Designer.Controls("Label1").Caption = "Some new caption text"
End Sub
You will need to do some research on how to "Trust access to the VBA project object model" yourself and understand the risks to do this in order for the above code to work.
If I understand the intent of what you are trying to accomplish correctly though, you could achieve this effect without having to put yourself or your end user(s) at risk.
(Most end user(s) typically would not have direct access to the VBA Designer where they would see the UserForm's un-initialized environment.) To do this, you would have to place your code in the UserForm's event.
The following assumes your Me.optProg.Value and Me.optTester.Value are Option Buttons which a user would change. If you create a "Settings" sheet in the file, you can place values in the cells of this sheet and then hide it so the users do not modify them directly. Then, reference the values of the cells and change the appearances of the Option Buttons at the same time as the UserForm's launch. (Additionally, you can set the Click events of the Option Buttons to change the values of the same cells and provide that change to affect the UserForm's Initialize event when called, but this should get you going in the right direction.)
Sub UserForm_Initialize()
'The Range below is completely up to you.
'Since you are using Boolean True/False, a simple "1" or "0" _
is easy to use to make the changes.
If Thisworkbook.Sheets("Some_Settings").Range("A1").Value = "1" Then
Me.optProg.Value = True
Else
Me.optTester.Value = True
End If
'do some other code here as needed to finish initializing the UserForm
If Me.optProg.Value = True Then
Me.NameLabelProg.Caption = Me.CB_Part.Value
Me.MailLabelProg.Caption = Me.TB_Mail.Value
ElseIf Me.optTester.Value = True Then
Me.MailLabelTest.Caption = Me.CB_Part.Value
Me.NameLabelTest.Caption = Me.TB_Mail.Value
End If
End Sub

Rowsource not working via VBA

I can't get the RowSource property of a list box to update via VBA. From another thread, I found the syntax, so I think this is correct. But, despite not failing, it doesn't do anything to the RowSource property (it remains blank). Below:
frmAddIngredient is the user form.
lbxIngredient is a listbox control in that form.
UniqueIngredients is one of the sheets in the workbook.
NumberOfItems is 1 (in this case).
It doesn't give an error, but it doesn't change anything, either. The form itself is not active at this time. This code is supposed to set up the form for later showing.
frmAddIngredient.lbxIngredient.RowSource = Sheets("UniqueIngredients").Range("A1:A" & CStr(NumberOfItems)).Address
The most recent code is
frmAddIngredient.lbxIngredient.RowSource = "=UniqueIngredients!A1:A1"
but, it still doesn't change anything in the actual form.
Also, can I add a new post, or do I have to continue editing this one and adding stuff?
What you want (as discussed in the comments on your question) is not possible. Setting something by code does not change it's property in the properties window and is only so until your project resets.
Consider a Userform with 2 buttons, with their original name and caption and then in a module paste these 2 subs.
Sub demo1()
UserForm1.CommandButton1.Caption = "Demo 1"
UserForm1.Show
End Sub
Sub demo2()
UserForm1.CommandButton2.Caption = "Demo 2"
UserForm1.Show
End Sub
When you run the first Sub demo1 Button 1's caption has changed but Button 2's caption has not.
Close the Userfom and now run demo2, you'll see that Button 1's caption is back to it's original hard set (properties window) name and that now Button 2 has a different name.

VBA - How to retrieve value from one TextBox and make it appear in another?

I have Userform in Excel.
The first tab of the multipage has a variable called Staffwage_TextBox. Someone is expected to enter a value.
I want to retrieve this value and make it appear on the next multipage in a TextBox called staffwage.
I have this code so far:
Private Sub staffwage_Change()
Dim trial As String
Staffwage_TextBox.Characters.Text
staffwage = Staffwage_TextBox.Value
trial = Staffwage_TextBox.Characters.Text
End Sub
Any help would be appreciated,
Thank you :)
If I got you right, You have a multipage and tab 1 and 2 hold a textbox and if something is entered in the first textbox, you want it to appear also in the second one.
First things first: Elements within the multipage are treated like there is no multipage. So it doesn't matter where they are.
While I'm confused by: ...a variable called Staffwage_TextBox.... I'm sure that is an object.
To make it as easy as possible I will use different names.
I created a multipage and added a textbox at both pages. Named TextBox1 (at page 1) and TextBox2 (at page 2). Now I double click TextBox1 and get moved to the sub for a change event (looks like you are at this point). Now I simply put in there:
TextBox2.Value = TextBox1.Value
And that is all. If i now put anything in the first text box, then the second one will have the same text. (Only works in that direction)

Store location of command button cell address to variable in VBA

Okay so I'm using Excel and trying to get a cell address based off the location of the command button I'm clicking. I want to store that cell address in a variable as I click the button to be used in the next code for the user form that pops up as a result of clicking that button. Ive looked at this link: Store location of cell address to variable in VBA , which is similar to what I'm trying to do but I cant seem to get it to work with the ActiveSheet.Shapes(Application.Caller).TopLeftCell.Address that gives an address based off the top left corner of the command button. I'm trying to do it this way so I can use the same code from the next part for every similar button I add without having to manually change the code based off where the button is.
I'm pretty new when it comes to Excel and VBA so any help would be appreciated.
Edit:
I tried adding the code but I'm getting a Runtime error '1004' Unable to get Buttons property of the Worksheet class. I have this
Sub Button2_Click()
Dim BtRng As Range
Set Btnrng = ActiveSheet.Buttons(Application.Caller).TopLeftCell
MsgBox Btnrng.Address
Btnrng.Offset(3, 3) = "Hello"
formAddBill.Show
End Sub
This button is also popping up a userform for information input. the error is happening on Set Btnrng = ActiveSheet.Buttons(Application.Caller).TopLeftCell
If you are using application.caller then I assume your buttons are from the forms.control toolbox. This makes life a lot easier.
Assign each button with this code example:
Sub Rng_Butn_Clicked()
Dim BtRng As Range
Set Btnrng = ActiveSheet.Buttons(Application.Caller).TopLeftCell
MsgBox Btnrng.Address
Btnrng.Offset(3, 3) = "Hello"
End Sub
If you are using command buttons from the activex toolbar, it can get a bit confusing. Doing it one at a time is easy enough
Private Sub CommandButton1_Click()
Me.CommandButton1.TopLeftCell.Offset(0, 1) = "Hi"
Me.CommandButton1.TopLeftCell.Offset(, 2).Interior.Color = vbBlue
MsgBox Me.CommandButton1.TopLeftCell.Address
End Sub
But assigning one code to many activex command buttons is not as easy, I would prefer to use the Forms Buttons if you want to assign one code to many buttons.
Edit: If you are getting this error
You probably put the code in a command button. Please reread my answer.
This is what I am referring to:Form Controls & ActiveX Controls

VBA UserForm Dynamic Resize Event with Multipages

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

Resources