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
Related
code screenshot
I've tried several different methods and nothing seems to be working.
One way to make sure you are targeting the right object, is by selecting it in the dropdown list.
Then the Private Sub UserForm_Initialize() sub will be created automatically, and is sure to point in the right direction.
The thing about specifically the UserForm object, compared to the others, is that it will always be called UserForm no matter what the name is, because there will always only be one userform in a userform.
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
So I am currently studying SQL Server but right now I am just working a standard office admin job while I'm studying.
I never really made macro's before and little knowledge on VB but decided to design a macro for work to make things a bit easier for my team.
The macro just very simply allows the user to enter data, stats etc and gives the percentage or average statistic resulting in a total letting the user know if the statistics have been hit that day, week, month etc.
It works well but I would like to add a "SUBMIT" button that when a user clicked it would send the data they have entered in specified cells to myself. I am not sure how to go about it, If needed I don't have access to systems like SQL, Visual Studio etc in work as said just basic admin job at the moment.
Would It need to be submitted as a CSV? or could it be submitted from the user's sheet straight onto another macro I have designed giving the results for the whole team? As said I am totally new to this idea.
Cheers Guys.
Awright, according to what you may need in a very simple approach, the first thing you need to do it's to know the cells where they're going to enter info (care with ranges ), let's assume for this example that whe only had one data entered in the first cell of the team worksheet. So, create a button called 'button1' or as you wish and on the click event use this code :
Private Sub button1_click()
Teamsheet.Cells(row,column) = Yoursheet.Cells(destinyrow,destinycolumn)
End Sub
That would copy the value from one sheet to another, now, if you had you sheet locked via password, you must unlock it before doing that,then lock it again so code would be like this :
Private Sub button1_click()
On Error Resume Next
yoursheet.unprotect password:="yourpassword"
Teamsheet.Cells(row,column) = Yoursheet.Cells(destinyrow,destinycolumn)
On Error Resume Next
yoursheet.PROTECT password:="yourpassword"
End Sub
I clarify that this is a very simple approach, so, if you're using specific cells you can copy one by one and this would do (so you can make anny calculation son your admin sheet), but when you're copying ranges should be like this :
Teamsheet.Range("A1:D3").Value = yoursheet.Range("A1:D3").Value
Also, always consider how they enter this data you need.
UPDATE :
Let's say you have a team workbook and yours is admin_workbook, concept it's similar. This code will do what you need but both workbooks should be at the same folder or path :
Private Sub button1_click()
Var_data = Teamsheet.Cells(row,column)
Application.ScreenUpdating = False
Workbooks.Open Filename:=ThisWorkbook.Path & "\admin_workbook.xls"
ThisWorkbook.Activate
Admin_sheet.Cells(destinyrow,destinycolumn) = var_data
Workbooks("admin_workbook.xls").Close SaveChanges:=True
Application.ScreenUpdating = True
End Sub
First you capture data on a var, then you open your admin book, put the data on the cell you want and close that workbook saving changes (you decide if you keep this line or mantain the workbook open and save manually). Also, Application.screenupdating it's a line that helps your screen doesn't flick when changing between workbooks.
Hope it helps friend !
I inherited a workbook from someone who is long gone. They put a button on the dashboard that activates the following code.
Private Sub CommandButton2_Click()
Sheet5.ShowDataForm
End Sub
It brings up a form with all the elements/columns on that sheet, with all the buttons need to put in more data. I like the simplicity, but is there any way I can add in validation? Things like:
In the 7 fields it collects, 5 are required.
In the first field, I want to make sure a number is entered and that number isn't on the list already.
I can code these, I just don't seem to have an entry point into the DataForm.
Thanks!
Chuck
While that won't be the answer you like to hear, but according to THIS, your request can't be done in a simple way. So either create your own userform or dive extremely deep in the win api. Sorry :(
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