I currently have a cell with a list separated by commas (1, 2, 3) and the list isn't always the same amount of items. It can be up to 10 items.
I then have a listbox with items 1 - 10 in it.
I want to be able to select the specific items that are in the cell on a form with the listbox on it.
I have started by splitting the cell into an array like this:
Dim Array() As String
Array= Split(ActiveSheet.Range("A1"), ",")
But I can't figure out how to select items in my listbox that match the array.
Try the below Code. I tested it. It worked fine for me.
Sub formdisplay()
Dim valsToSelect
valsToSelect = Split(Range("E6").Value, ",") 'E6 is my cell where i have my value in the form of 5,6,7,9
For Each item In valsToSelect
i = 0
For Each listItem In UserForm1.ListBox1.List
If CStr(listItem) = item Then
UserForm1.ListBox1.Selected(i) = True
Exit For
End If
i = i + 1
Next
Next
UserForm1.Show
End Sub
Related
I would like to know if there is a way to add selected multiple combo box values to a dynamic array. So far this my code below, at the moment I can only submit the one selected Combobox value to the array list.
Private Sub UserForm_Initialize()
ComboBox1.AddItem "1"
ComboBox1.AddItem "2"
ComboBox1.AddItem "3"
End Sub
Private Sub CommandButton1_Click()
Dim cmbbox(10) As String
Dim i As Integer
For i = LBound(cmbbox) To UBound(cmbbox)
cmbbox(i) = ComboBox1.Value
MsgBox cmbbox(i)
Next i
End Sub
I would like to be able to select a value from the combo box, and then that value gets passed to my array at the 0 position, and then if the another value is selected from the combo box, then that value is passed to my array's 1 position etc...
This should do:
For Each Item In ComboBox1.List
If Not Item Then
MsgBox Item
End If
Next
Edit: Did I miss your point here, or did you change your question? According to what I read now, you want to append combobox value at the end of your array each time you hit commandbutton. You should do as follows:
Define your array outside of your sub (at the very top):
Dim cmbbox() As Variant
and the code should look like:
Private Sub CommandButton1_Click()
If Len(Join(cmbbox, "")) = 0 Then 'if your array is empty add the first value from combobox
ReDim cmbbox(0)
cmbbox(0) = ComboBox1.Value
Else 'if your array is not empty redim your array and add value from combobox
ReDim Preserve cmbbox(UBound(cmbbox) + 1)
cmbbox(UBound(cmbbox)) = ComboBox1.Value
End If
MsgBox "Last Added Item : " & cmbbox(UBound(cmbbox))
End Sub
As #Tehscript has indicated, the property you're after is .List which returns a two-dimensional, zero-based array: the first dimension being 'rows' and the second 'columns'.
From your question, it seems as if you want a specific index (or indices) from the row dimension. If your ComboBox only has one column then the second dimension could be hard-coded as zero.
A For Each loop would be okay, but the problem is that it will loop through every item in the array rather than just each row. It might be more efficient, therefore, to run a For [index] loop. Let's say you want the first and third items in your ComboBox, then the code snippet would be:
Dim i As Long
Dim v As Variant
v = ComboBox1.List
For i = 0 To UBound(v, 1)
If i = 0 Or i = 2 Then
MsgBox v(i, 0)
End If
Next
I have multiple pivot tables on a sheet, I'm wanting to loop through all of the pivot field items on 2 of those pivot tables (tables 4 and 5). I created 2 PivotItems variable objects, put them in an array, and I want to do a nested For Each loop iterating over each object in the array, then iterating over each pivot item in each array object. I keep getting a Run-time error '424': Object required at line 11 For Each PvI in Item
Here's the code:
Dim PvtTbl4, PvtTbl5 As PivotItems
Set PvtTbl4 = Worksheets("Main").PivotTables("PivotTable4").PivotFields("dayname").PivotItems
Set PvtTbl5 = Worksheets("Main").PivotTables("PivotTable5").PivotFields("dayname").PivotItems
Dim PivotArray As Variant
PivotArray = Array(PvtTbl4, PvtTbl5)
Dim PvI As PivotItem
Dim Item As Variant
For Each Item In PivotArray
For Each PvI In Item //<-- error
//do something
Next PvI
Next Item
i have the following function that im using to populate a userform listbox with data from an array:
Function PopulateListboxWithArray(lstbox As MSForms.ListBox, var As Variant)
With lstbox
If Not IsEmpty(var) Then
.Clear
.list = Application.Transpose(var)
.ListIndex = -1
End If
End With
End Function
My listbox contains two columns with the following properties:
PROBLEM
The data in the array has an ID column and a lastname column. I dont want the user to see the ID column so ive set that column width to 0 in the form.
When i import data that has more than one row, the data shows up in the listbox as expected.
However, when the array only contains one row of data, the listbox shows up blank !
I have tried deleting the columnwidths in the image above and when i do so and reimport the one row of data, i get the ID and lastname stacked on top of one another. But ofcourse this is not the result i want.
I have even tried replacing .list = Application.Transpose(var) with .list = var to no avail.
What am i doing wrong here, or is there a better way to populate a listbox?
cheers
I have found the answer in this post:Adding item in listbox with multiple columns
I needed to use the .List property My function now looks like this:
Function PopulateListboxWithArray(lstbox As MSForms.ListBox, var As Variant)
With lstbox
If Not IsEmpty(var) Then
.Clear
If UBound(var, 2) > 0 Then
.list = Application.Transpose(var)
Else
.AddItem var(0, 0)
.list(.ListCount - 1, 1) = var(1, 0)
End If
End If
End With
End Function
edit to add more "background"
not so sure why you're using Application.Transpose()
With lstbox
If Not IsEmpty(var) Then
.Clear
If UBound(var, 1) = 1 Then
.AddItem
.List(0, 0) = var(1, 1)
.List(0, 1) = var(1, 2)
Else
' .List = Application.Transpose(var)
.List = var
End If
.ListIndex = -1
End If
End With
where I populated var in the following way:
Private Sub UserForm_Initialize()
Dim var As Variant
With Worksheets("LB") '<--| change "LB" to your actual sheet name
var = .Range("B1", .Cells(.rows.Count, "A").End(xlUp)).Value '<--| populate var with columns "A:B" cells values from row 1 down to column "A" last non empty row
End With
With Me.ListBox1
.ColumnCount = 2 '<--| set listbox columns count
.ColumnWidths = "0;144" '<--| set listbox columns width
End With
PopulateListboxWithArray Me.ListBox1, var
End Sub
The problem is that i need to hide some rows of the DataGrid, based on which monitor they are displayed.
Here is my code:
For Each row As DataRowView In DataGrid1.Items
cellValue = row.Item("Monitor")
If cellValue.StartsWith("B") Then
//the code i need
End If
Next
The DataGrid1.Items.Remove() or DataGrid1.Items.RemoveAt() can't be used, cause my ItemSource is in use when they are called.
I prefer changing its visibility to hidden or height to 0.
Sorry if this question is not in the right format or looks bad, this is my first one :P (any tips are welcome)
Thanks in advance
This should work for you:
row.Visibility = Windows.Visibility.Collapsed
P.S.:
In my scope the DataGrid was bound to a List(Of String) so i had to get that row first. So when using
DataGrid.Items(i) you will only get the item itself which was a String.
To get the related DataGridRow you have to use this function:
DataGrid.ItemContainerGenerator.ContainerFromIndex(IndexOfItem)
Changed my code to:
Dim numOfRows As Integer
Dim listA As New List(Of Integer)
Dim listB As New List(Of Integer)
Dim deleted As Integer = 0
For Each row As DataRowView In DataGrid1.Items
numOfRows += 1 'Adding up to total number of rows'
If row.Item("Monitor").ToString.StartsWith("A") Then
listA.Add(numOfRows - 1) 'Adding rows indexes to a list'
ElseIf row.Item("Monitor").ToString.StartsWith("B") Then
listB.Add(numOfRows - 1) 'Adding rows indexes to a list'
End If
Next
The reason i didn't use row.Delete() is because the For Each loop breaks if i change its item source on the run. Thus, I'm deleting the rows on another loop (one for each monitor):
Dim rowA As DataRowView
For Each litem As Integer In listA
litem -= deleted
'The indexes on the list were added in a sorted order'
'ex. {4,7,14,28,39} . So if i delete the fourth row'
'all of the rows that remain will have their index'
'decreased by one,so by using an integer that represents'
'the number of the deleted rows, i can always delete the'
'correct "next" row'
rowA = DataGrid1.Items.Item(litem)
rowA.Delete()
deleted += 1
Next
I have a ListView control set up in details mode, and on a button press I would like to retrieve all column values from that row in the ListView.
This is my code so far:
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
Dim items As ListView.SelectedListViewItemCollection = _
Me.ManageList.SelectedItems
Dim item As ListViewItem
Dim values(0 To 4) As String
Dim i As Integer = 0
For Each item In items
values(i) = item.SubItems(1).Text
i = i + 1
Next
End Sub
But values just comes out as an empty array. Any ideas? I just want the values array to be filled with the data of that ListView row.
Cheers.
Here's the single-line solution:
mylistview.Items.Cast(Of ListViewItem).Select(Function(lvi As ListViewItem)lvi.SubItems(1).Text).ToArray()
You need to iterate the SubItems, not the selected items. Fix:
If Me.ManageList.Items.Count <> 1 Then Exit Sub
Dim row As ListViewItem = Me.ManageList.Items(0)
Dim values(0 To row.SubItems.Count-1) As String
Dim i As Integer = 0
For Each item As ListViewItem.ListViewSubItem In row.SubItems
values(i) = item.Text
i = i + 1
Next
Just to check, are you aware that item.SubItems(1).Text will get the texts from the second column? And that since you're using SelectedItems it'll only look at the currently selected items in the ListView.
Edit:
Also, are you sure there will always just be a maximum of 5 selected items in the ListView?
Otherwise you'll have problems with
Dim values(0 To 4) As String
Old question, I know... maybe this helps for someones reference.. stumbled upon this question in a search for something else.
This works... not sure if the original poster somehow used an empy colelciton of selected items...
Also, dynamically resizing array based on selected items to overcome unforseen bugs...
Dim valueArray(mylistview.SelectedItems.Count - 1) As String
Dim i As Integer
For Each Item As ListViewItem In mylistview.SelectedItems
valueArray(i) = Item.Text
i += 1
Next
To get subitems, use item.subitems(1).text ... subitems(2).text.. etc
Cheers