VBA - Listbox and Data - vba

I have a listbox called listbox1 that shows me the data in sheet1 but the problem is that it doesn't show me all of the data sometimes i have 3000 line of data and it shows me anlo first 50 line this is the code i am using if someone can help please :
ListBox1.List = Sheets("Sheet1").Range("A1:F3000").Value

if you need datas from one column only then you'd go:
ListBox1.List = Application.Transpose(Sheets("Sheet1").Range("A1:A3000")) '<== change "A"s in Range("A1:A3000") to wanted column index
otherwise if you want to list data from more columns you have to go like this:
Private Sub UserForm_Initialize()
Dim Data As Variant
...
Data = Sheets("Sheet1").Range("A1:F3000")
With UserForm1.ListBox1 '<== change it with actual UserForm and ListBox names
.ColumnCount = 2
.List = Data
End With
but you'd be able to select one or more (up to ListBox MultiSelect property setting) entire row, and not single "cells".

Try changing the properties of the listbox to the range you need. Then change ColumnCount to 6.
Ie:
To make the RowSource dynamic, input 6 on the ColumnCount proterty and delete the range in the RowSource property and add this code to the form.
Private Sub UserForm_Initialize()
ListBox1.RowSource = Sheets("Sheet1").Range("A1:F3000").Address(external:=True)
End Sub

Related

Need help, VBA, need the combobox to avoid entering a specific values

