VBA: TextBox and ListBox - vba

On my UserForm I have a TextBox where is write down my Serial Numbers, something like 421012.
Below it I have a ListBox with options like "Tools, Bench, Wheel"
What I'm attemting to do:
After I type my Serial that I just typed in, I click on one of the options in the ListBox and it should put a value behind my Serial.
Something like this: 421012 + "selecting Tools" =42101215
the "Bench" would get an result of 42101245
My code so far:
Private Sub UserForm_Initialize()
With ListBox1
.AddItem "Tools"
.AddItem "Bench"
.AddItem "Wheel"
End With
End Sub
Somehow I have to tell VBA that Tools equals 15 or Bench equals 45 etc.etc.

Add a second column and then reference that when you're doing your concatenation of the value. "42012" & me.listbox.column(1) should return the value of whatever is selected
with listbox
.columncount = 2
.additem
.list(1,0) = "Tools"
.list(1,1) = 15
.list(2,0) = "Bench"
.list(2,1) = 45
.list(3,0) = "Wheel"
.list(3,1) = 75
end with

I use this much more frequently with objects than with intrinsic types, but another option is to store a Dictionary containing lookups keyed on your ListBox items:
'Module level variable
Private SerialLookup As Scripting.Dictionary
Private Sub UserForm_Initialize()
Set SerialLookup = New Scripting.Dictionary
With SerialLookup
.Add "Tools", "15"
.Add "Bench", "45"
.Add "Wheel", "42"
End With
ListBox1.List = SerialLookup.Keys()
End Sub
Then you can retrieve whatever value you need by using it as an index into the Dictionary, i.e.:
TextBox1.Value = "421012" & SerialLookup.Item(ListBox1.Value)

Related

Transfer multiple selections from ListBox to Word bookmark

I am a bit of a novice when it comes VBA (and coding generally) and apologize in advance if this isn't possible. I have created a userform for users to input different pieces of information (name, address, etc.). When the user clicks the "OK" command button, the values of the differing text boxes appear at various bookmarks within the Word document. For instance, TextBox 1 is dedicated to name, TextBox2 is for the address, etc.
Additionally on the userform, I have added a ListBox that lists various types of data. Ideally, I'd like to have the multiple selections from that ListBox appear at the bookmark in Word on the same line together after clicking the "OK" command button on the userform. It transfers the values to the document (where the cursor is blinking), but I can't figure out how to assign it to the bookmark.
You can see below that I'm assigning the values of the textboxes to the bookmarks and essentially want to do the same with the ListBox.
The ListBox is filled at initialization with the following code.
With ListBox1
.AddItem "name"
.AddItem "address"
.AddItem "secondary address"
.AddItem "Social Security Number"
.AddItem "financial account number"
.AddItem "date of birth"
.AddItem "email address"
End With
End Sub
Private Sub CommandButton1_Click()
Dim DataSub1 As Range
Set DataSub1 = ActiveDocument.Bookmarks("DataSub1").Range
DataSub1 = Me.DataSub1.Value
Dim dAddress As Range
Set dAddress = ActiveDocument.Bookmarks("dAddress").Range
dAddress.Text = Me.dAddress.Value
Dim ii As Integer
For ii = 0 To ListBox1.ListCount - 1
If ListBox1.Selected(ii) Then
Selection.Text = ListBox1.List(ii)
Selection.MoveRight
Selection.TypeParagraph
End If
Next ii
Application.ScreenRefresh
Me.Repaint
Me.Hide
End Sub
For example:
Dim ii As Integer, listText As String
For ii = 0 To ListBox1.ListCount - 1
If ListBox1.Selected(ii) Then
listText = listText & ListBox1.List(ii) & vbCr
End If
Next ii
ActiveDocument.Bookmarks(bmkName).Range.Text = listText

Save combobox value as global variable and select row of selected value

