Property "RowSource" Fails to Display on Combo Box - excel

If I set the .RowSource property of a ListBox to a Named Range then in both the VBA display of the UserForm and the Excel display of the UserForm the row sources can be selected. However, with the ComboBox I only see this on the VBA display. If after setting the .RowSource property I try to .AddItem to the ComboBox object I get run time error 70, permission denied.
Private Sub UserForm_Initialize()
With pres_unit
.AddItem "kPa"
.AddItem "bar"
End With
End Sub
How can I use a named range as a source for the drop down options of a ComboBox?

If you are using a NamedRange for a single Column, you can use the List property of the ComboBox:
Private Sub UserForm_Initialize()
' create the Named Range "myNameRange"
' you can manually (or with variables) modify the Range("B2:B10") in "Sheet1"
ThisWorkbook.Names.Add "myNameRange", Sheets("Sheet1").Range("B2:B10")
With pres_unit
.List = Range("myNameRange").value
'disallows user input, only values from list
.Style = fmStyleDropDownList
End With
End Sub

Related

Can you 'use' a macro with a listbox? VBA Excel Userform

I would like to use a 'listbox' to give the user of a userform a fixed list of 'options' to choose from. I have the information in the worksheet on a separate sheet but can i use a macro to assign the range of data or will i need to specify the range with vba code?
Thanks in advance.
o/
If you have a ListBox on an actual UserForm, it is easiest to define the items directly in the user form's code behind. In VBA, right click on the user form, go to 'View Code' and specify your list of options e.g. in the initialize function:
Private Sub UserForm_Initialize()
With ListBox1
.List = ThisWorkbook.Worksheets("Sheet1").Range("A1:A6").Cells.Value
End With
End Sub
You can also specify a multi-column range:
Private Sub UserForm_Initialize()
With ListBox1
.List = ThisWorkbook.Worksheets("Sheet1").Range("A1:B6").Cells.Value
.ColumnCount = 2
.ColumnWidths = "40;20"
End With
End Sub
It is also possible to assign the list within a macro. Just make sure you assign the list between loading and showing the form.
Public Sub MyMacro()
Load UserForm1
UserForm1.ListBox1.List = ThisWorkbook.Worksheets("Sheet1").Range("A1:A6").Cells.Value
UserForm1.Show
End Sub
If you instead have a ListBox (Form Control) which is embedded in a worksheet, you can specify a worksheet range without VBA. Simply right-click on the listbox, select 'Format Control', and specify 'Input range' on tab 'Control'.

How to programmatically add values to a listbox in a worksheet, not on UserForms?

I would like to programmatically add values to a listbox that I inserted inside an Excel worksheet. I've tried searching up for a solution online but all I see are solutions meant for UserForms, not for Worksheets. Am I missing something?
There are two types of listbox controls you can add to a worksheet.
With a Form Control, the content content is set via the input range, which contains range of values in a worksheet. For example:
If the listbox is an ActiveX control, you can refer to it in code via the worksheet that it is placed on, for example:
Public Sub UpdateListBox()
With Sheet1.ListBox1
.AddItem "A1"
.AddItem "B2"
.AddItem "C3"
End With
End Sub
Try following codes:
Sub AddItemToListBox()
With Sheet1.ListBox1
.AddItem "Paris"
.AddItem "New York"
.AddItem "London"
End With
End Sub
See these article for more details.
Article Link 1
Article Link 2
Assuming you are using List Box under Form Controls in a Worksheet, you can manually refresh data given the name of the List Box, you can extend it further to accept input of different datatypes. I am making use of non-intelli collection ListBoxes in my code. Note it takes the first name-matched listbox in the workbook.
In a separate Module:
Option Explicit
Sub RefreshListBox()
Dim oListBox As ListBox
Set oListBox = GetListBox("List Box 1")
If Not oListBox Is Nothing Then
With oListBox
.RemoveAllItems ' Clears old items
.AddItem "Item 1"
.AddItem "Item 2"
.AddItem "Item 3"
End With
End If
Set oListBox = Nothing
End Sub
Private Function GetListBox(ListboxName As String) As ListBox
Dim oWS As Worksheet, oLBox As ListBox
On Error Resume Next
For Each oWS In ThisWorkbook.Worksheets
If oWS.ListBoxes.Count > 0 Then Set oLBox = oWS.ListBoxes(ListboxName)
If Not oLBox Is Nothing Then Exit For
Next
Set GetListBox = oLBox
End Function