I have a list of data in a sheet from A1:A8 and i have a combobox1 in a userform. What i want is to avoid a user to input an invalid value in the combobox1 based on the lists of data in the sheet
Go to combobox properties and change Style from 0 - fmStyleDropDownCombo to 2 - fmStyleDropDownList
i think this is what you are searching:
https://msdn.microsoft.com/en-us/vba/access-vba/articles/combobox-beforeupdate-event-access
There you have a way to check the values with that you want, there is a example where you can replace it with your range of cells.
Edited (Nice point, i'll put the example here):
Private Sub Combobox_BeforeUpdate(Cancel As Integer)
For i = 1 to 8
If(Cells(i, 1).Value == Me.Combobox.Value)
Cancel = True
Me.Combobox.Undo
End If
Next i
End Sub
Cya.

Userform listbox that depends on another listbox

I have been looking in the internet for the answer to this, but mostly people say to use data validation, which doesn't really solve my problem. What I'm trying to do is, lets say that I have ListBox1, which has 3 values (red, blue, green) and there's another listbox (ListBox2) where I want value of a list from a worksheet to appear depending on the answer of the first ListBox. For example: I select red from listbox1 and then I want to have the options from the list "red" (apple, coke,fire) in listbox2.
I would greatly appreciate some help in this. Thanks
you could use something like follows (adapt it as per your needs):
Private Sub ListBox1_Click()
With Me.ListBox2
.Clear
.List = Application.Transpose(GetColorItemsRange(Me.ListBox1.value)) 'fill referenced listbox with values from the range returned by GetColorItemsRange function
End With
End Sub
Function GetColorItemsRange(colorValue As String) As Range
With Worksheets("ColorItames") ' change "ColorItames" with actual name of your worksheet with items associated to colors
With .Range("A1", .Cells(1, .Columns.Count).End(xlToLeft)).Find(what:=colorValue, LookIn:=xlValues, lookat:=xlWhole) 'find and reference the referenced sheet row 1 cell matching the passed value
Set GetColorItemsRange = .Parent.Range(.Cells.Offset(1), .Cells.End(xlDown)) 'return the range ranging from referenced range down to last not empty cell before first empty cell
End With
End With
End Function
Data validation is the way to go. You would want to leverage some combination of VBA to adjust the range listbox2 is using after Listbox1 is updated. This is relatively easy if only 1 selection is used on listbox1.
Hopefully you just have one selection, so you could do the following code:
Private Sub ListBox1_Click()
If ListBox1.Selected(0) = True Then
'Selection is apple. Adjust DynamicRange name for A1:A3
ThisWorkbook.Names("DynamicRange").RefersTo = Range("A1:A3")
ElseIf ListBox1.Selected(1) = True Then
ThisWorkbook.Names("DynamicRange").RefersTo = Range("B1:B3")
ElseIf ListBox1.Selected(2) = True Then
ThisWorkbook.Names("DynamicRange").RefersTo = Range("C1:C3")
End If
End Sub
This is based on a setup that looks like this:
Here's what both listbox properties would look like:
If you want to download this classy template, click here.

Sortable List in VBA?

Is there a way to create a VBA user interface that will allow user to order items? See example image taken from a pdf editor.
I want my users to be able to order data in a popout window or list and output their order to a different location. Data is a list of buildings.
Thanks!
For this solution I used a UserForm with a ListBox and a SpinButton control.
I put a list of cells I wanted in my listbox in Column A of Sheet1. I have a generic Building 1, Building 2, etc. through Building 19 so that my data is contained in the range A1:A19 of Sheet1. This is arbitrary and you should change to suit your needs.
This code basically stores the original RowSource that contains the items in the ListBox, deletes the RowSource from the ListBox, rearranges the underlying source data and then re-applies the RowSource to the ListBox in the new order after the user clicks either up or down on the SpinButton
I did not change the default control names (UserForm1, SpinButton1, ListBox1).
Open the VB Editor (either Developer tab --> Visual Basic or press ALT+F11
Right click Microsoft Excel Objects --> Insert --> UserForm
Add a SpinButton and a ListBox so the UserForm looks like so
In the VB Editor, right click UserForm1 under Forms --> View Code
Paste the following code in
Private Sub UserForm_Initialize()
'Populate the UserForm with data from range A1:A19 (arbitrary, change to suit your needs
ListBox1.RowSource = Sheet1.Range(Sheet1.Range("A1"), Sheet1.Range("A1").End(xlDown)).Address
End Sub
Private Sub SpinButton1_SpinDown()
With ListBox1
If .ListIndex = .ListCount - 1 Then Exit Sub 'No item selected or we are in the last position
lCurrentListIndex = .ListIndex + 1 'Get the current position of the item
strRowSource = .RowSource 'Get the current row source
strAddress = Range(strRowSource).Address 'Address of the row source range
strSheetName = Range(strRowSource).Parent.Name 'Grab the parent sheet name
.RowSource = vbNullString 'Empty the listbox
'Re-arrange the underlying data
With Range(strRowSource)
.Rows(lCurrentListIndex).Cut
.Rows(lCurrentListIndex + 2).Insert Shift:=xlDown
End With
'Re-apply the row source
.RowSource = strRowSource
'For continuity, select the previously selected element in its new position
.Selected(lCurrentListIndex) = True
End With
End Sub
Private Sub SpinButton1_SpinUp()
Dim lCurrentListIndex As Long
Dim strRowSource As String
Dim strAddress As String
Dim strSheetName As String
With ListBox1
If .ListIndex < 1 Then Exit Sub 'No item selected or we are in the first position
lCurrentListIndex = .ListIndex + 1
strRowSource = .RowSource
strAddress = Range(strRowSource).Address
strSheetName = Range(strRowSource).Parent.Name
.RowSource = vbNullString
With Range(strRowSource)
.Rows(lCurrentListIndex).Cut
.Rows(lCurrentListIndex - 1).Insert Shift:=xlDown
End With
.RowSource = strRowSource
.Selected(lCurrentListIndex - 2) = True
End With
End Sub
I tried to add comments to clarify what I am doing. It is a slightly cumbersome method, and was adapted from code available here. If you click the Debug (looks like play) button the form should display and populate with the values of cells A1:A19 of Sheet1. You can then select an item in the ListBox and press the up or down buttons of the SpinButton in order to move items up or down the list. An important assumption is that MultiSelect is disabled on this ListBox.
Ordinarily I would not post such an in-depth solution without seeing what you tried, but this problem piqued my curiosity.

Listbox Modification in VBA

I created a Userform in Word which imports 3 columns of data from an excel sheet, inserts it into bookmarks and in the name of the word document and saves it as a pdf.
Now I wanted to add a Listbox into the form to be able to add, modify and delete the inputs manually which are usually imported from the excel sheet .
I already figured out how to add data from 3 textboxes into a 3 Column Listbox but even after googling for hours I can't find a good way to modify selected data.
VB.net has the .selectedItem property, VBA does not. Can anybody give me an example how to modify a multi column listbox with the values of 3 textboxes?
Thanks in advance
You need to iterate through ListBox.Selected and check if it is True. Once you get a True one, you can process that item.
Sample code adds some items with columns and sets up a click event to run through the Selected items.
Private Sub ListBox1_Click()
Dim i As Integer
For i = 0 To ListBox1.ListCount - 1
If ListBox1.Selected(i) Then
Debug.Print ListBox1.List(i, 0)
End If
Next i
End Sub
Private Sub UserForm_Initialize()
ListBox1.AddItem "test"
ListBox1.AddItem "test1"
ListBox1.AddItem "test2"
ListBox1.ColumnCount = 3
ListBox1.ColumnHeads = True
ListBox1.List(1, 0) = "change to 1,0"
ListBox1.List(1, 1) = "change to 1,1"
ListBox1.List(1, 2) = "change to 1,2"
End Sub
Picture of form with Immediate window after clicking each item in turn.

Passing Cell Values into a combo box list

I am looking to pass cells values into the list for a combo box in Excel.
I have created an array to pass the cell values into but they aren't getting to the combo box list when they are being called. I need the cell values to display as the drop down items and all that displays is the blank space:
Sub Clear_Invoice_Data()
Dim Arr As Variant
Arr = Range("A1:A5").Value
frmAddLineItem.ddlProduct.List = Arr
End Sub
It is called by a button from a User form:
Private Sub cmdClearAll_Click()
Call Button_Functions.Clear_Invoice_Data
i = 18
End Sub
Pictures of problem: (no values)
There is another (rougher) method found here to do this if you so choose. Also make sure you define the Sheet name you are sourcing the data from:
Sub AddItemsToBox()
Dim itemcell As Range
For Each itemcell in Sheets("Source Sheet Name").Range("A1:A5")
frmAddLineItem.ddlProduct.AddItem itemcell.Value
Next itemcell
End Sub
Using ComboBox, you can use .List property of the same to populate worksheet values to combox box.
Check this below example:
ComboBox1.List = Worksheets("Sheet1").Range("A1:A5").Value
If you want to use Array,
Dim Arr As Variant
Arr = Worksheets("Sheet1").Range("A1:A5").Value
ComboBox1.List = Arr
I am just getting into VBA and I am doing exactly this. I think the way I found is pretty easy and straightforward. I'll share it in case it can help someone
Declare variable to store table from Worksheet as "range". Make sure
it's outside of sub routines so it can be called by any function
Dim myTable As Range
Store table values in declared variable. Then select which column (range) of values you want to store in your combo box.
Private Sub UserForm_Initialize()
Set myTable = Worksheets("dataSheetName").Range("A2:B6")
Me.myComboBox.List = myTable.Columns(1).Value
End Sub ```