I have a Combobox in a user form. I get the values for that Combobox from a specific column in my worksheet (Column T).
The Combobox is simply a list of insurance products and their pricing elements, it shows all products on the user form so that the user can select whatever products he wants.
The problem is that the list can grow or decrease (i.e. new products or deleting them). And to make things more complicated, the sheet that has the aforementioned values is located in a very hidden sheet (protected for confidentiality purposes). So whenever I open my userform, the Combobox doesn't show any data (i always have to open the hidden sheet so that they can be shown)
Here's the code
Private Sub UserForm_Initialize()
Products = Range(Range("T7"), Range("T7").End(xlDown)).Address
UserForm11.produit.RowSource = Products
End Sub
Whenever i add the Worksheets name (Worksheets("Calcul").Range(Range("T7"), Range("T7").End(xlDown)).Address) it generates an error.
I just want the combo box to be dynamic and fixed to the source mentioned in the hidden sheet. Any clues?
Private Sub UserForm_Initialize()
dim Products as string
with sheets("Calcul")
Products = .Range("T7", .Range("T7").End(xlDown)).address
end with
UserForm11.produit.RowSource = "'Calcul'!" & Products
End Sub
another way, Products as range.
Private Sub UserForm_Initialize()
dim Products as range
with sheets("Calcul")
Set Products = .Range("T7", .Range("T7").End(xlDown))
end with
UserForm11.produit.list= application.transpose(Products)
End Sub
The WorksheetFunction OFFSET() is made to create dynamic ranges. Creating a Dynamic Named Range in the Name Manager will allows us to see what range is being targeted. See: Dynamic Named Range. The Named Range or its formula can be used as the ComboBox's Rowsource. FYI: the Rowsource updates when the userform loads. You'll have to clear the Rowsource and reassign it to refresh the values after the userform is loaded.
InsuranceProductsList =OFFSET(Calcul!$T$7,1,0,COUNTA(Calcul!$T:$T)-COUNTA(Calcul!$T$1:$T$7),1)
Related
I have an Excel VBA application that goes through a sheet which contains product orders on a sheet in a workbook and searches the worksheet for orders that match various criteria which is populates in a search worksheet. The contents from this worksheet are then displayed in list box. There are several user forms that allow the user to select an order and then manipulate the order. After doing this the order manipulated may not meet the search criteria so I want to clear the list box contents and the selected row in the list box. I have tried numerous things but nothing seems to work. My latest is something list this:
Private Sub ClearListBox()
UserForm5.lstOpenO.ListIndex = -1
UserForm5.lstOpenO.RowSource = ""
End Sub
But I have tried setting the UserForm5.lstOpenO.Selected to false for all the rows. I have tried clearing the search worksheet and then displaying that which should only show the headers on the columns but the highlight in the selected row remains.
Any help would be greatly appreciated
Bruce
First of all you should not use the default instance of the userform in your code. This will lead to ambigous behaviour
I'd suggest
Private Sub ClearSelectionInListBox()
Dim varItm as variant
With lstOpen0
varItm = .MultiSelect
.MultiSelect = 0
.MultiSelect = 1
.MultiSelect = varItm
End With
End Sub
This will clear the selection within the listbox.
It is not clear if you really want to clear the contents. Because if you want then it does not make sense to think about clearing the selection.
If you want to clear the listbox then it is not neccessary to clear the selection first.
Private ClearListBox()
With lstOpen0
.RowSource = ""
.Clear
End With
End Sub
But after that you need to fill the listbox again.
Further reading
VBA userform
Userform.Show
ListBox
I have a small issue, but I cannot find the code for it. For simplicity I made a simple example.
I made a validation userform with a modeless listbox (lsb_dataval_man). This listbox is populated with named ranges with only the names that have an error or are empty. Because there are many tabs, the user can navigate quickly to the sheets and cells which need attention and modify or populate them.
When you doubleclick a named range in the listbox, it will go to that reference and users need to enter data directly. See the simplified code for this double click below.
With the command Application.Goto ThisWorkbook.names(named_range).RefersToRange it will select the cell, but I am not able to type in the cell directly as like when you would do a select. A user needs to click the cell again or click in the formula bar and enter data. How can I change my code so after going to the cell of the named range, the user can type directly? Perhaps this is related to the modeless form which is still active?
The code:
Private Sub lsb_dataval_man_DblClick(ByVal Cancel As MSForms.ReturnBoolean)
Dim named_range As String
named_range = Me.lsb_dataval_man
'select the named range cell
Application.Goto ThisWorkbook.names(named_range).RefersToRange
End sub
This should work:
Private Sub lsb_dataval_man_DblClick(ByVal Cancel As MSForms.ReturnBoolean)
Dim named_range As String
named_range = Me.lsb_dataval_man
AppActivate ThisWorkbook.Windows(1).Caption 'switches focus from userform to workbook
ThisWorkbook.Names(named_range).RefersToRange.Select 'selects cell
End sub
off late I got developed interest in excel VB applications and practicing it since last 2 years. ( I use Excel 2016 version )
I have a problem with my recent Vb program. I have designed a main Userform with two ComboBoxes and out of it, ComboBox2 list is depending on ComboBox1 value.
For ComboBox1 row source, I have linked it to one of the named ranges in worksheet Sheet4("Settings") ColumnB which is actually the list of process names and in same sheet # different columns I have many named ranges (task list) bearing the name of all the items listed for ComboBox1 as headings.
So when I choose list in ComboBox1 dropdown , (Process names, which is also named range) only the relevant options will appear in ComboBox2 (task item) and upto this file is working fine.
Now to add more task line items to named ranges which will appear in combobox2 , I have designed another Userform (2nd one)so that user can add more task options himself under any selected process and he don't have to approach me.
So what I did, in a 2nd Userform I added combobox3 and TextBox1 with submit button. The Combobox3 will show same list as ComboBox1 does -Process names.
Intention of 2nd Userform is when user select process name from Combo3 followed by new task list entered in textbox1 and after submitting, Vb will look for named range selected in combo3, should go to worksheet "Settings" and find empty row in selected process range and will push the value of textbox1-task item. So that user will be able to get newly added value under his selected process in a main Userform.
But the problem is here, while operating the file the excel file is to set to show only Userform, to feel like App and will not display any worksheets during Userform operation. (No excel environment)
However while using 2nd Userform the code is forcing to display " Settings" worksheet (excel environment), since it is forbidden in code for not to navigate to excelworksheet, UF2 action failing in completing task.
However, If I keep Worksheets displaying in background (& 2nd Userform in front) then no issue, I was able to append the textbox1 value to desired named ranges in " Settings" ws.
The Error i get while using 2nd Userform without having excel sheets Opened is : " Run-time Error '1004' , Select method of Range class failed" . I tried using " application.screenUpdate=False/True", but no use, code stops working, unless excel worksheet is displayed...
any help in this regard will be appreciated
Code written :
Private Sub UserForm2_Initialize() ' UF name "addCat"
Dim i As Long
For i = 2 To Sheet4.Range("B50").End(xlUp).Offset(1, 0).Row
Me.SelFnCB.AddItem Sheet4.Cells(i, 2).Value
Next i
End Sub
'---------------------------------------------------------------
Private Sub CatUpdtCmd_Click() 'this is to add new items under selected namedRanges in worksheet
Dim lastRow As Long
Dim ws As Worksheet
i = Me.SelFnCB.Value 'Combobox 3 value with process names as named Range
Set ws = Worksheets("Settings")
Worksheets("Settings").Unprotect "Bnm0000"
Range(i).Select ' at this stage of code execution , screen tries physically go to worksheet and tries to display it.which i dont want.
Range(i).Find("").Activate
ActiveCell.Value = Me.NwCat.Value '( "NwCat" is TextBox1 with new task to be added to selected named range)
Worksheets("Settings").Range("A1").Select
Worksheets("Settings").Protect "Bnm0000"
Me.NwCat.Value = ""
ActiveWorkbook.Save
MsgBox " Done, Select the process again to view your update ", vbInformation, Title:=" Category added "
Unload Me
addCat.Show
End Sub
I want to take a "list of name" data sets from excel and use it for an Combobox Userform. The user uses a dropdown menu from the Combobox to make his/her selection of a name.
My problem is that I don't know how to apply the "list of name" data sets to the combobox. The range for the list of names is in cell C2:AU2 and its in a worksheet called "DoNotPrint-Names". Once the name is chosen by the user I want it to output it to a cell in "DoNotPrint-Setup" worksheet when the button "Next" is clicked.
Screenshot Part of the Names list:
https://imgur.com/sqsUFmF
Screenshot of Userform:
https://imgur.com/UX8ytrY
I tried the code below which asks the Userform to prepopulate cells from "DoNotPrint - Names" worksheet by transposing it first since its a single row. Not sure how to proceed afterward.
Private Sub UserForm_Initialize()
ComboBox1.List = WorksheetFunction.Transpose(DoNotPrint - Names.Range("C2:AU2"))
End Sub
Select your list and give it name,
Example
Then load that list on your ComboBox1
Code Example
Option Explicit
Private Sub UserForm_Activate()
Dim vList As Variant
For Each vList In [MyList]
Me.ComboBox1.AddItem vList
Next
End Sub
Addendum to found solution
Just in Addition to #0m3r 's valid answer, you can use the array Approach to assign the entire named horizontal list to the comboboxe's .List property in a one liner:
Me.ComboBox1.List = Application.Transpose([myList])
Edit
Alternatively you can use the control's .Column property which seems to be rather unknown:
Me.ComboBox1.Column = [myList].Value
I am using Excel 2013 and I have written the below code to auto populate a combobox from a range for one of my userforms. I have the combobox named "Times"
Sometimes this populates the list and other times it gives a blank list even though the range did not change.
Any idea how to stop this and just have it work everytime?
Private Sub UserForm_Initialize()
Sheets("Backend").Visible = xlSheetVisible
Times.RowSource = Sheets("Backend").Range("E1:E96").Address
Sheets("Backend").Visible = xlVeryHidden
End Sub
I have found a different way to do this now that works. Thanks to this post.
Populate list box with a named range