Adding a tooltip to a ComboBox in Excel with VBA

I've added two Dropdown (aka ComboBox) to a Sheet
Using this piece of code I can access the Dropdown but how can I add a tooltip on the Dropdown?
The best solution would be to show a different text for every item but if there is only an unique tooltip for the whole dropdown I can change it after selecting every item.
Sub DropDown1_Change()
Dim s As Object
Set s = ActiveSheet.Shapes(Application.Caller)
s.ToolTip = "Example"
Debug.Print s.ControlFormat.Value
End Sub
This is a forms combobox, it would not have a tooltip capability, but you can make it look like it has a tool tip.
Place a hyperlink with a screen tip underneath the combobox, when you mouse over the combobox the screen tip will pop up.
You can place the hyperlink on many cells if you intend on stretching the combobox over many cells.
Like this
Here is a 20 second clip
http://www.screencast.com/t/ZbkEOyXntItk
You can get the range of the combobox with application.caller.
Assign each combobox to this macro, then you would only need one macro.
Sub DoIt()
Dim r As Range
r = ActiveSheet.Shapes(Application.Caller).TopLeftCell
ActiveSheet.Hyperlinks.Add Anchor:=r, Address:=r, ScreenTip:="5435435345", TextToDisplay:="ddddddddddddddddddd"
End Sub
Following is my code:
Private Sub ComboBox1_Click()
' Adding new items
ComboBox1.AddItem ("S")
ComboBox1.AddItem ("M")
If ComboBox1.Text = "S" Then 'Add your dropdown item here
With Me.ComboBox1
.ControlTipText = "Strong" ' Add your text here
End With
End If
If ComboBox1.Text = "M" Then 'Add your dropdown item here
With Me.ComboBox1
.ControlTipText = "Moderate" ' Add your text here
End With
End If
End Sub

How to display a worksheet on a form in excel-vba

I'm trying to display a worksheet on a form, but until now i did not succeed. The worksheet it contain 15 columns and about 2000 rows. Can someone help me?
Build a form with a listbox on it, and also a button. Then put this code into the form module. The button should have the name CommandButton1 and the listbox should be named ListBox1.
Finally on the listbox control change the columncount to 15.
Private Sub CommandButton1_Click()
Unload Me
End Sub
Private Sub UserForm_Initialize()
' Activate the sheet that has the data you want displayed in the listbox
Sheet1.Activate
' Put the range of the data you want displayed.
' Here I used columns "A" to "O" and 2000 rows
ListBox1.RowSource = "A1:O2000"
End Sub

How to save the .List of a combobox to a variable

I would like to store all of the items from a combobox in a variable. What type should the variable be? I tried to make a
Dim ComboBoxItems() As String ' dynamic array of strings
but then I am not sure how to copy the items from the combobox into this dynamic array?
I can do it in a loop:
for i = 1 to combobox.ListCount - 1
ComboBoxItems(i+1) = combobox.List(i)
next i
(note, am I right that it is really crazy and a dynamic array is 1-indexed while the combobox List is 0-indexed??)
Is this the way to go? Or surely there is a way to copy the entire list without a loop?
For an ActiveX ComboBox on a worksheet called Combobox1 you could use:
Sub Test()
Dim vArr()
ReDim vArr(1 To ComboBox1.ListCount)
vArr = ComboBox1.List
End Sub
Same approach for a UserForm with a combobox ComboBox1 and command button CommandButton1
Private Sub CommandButton1_Click()
Dim S()
ReDim S(1 To Me.ComboBox1.ListCount)
S = Me.ComboBox1.List
End Sub
Private Sub UserForm_Initialize()
'Load some items into ComboBox
With Me.ComboBox1
.AddItem "fred, jones'"
.AddItem "mary"
.AddItem "wayne"
End With
End Sub

Resources