I have a code that adds a couple of ComboBoxes to a UserForm (the number of ComboBoxes is variable). Now that I want to control a certain ComboBox based on the selected value of another ComboBox (change or click event), how do I do that via VBA code?
I am having trouble working through the examples given on other posts for this topic. I have a userform that creates a number of controls at runtime. One group of controls is a series of textboxes and a label underneath that sums the values of the textboxes. When a user changes the value in a textbox the label needs to update the sum. I have read about created class modules to define the event but it is all going over my head.
I'm trying to attach Worksheet_Change to a worksheet created at runtime. Usually you put it in the code window of the Sheet object but what do you do when you create the worksheet at runtime?
I have a userform that has one combobox at the top created manually. When the userform is opened, the user select an option in the combobox (these options are taken from a range on 1 worksheet). From the selection of the combobox, I use the comboxbox's change event to create and display 5 columns of textboxes and 2 columns of command buttons on the userform.
The number of rows of textboxes created depend on the option selected from the combobox since each option links to a different range of cells. Each of the 5 textboxes in each are set to be ".enabled = False" and display text as per the cell values within a range on another worksheet. 2 Columns of command buttons are created at the end of each row of textboxes - 1 is enabled and the other is not.
The creation of the textboxes and command buttons works as required. However, I am having problems with setting click events for each command buttons. When the 1st column of Command buttons are created, I need the click events to be created and filled out with 2 actions:
1. Enable all textboxes in the same row as the command button
2. Enable the other command button in the same row.
Here is the code I have so far that creates the textboxes and command buttons.
Each of the 5 textboxes and 2 command buttons have a unique name so the 1st row will have textbox and command button names of cTxtA1, cTxtB1, cTxtC1, cTxtD1, cTxtE1, CmdAmend1 and CmdConfirm1. The 2nd row will have the same names but with 2 on the end and so on. The bold sections is the code for the creation of the command buttons that I want click events for.
Code:
Private Sub CboTeamSelect_Change() Application.ScreenUpdating = False If CboGroupSelect.Value = "" Then Exit Sub Dim cTxtA As Control, cTxtB As Control, cTxtC As Control, cTxtD As Control, cTxtE As Control Dim CmdAmend As Control, CmdConfirm As Control Dim iNum As Integer Dim TxtTop As Long
This code create a userform on the workbook opening .I'm having problems with
1. Closeing the Visual basic window after this procedure runs 2. Being able to use the create userformstext box value in other procedures 3. how to delete the userform on close
Sub AddUserFormInputRequest() Dim objVBProj As VBProject Dim objVBComp As VBComponent Dim objVBFrm As UserForm Dim objChkBox As Object Dim x As Integer Dim strCode As String Dim firstLine As Long, SecondLine As Long Set objVBProj = Application.VBE.ActiveVBProject Set objVBComp = objVBProj.VBComponents.Add(vbext_ct_MSForm) With objVBComp . Name = "InputRequest" . Properties("Width") = 200 .Properties("Height") = 100......................
I'm trying to add a worksheet event via code. It works OK when I run it on its own but I get a run-time error 9 subscript out of range error when I try to run it from another procedure.
Please find attached my 2 bits of code. Any help greatly appreciated.
This is the bit that works OK on its own but not when called from my other procedure
Sub Code_To_Write_Code()
'This writes code to the new sheet
Dim StartLine As Long Dim SheetToAddCodeTo SheetToAddCodeTo = ActiveSheet.CodeName
With ActiveWorkbook.VBProject.VBComponents(SheetToAddCodeTo).CodeModule StartLine = .CreateEventProc("Calculate", "Worksheet") + 1 ..............
I have code that creates a row of controls on an MSForms.Userform at runtime.
I would like the user to be able to save these controls so that they are available the next time he opens the form. The user would be able to add or delete a row of controls and save them AND the values that he has set. This allows great flexibility for each session using the form.
The values aren't a real problem - I've been using the SaveSettings function for the design time controls values. The runtime controls are combobox, textbox, listbox, checkbox.
I don't want to design-time build the controls and just toggle the visible property (not a good solution for my app).
I have a userform with a single blank multipage. At runtime additional pages are added, the number of pages depends on input from another userform. Six frames, containing labels, textboxes and comboboxes are then added to each page.
I need to be able to use the textbox and combobox change events of these dynamically created controls to perform lookups and calculations. Although I can name the controls at the time they are created, it is not feasible to write code specifically for each control (I can have over 1,600 text boxes distributed over 9 pages, for example).
Having searched for some time on how to achieve this I believe using a class module is the way to go. However, how to use a class module is just not sinking in I'm afraid.
HTML Private WithEvents mpTextBox As MSForms.TextBox Private WithEvents mpComboBox As MSForms.ComboBox
Private Sub Class_Initialize() Set mpTextBox = MSForms.TextBox Set mpComboBox = MSForms.ComboBox End Sub
Private Sub mpComboBox_Change() MsgBox "ComboBox value has been changed." End Sub...........................................
I've created several UserForms, for some reason this one won't co-operate!
Issue are: 1. Have to click the OK button 3 times for the code to execute (this does not occur when I filter through the code in debug/F8 mode).
2. Before processing I have a label and image set to visible = true that won't appear on the form at runtime (this works when I filter through the code in debug/F8 mode).
Imagine I have a form with a few controls on it. And the controls properties are set up at runtime.
Is there any way possible to save the property changes that were made at runtime to the controls themselves? I mean - short of manually editing all the controls at Design time?
This might be easier to explain by example. See dummy code below. Using this example - I want to find something that will save the Caption of CommandButton1 as "TestMe" (rather than have it only temporarily set at runtime).
I use the following (work in progress) function to add the controls to the userform (usually added to page or frame).
Code: Public Function AddControls(ByVal objTarget As Object, ByVal strUiName As String) Dim rngControls As Excel.Range, rngProperties As Excel.Range Dim rngControl As Excel.Range, rngProperty As Excel.Range Dim objControl As Object
With shtFormUI Set rngControls = .Range("D2:D" & .Range("D" & .Rows.Count).End(xlUp).Row)
[Code] .....
Now I need a means of trapping the controls events. I thought I could use a class, e.g.: cFormEvents
Code: Option Explicit
Public WithEvents lblLabel As MSForms.Label Public WithEvents tbxTextBox As MSForms.TextBox Public WithEvents cbxComboBox As MSForms.ComboBox Public WithEvents lbxListBox As MSForms.ListBox Public WithEvents cbtCommandButton As MSForms.CommandButton
Private Sub lblLabel_Click()
[Code] .....
It seems I cannot reference the controls because I they are added at runtime. For the given example, I want to run whatever procedure name appears for lblPrimaryContact in column AC (click event). So in my userform module I instantiate the class, but I get an error when I try and reference the control:
Code: Set m_clsFormEvents.lblLabel = Me.lblPrimaryContact Error is "method or data member not found".
Any alternative method to grab the click event for the control added at runtime?
My program(which is an add-in, or will be when I've finished writing it) creates a new worksheet called "Groups Summary" I would like this sheet to be locked so that the user can not modify anything on it. I would also like that if they double click a cell this will launch a userform if based on certain conditions.
I believe this code would have to go in the worksheet itself, so the question is how do I get the macro to enter code into a worksheet that it creates?
Is there a way to write it in a standard module and then automatically copy it when the worksheet is created?
Is there a way to fix a private sub to catch keydown effect of various combos and textboxes of my userform?
Instead of typing the same thing to each and every control's keydown event just call a sub. Code goes like:
Select Case KeyCode Case 112 ShellExecute 0, "open", "hh.exe", ThisWorkbook.Path & "1.chm", "", 1 Case 113 If MsgBox("Backup now", vbQuestion + vbYesNo) = vbYes Then BackUp Case 114 Col = 3 SearchD Case 115 showexcel Case 116 readonly e.c.t.
Instead of that, if i place code in a private sub test() just call test in keydown event
I want to be able to reset the value in a combobox, but without the combobox executing code, when it resets. Is there any way of doing this?
I have tried the code below but the ComboBox still executes when its value is changed.
Sub Reset_combobox() Worksheets("Sheet1").ComboBox1.Enabled = False Worksheets("Sheet1").ComboBox1.Value = 1 Worksheets("Sheet1").ComboBox1.Enabled = True End Sub
I use excel 2002 but some of my office are on 97, i want to add a small workbook open event code which works for me but debugs for the others?? The code is basically, go to a tab, on that tab and that range sort..
Example below is in my macro and when my peer is running this worksheet which has Workbook_Open() enabled in "ThisWorkbook", the outcome is that he can not successfully save this workbook because he doesn't have the folder and sub directory in his C: drive.
How do I code it such that in the event the user does not have the specified folder and sub-foldername, it will automatically create one for him and save it as "C:Spare PartsCritical ListStocking Status.xls".
I am trying to create some controls in a userform on the fly based on the data in a sheet. The part of my code is the following:
summaryForm.Controls.Add bstrProgID:="forms.label.1", Name:="LAWts", Visible:=True With summaryForm.Controls("LAWts") .Top = 120 .Left = 20 .Height = 18 .Width = 300 .Caption = "Weights: " End With
Question I is that how can I connet this textbox to a click event or just to a procedure when it is clicked? I tried to use OnAction but it does not work for textbox in a userform. Question II is that how can I find the all properties of a textbox or any other controls? Is there a manuel or reference available on the internet?
I am working on a dynamically produced userform but the dynamically created Cancel and OK buttons don't work. I have been able to get round it by adding the buttons to the top of the form (non- dynamic) but I'd really like to be able to create them dynamically based on variable for other parts of the project. How can I produce dynamic command buttons that actually work?
Here's my code but I'd be happy with generic code I could use.
Private Sub UserForm_Activate() ' Define variables Dim LabelV As MSForms.Label Dim CheckboxV As MSForms.Checkbox Dim rngFields As Excel.Range Dim field As Excel.Range Dim lngNextTop As Long Dim lngTitleBarHeight As Long Dim ColumnNum As Integer
' Set constants ' Choose height and width of Label box Const cTextBoxHeight As Long = 12..............
I created a macro to create command buttons on sheet 1 of my worksheet. The created buttons using oleobject and class type: forms.commandbutton1, creates an object with no properties on the sheet (i.e. if you right click on it excel thinks it's an object and not a control.) Is there a way to create it as a control?
Dim FName As String Dim VBComp As VBComponent Dim wbnew As Workbook Dim x As String
Set wbnew = Workbooks.Add
Set dest1 = ActiveWorkbook
dest2.Activate With ActiveWorkbook FName = .Path & "code.txt" If Dir(FName) "" Then Kill FName End If For Each VBComp In .VBProject.VBComponents If VBComp.Type vbext_ct_Document Then VBComp.Export FName wbnew.VBProject.VBComponents.Import FName Kill FName ElseIf VBComp.Name = "ThisWorkbook" Then If VBComp.CodeModule.CountOfLines > 0 Then wbnew.VBProject.VBComponents
The complete code in this workbook sends the entire workbook to one person and it creates a new workbook and pastes a range and the VB code needed to run the button it copies and sends this to another person.
The codes runs excellent in 2003. In 2007 I can get the entire workbook to send to the first person, but
The email to the second person will send, but they can not open the workbook. I get the error "File Format or file extension is not valid. Please verify file is not corrput and that extension matches file.
I know trust access to visual basic has to be enabled in 2003 for this code to run, so in 2007 I went to the Excel options trust center and clicked the box which says Trust access to the VBA project object model.
When I step through this code everything looks great. The worksheet area is correct, the button is copied, the code can be seen in the properties window, but then I get the email and it will not open.
There is even a line of code that kills the file after the mail is sent (temporary saving location is the desktop). So, I used a ' and made it a comment line so I could try to open the file from my desktop. It will not open there either. File extension being used is .xlsm
Following bit of code runs fine if placed in Word VBA:
Code: Public Sub TaskUsageInExcel() Dim tsk As Task For Each tsk In Tasks 'Gives RTE 429 when run through Excel Debug.Print tsk.Name Next tsk End Sub
So I referenced 'Microsoft Word 12.0 Object Library' in Excel and ran the code and it gave me this error 429. Noticeable part was, no instance of word was running at that time.
So I modified the code as below:
Code: Public Sub TaskUsageInExcel2() Dim wdApp As Word.Application Dim tsk As Task
I have two radio buttons. Let's call them Button_1 and Button_2. They both have code behind the click event.
One of the lines in the Button_1 code makes the value of Button_2 false. If Button_2's value at the time is true, the Button_2 click event fires off and runs through it's code.
How do I disable Button_2's click event in this scenario so the value changes, but the code in it's click event won't run?
I tried application.enableevents = false, but that didn't do it.
way to allow a user to add a control to a userform without going into the VBE. Here is the situation, I am developing a userform to calculate a projected budget. I want the user to be able to select the number of controls to add, and click a command button to add them. Based on other selections that the user makes, different controls would automatically be added. Is this possible? As a follow up, will I be able to atttach code to these new controls?