binding buttom to get strings from multiselect listbox in VBA - vba

do anybody know how to get the strings from a multiselect listbox if the user click on a button in excel(VBA)?
And if i click on the button it take the strings into specific cells like D18,D19 and so on..
here is a little overview.
http://fs1.directupload.net/images/150625/o8xhqsll.jpg

I think the best bet is to take a look at this code, and try it out. Come back if you have more questions.
Private Sub UserForm_Initialize()
Me.ComboBox1.AddItem "a"
Me.ComboBox1.AddItem "b"
Me.ComboBox1.AddItem "c"
Me.ComboBox1.AddItem "d"
End Sub
Private Sub CommandButton1_Click()
Dim intComboItem As Integer
For intComboItem = 0 To Me.ComboBox1.ListCount - 1
If Me.ComboBox1.List(intComboItem).Selected = True then
''''Do something
End If
Next
End Sub

Related

Get the values from a Userform to a Module inside an Excel Macro

I have moderate knowledge in SQL and able to manage simple queries...
Basic intention was to add a drop-down on a Excel macro I created (instead of a text box where user give whatever values he wants) but after so much research I came to know that only a Userform can help me to satisfy my requirement, Now I want to know how a value selected under a Userform, can be made available under a Module, so that I can use those values selected from a drop down for any other purpose. I have created a sample user-form with below codes
For USER-FORM
Private Sub cmdAdd_Click()
Call Try1
Unload Me
End Sub
Private Sub UserForm_QueryClose(Cancel As Integer, CloseMode As Integer)
If CloseMode = vbFormControlMenu Then
Cancel = True
MsgBox "Please use the Close Form button!"
End If
End Sub
Private Sub UserForm_Initialize()
Dim cPart As Range
Dim cLoc1 As Range
Dim ws As Worksheet
Set ws = Worksheets("LookupLists")
For Each cPart In ws.Range("PartIDList")
With Me.cboPart
.AddItem cPart.Value
.List(.ListCount - 1, 1) = cPart.Offset(0, 1).Value
End With
Next cPart
For Each cLoc1 In ws.Range("LocationList")
With Me.cboLocation
.AddItem "Cluster1"
.AddItem "Cluster2"
.AddItem "Location 3"
End With
Next cLoc1
Me.txtDate.Value = Format(Date, "Medium Date")
Me.txtQty.Value = 1
Me.cboPart.SetFocus
End Sub
For Module
Sub Rectangle1_Click()
frmPartLoc.Show
End Sub
Sub Try1()
Dim Location
Location = cboLocation.Value
Part = cboPart.Text
MsgBox Location.Value
MsgBox Part.Text
End Sub
actually this code have some issue here
"Location = cboLocation.Value Part = cboPart.Text"
I guess its possible to get the values on a MODULE that we selected using a USER-FORM. But I am having some mistake with my code, or understand this stuff little differently. Please help me to correct my code.
I take help from a website to create this User form (not using the complete code) if you want to please use this link to get the zip file of the same or you can browse there using below link
http://www.contextures.com/xlUserForm01.html
Thanks in advance for helping me.
Thanks to Tigeravatar who help to find the solution.
We have to mention the USER-FORM name as well prior to combobox name from which the vale we are populating from USER-FORM.
Please find the Corrected code below
Sub Try1()
Dim Location
Location = frmPartLoc.cboLocation.Value
Part = frmPartLoc.cboPart.Value
MsgBox Location
MsgBox Part
End Sub
Thanks again for all the comments

filter a ListBox with a Combobox on VBA

I'm working on a Userform and I have weeks trying to develop a code to filter a listbox depending of the value of a combobox.
The closest I have done is make a commandbutton to filter the table where the listbox feeds but it doesn't refresh the listbox.
I have seen on forums people doing things like I want but I have tried all of them with no results.
Private Sub CommandButton1_Click()
If ComboBox1.Value <> "" Then ActiveSheet.ListObjects("Tabla2").Range.AutoFilter field:=2, Criteria1:=ComboBox1.Value
End Sub
Private Sub CommandButton2_Click()
ActiveSheet.ListObjects("Tabla2").Range.AutoFilter field:=2
End Sub
Private Sub CommandButton3_Click()
Unload UserForm2
UserForm3.Show
End Sub
Private Sub UserForm_Initialize()
For i = 2 To 30
ComboBox1.AddItem Sheets("Proyectos - J.P.").Range("A" & i).Value
Next i
End Sub
04-05-2017
Workbook Link
https://drive.google.com/open?id=0B4B7v0UZxizCYnY2bVNTNURyLVU
In the WorkBook you will see 3 userforms, the Userform1 is Ok.
The userform2 have the Combobox (Proyect Code) and the ListBox i want to filter.
The userform3 is not ready yet, because i need to have on 100% the Userform2 to take a decision.
Hope it helps.
Regards
I have already faced a similar situation, but instead of a ComboBox, I needed to filter the ListBox based on the selection of other ListBox and the selection of an Option. The way I found to meet my need was to use a Pivot Table in a hidden sheet. It worked fine for me, bit not all data can be rearranged in a Pivot Table, so I will understand if my suggestion does not work for you.
First Step: set up a Pivot Table with your data source to be used in your ListBox. Pull the fields you want to filter in the Filters area. Create a dinamic named range with your data, like in the image:
=OFFSET('Certificates Pivot'!$A$5;0;0;COUNTA('Certificates Pivot'!$A$5:$A$50);2)
Second Step: create your UserForm. I set up 2 ComboBoxes as filters to the ListBox, but you can remove or add as many as you can, you'll just need to adjust your code. I also named the ranges that will be available in the list options of the ComboBoxes. So we'll have:
The UserForm's code will be something like this:
Private Sub UserForm_Initialize()
ComboBox1.RowSource = "CustomerID"
ComboBox2.RowSource = "SalesOrg"
With ListBox1
.RowSource = "Consult_List"
.ColumnCount = 2
.ColumnWidths = "60;90"
End With
End Sub
Private Sub ComboBox1_Change()
Dim SelectedCID As String
Dim SelectedSO As String
SelectedCID = ComboBox1.Text
SelectedSO = ComboBox2.Text
With Sheets("Certificates Pivot").PivotTables("Certificates_PivotTable")
.ClearAllFilters
If Not SelectedCID = "" Then .PivotFields("Customer ID").CurrentPage = SelectedCID
If Not SelectedSO = "" Then .PivotFields("Sales Org.").CurrentPage = SelectedSO
End With
ListBox1.RowSource = "Consult_List"
End Sub
Private Sub ComboBox2_Change()
Call ComboBox1_Change
End Sub
You can hide the sheet where your Pivot Table is, so when you filter it through your UserForm, it will update in the background. You should also set up your Pivot Table to update its cache to capture new inputs in your data source.
I hope it works for you! Let me know what were the results.

VBA Userform Text Box Scroll Start at top

I have an Excel user form with multiple multi-line text boxes that need scroll bars. When I click in a textbox to scroll, it starts at the bottom of the text. When this happened with just one textbox on the user form I used this:
Userform1.TextBox1.SelStart = 0
and everything worked. If I try to use that on multiple text boxes in the same form, the scroll bar never appears for any of the boxes. Does anyone know how to fix this?
Update:
Found a quirk that my help narrow down the problem: with multiple textboxes, selstart=0 works on the first box, but then I need a much bigger number for the selstart of the next textbox. Example. The code below puts the scrollbar at the top of both textboxes. The form is shown through a double click on sheet 1 and the the values of the textboxes are create in the initialize sub.
Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
UserForm1.Show
End Sub
--------------
Private Sub UserForm_Initialize()
UserForm1.TextBox1.Value = Sheets("Sheet1").Cells(1, 1).Value
UserForm1.TextBox1.SelStart = 0
UserForm1.TextBox2.Value = Sheets("Sheet1").Cells(2, 1).Value
UserForm1.TextBox2.SelStart = 200
End Sub
But I could only find that textbox2 had to start at 200 through guess and check. I dont know how to determine where that textbox should start.
I had a breakthrough. If I use SetFocus then do selstart= 0 everything seems to work.
Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
UserForm1.Show
End Sub
Private Sub UserForm_Initialize()
UserForm1.TextBox1.Value = Sheets("Sheet1").Cells(1, 1).Value
UserForm1.TextBox1.SetFocus
UserForm1.TextBox1.SelStart = 0
UserForm1.TextBox2.Value = Sheets("Sheet1").Cells(2, 1).Value
UserForm1.TextBox2.SetFocus
UserForm1.TextBox2.SelStart = 0
End Sub

Excel Userform with Textbox, how to toggle through values in range of textbox

Purpose: Click on a cell in a range (Range: Column K:K on excel worksheet). Once you click on a specific cell in column K, userform pops up with cell value using following code:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
On Error Resume Next
If Target.Cells.Count > 1 Then Exit Sub
If Not Intersect(Target, Range("K:K")) Is Nothing Then
Credit_Information.TextBox1.Value = Target.Value
Credit_Information.Show
End If
End Sub
My question, is depending on where I click on column K, I want to use two buttons on my userform (Previous and Next) to move up and down column K and see the values of the cell dynamically change on my userform. Is this possible? Please let me know if any clarification is needed.
Just add the two command buttons to your userform.
Name one of the buttons cmdNext and give it a caption of "Next".
Name the other button cmdPrev and give it a caption of "Previous".
Then, in the userform code module, place these routines:
Private Sub cmdNext_Click()
ActiveCell(2).Select
End Sub
Private Sub cmdPrev_Click()
If ActiveCell.Row > 1 Then ActiveCell(0).Select
End Sub
That's it.
Note: if you want you can add code to ensure that the ActiveCell is in column K before allowing the new selections:
If ActiveCell.Column = 11 Then ...
Perfect, Thanks!
I also found out that using Offset worked for me too in this manner. I'm not sure however if I'm breaking any conventions by doing this.
Private Sub CommandButton1_Click()
ActiveCell.Offset(-1).Activate
End Sub
Private Sub CommandButton2_Click()
ActiveCell.Offset(1).Activate
End Sub
It is possible, but I would create another procedure for that. What you could do is declare a public variable in your userform & set it equal to the range Target. Then you could call another procedure from the userform on each button click and redefine the selected range after each click.
So, at the top of your userform do this:
Public selected_cell as Range
Then for the up button:
Private Sub ButtonUp.Click()
If selected_cell.Row < 2 Then Exit Sub
selected_cell.Rows(0).Select
Set selected_cell = selected_cell.Rows(0)
me.TextBox1.Value = selected_cell
End Sub
And the down button would be:
Private Sub ButtonDown.Click()
selected_cell.Rows(2).Select
Set selected_cell = selected_cell.Rows(2)
me.TextBox1.Value = selected_cell
End Sub
Now let's make your code like this:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
On Error Resume Next
If Target.Cells.Count > 1 Then Exit Sub
If Not Intersect(Target, Range("K:K")) Is Nothing Then
With Credit_Information
Set .selected_cell = target
.TextBox1.Value = Target.Value
.Show
End With
End If
End Sub

Jump to an item in Listbox

One question, it is possible to jump somehow to a certain index in a specif listbox as in the image below?
I already tried the following code
Listbox.ListIndex = index
But it drives me to the error You've used the ListIndex property incorrectly
One property of my list that might be important to mention.
Row source type : Table/Query
Thank you in advance.
Try ListBox.Selected(index) = True. If it is a multiselect listbox, you also need to loop through the other elements and unselect them in the same way.
Create a standard module with the code
Sub Main()
UserForm1.Show
Unload UserForm1
End Sub
Insert a userform and visually do something like
Go into the userform code and add
Private Sub CommandButton1_Click()
Dim v As Long
For v = 0 To ListBox1.ListCount - 1
If TextBox1 = ListBox1.List(v) Then
ListBox1.Selected(v) = True
End If
Next v
End Sub
Private Sub UserForm_Initialize()
With ListBox1
.AddItem ("text1")
.AddItem ("text2")
.AddItem ("text3")
End With
End Sub
Run Main Macro
Type in the box : text2
The text2 will be selected in the list