I want to save selected combobox value as global variable and select row of selected value.
I have an Excel file, where I want to make calculations based on inputs in sheet1 and data on sheet2.
Inputs are provided by combobox1 (list of names from column A in sheet 2), combobox2 (case yes/no) and combobox3 (values 1,2,3).
After I select value in combobox1 (for example: ABC which is value A7 from sheet2), I want to calculate from data in row 7 in sheet2:
B7 (sheet2) + C7 (sheet2) * combobox3 value + D7 (sheet2) * (combobox2(yes = 2 / no = 0).
Can anyone help me with that?
Public SelectedComboBox1 As String
Public SelectedComboBox2 As String
Public SelectedComboBox3 As integer
Public calculate2 As Integer
Private Sub ComboBox1_DropButtonClick()
Sheet1.ComboBox1.List = Sheet2.Range("A3:A46").Value
SelectedComboBox1 = Me.ComboBox1.Value
End Sub
Private Sub ComboBox2_DropButton()
With Me.ComboBox2
.AddItem "YES"
.AddItem "NO"
End With
SelectedComboBox2 = Me.ComboBox2.Value
End Sub
Private Sub ComboBox3_DropButton()
With Me.ComboBox3
.AddItem "1"
.AddItem "2"
.AddItem "3"
End With
SelectedComboBox3 = Me.ComboBox3.Value
End Sub
Public Sub Calculate2_click()
calculate2 = Sheet2.Range("B7") * Sheet2.Range("C7") * SelectedComboBox3+Sheet2.Range("D7")*??
Sheet1.Range("H10").Value = calculate2
End Sub
It seems like you added a ActiveX combobox. You might want to use a form-combobox instead for Excel Sheets. Nevertheless: in the Editor you can add varioous actions to events in the combobox. What you did is add a reaction to the button-down-click Event. There you told excel to fill the list and set the SelectedCombobox variable to the - as of yet undefined - value of the combobox.
What you might want is one sub to fill the list as you did and another sub reacting to the change event of the combobox. That sub will be called as soon as someone changes the value of the box:
Private Sub ComboBox1_DropButtonClick()
Sheet1.ComboBox1.List = Sheet2.Range("A3:A46").Value
End Sub
Private Sub ComboBox1_Change()
SelectedComboBox1 = Me.ComboBox1.Value
End Sub
This should get you a good start. There are plenty of resources out-there that teach you how to write effective macros in Excel.
If you just need a result that uses the value of these three comboboxes, you could also connect a simple form-comboboxes with respective cells and claculate the result out of those cells.
But if you still want to use vba, think about using just one button to trigger a sub and access the values immediately:
Private Sub Button1_click
cells("A1").value=Me.ComboBox1.Value * Me.ComboBox2.Value...
End Sub

Excel VBA: Accessing the individual fields in a selected ListBox row

I am trying to access the individual cells in a selected/highlighted ListBox "lstData" row so I can reference their values elsewhere.
When I set a watch for Me.lstData.SelectedItem, I get Expression not defined in context. Same with Me.lstData.SelectedIndex and Me.lstData.Rows(1). The only thing that kind of works for me is Me.lstData.Value, but it ONLY returns the leftmost cell. When I try to plug it into the =OFFSET function
=Offset(Me.lstData.Value, ,1,1)
to access the cell immediately to the right, I get Expression not defined in context again.
How can I reference the other selected cells?
I don't think you can use Offset on a ListBox form control. The way to reference a 'cell' in a multi-column ListBox is by indexing the List property.
Here, i returns the row of the selected item, and 1 represents the second column (base 0) of the listbox:
Option Explicit
Private Sub CommandButton1_Click()
Dim i As Long
With Me.ListBox1
i = .ListIndex
MsgBox .List(i, 1)
End With
End Sub
Private Sub UserForm_Initialize()
With Me.ListBox1
.AddItem "A"
.List(0, 1) = "Alpha"
.AddItem "B"
.List(1, 1) = "Beta"
End With
End Sub

lAdding files to a two column listbox

I am trying to add files to a list box in the same way as they are added when files are attached to an email, in two columns but able to address each file uniquely.
I am using Access VBA and do not know if it is possible.
Any help would be appreciated.
Thanks
Try mapping the indices of the items in the listBox to the files using a separate array/collection/dictionary. Then, when an item is selected, you can use the item's index to get the link
I just tried this below, works perfectly.
Userform Code:
Private d As Dictionary
Private Sub userform_initialize()
Set d = New Dictionary
'populate list box, and add items to dictionary
For i = 1 To 3
With ListBox1
d.Add .ListCount, "Link" & i
.AddItem 0
.List(.ListCount - 1) = "Hello World"
End With
Next
End Sub
'update label1's value based of listbox's selected item
Private Sub ListBox1_Change()
With ListBox1
If .ListIndex <> -1 Then
Label1.Caption = d.Item(.ListIndex)
End If
End With
End Sub
And the userform looked like this: (listbox named ListBox1, and label named Label1):
Example:
If you have problems using the dictionary object, you need to add a reference to "Microsoft Scripting Runtime". But really any collection-type can replace the dictionary in this case (an array/Collection)

Populate Combobox2 Based on Previous Combobox1 Selection

I have two combobox.The first combobox has three items as:{One, Tow, Three} now I would like to load the second combobox based on what user select in combobox1.
For example if user select One in the from combobox1 then namebox1 will populate to combox2
and if if user select Two in the from combobox2 then namebox2 will populate to combox2 and so on..
Can you please let me know how I can do this in VBA?
Thanks
Here is the Update Code:
Private Sub ComboBox1_Change()
Me.ComboBox2.Clear
Select Case Me.ComboBox1.Value
Case "One"
With Me.ComboBox2
.RowSource = "nameBox1"
End With
Case "Two"
With Me.ComboBox2
.RowSource = "nameBox1"
End With
Case "Three"
With Me.ComboBox2
.RowSource = "nameBox1"
End With
End Select
End Sub
Please be advise that I didn't use .addItem to populate the ComboBox1. It hasbeen populate by same method .RowSource which is usinf excel collection selection box
The easiest way to do this would be using a Select Case statement.
Suppose you had
Private Sub ComboBox1_Change()
Me.ComboBox2.Clear
Select Case Me.ComboBox1.Value
Case "One"
' If your data was information you wanted to put in yourself:
With Me.ComboBox2
.AddItem "Adam"
.AddItem "Allen"
.AddItem "Andy"
End With
Case "Two"
' If your data existed in a worksheet range
ComboBox2.RowSource = MyRange.Address
End With
End Select
End Sub
I hope this explains enough to get you going - Otherwise, I'm a bit confused - Can you explain what you mean by NameBox in your question