if cell value 1 then listbox .listindex = value - vba

I have the following code:
If ws.cells(A,B).value = "1" then .ListBox1.List(.ListBox1.ListIndex,5) = Checkbox.name
but it prompts an error and i'm not sure how to fix it
essentially, what i'm trying to achieve is that if the cell (A,B) has a value of 1 then insert a name of the checkbox into listbox, listindex of 5.
all other previous listindexes are added by the following code:
.list(.listcount -1, 1) = ws.cells(C,D).value
.list(.listcount -1,1) = ws.cells (E,F).value

This is how you populate a ListBox with items.
For i = 1 to 5
With me.ListBox1
.AddItem i
End With
Next
So assuming you have ListBox1 in UserForm1, this will result to:
Let's say you want to replace 5 by 6, you use .List property like this:
Me.ListBox1.List(4, 0) = 6
Syntax: expression.List(pvargIndex, pvargColumn)
Again, when you use .List property, Index and Column is always 1 less the actual Item and Column.
Since we want to replace 5 by 6 in a ListBox with 1 Column and 5 items in it, the Index of 5 is 4 and it's Column is 0.
The result would be:
Now, what if you have multiple-column ListBox and you want to populate those Columns.
Say Column 2 of our ListBox1, we use this code.
For i = 1 To 5
With Me.ListBox1
.List(i - 1, 1) = "Item" & i
End With
Next
Take note of the Index which is i - 1 and the Column which is 1 (actual column count -1)
The result would be:
So let's go back to your question.
This line:
If ws.cells(A,B).value = "1" then .ListBox1.List(.ListBox1.ListIndex,5) = Checkbox.name
will fail since you are trying to populate Column 6 of your ListBox1 which I believe doesn't exist.
Also you used .ListIndex property which returns the currently selected item index.
I don't know where you are executing your code but initially, .ListIndex value is -1 since it will only have value on runtime when the user get to select an Item.
Now these lines:
.list(.listcount -1, 1) = ws.cells(C,D).value
.list(.listcount -1, 1) = ws.cells(E,F).value
actually just replaces the last item second column value.
First it passes ws.cells(C,D).Value to it then overwrites it with ws.cells(E,F).value.
Hope this anwers some of your questions a bit.

Related

Listbox population sometimes shows number instead of text value

I have a listbox in a userform. I use a SQL query to pull data and then populate the listbox. When I look at the query in SQL, it is fine and shows text in those fields. The listbox is showing these numbers though, 4 and 5, what I am assuming is the column name or fields value?
It should say "Landfill/Wellfield" and "O2 levels", so it is not a text length issue. This is all test data. The last column correctly shows "Sparkplugs/Ignition System"
The listbox has 7 columns of data.
Code for the listbox population:
lbx_SSM.Clear
Do Until XL_RSet.EOF = True
For i = 1 To 7
If i = 1 Then
lbx_SSM.AddItem (XL_RSet.Fields(columnName(i)))
Else
If Not (XL_RSet.Fields(columnName(i)) = "Null")
Then lbx_SSM.List(lbx_SSM.ListCount - 1, i - 1) =(XL_RSet.Fields(columnName(i)))
End If
Next i
XL_RSet.MoveNext
Loop
I figured it out, I am not sure what the issue was, but using this to populate the listbox instead of the above does not cause the issue. I've used that first code all the time before so it might be a Fields() issue or something. See below:
XL_RSet.Open
XL_RSet.MoveFirst
i = 0
With Me.lbx_SSM
.Clear
Do
.AddItem
.List(i, 0) = XL_RSet![ID]
.List(i, 1) = XL_RSet![Unit]
.List(i, 2) = XL_RSet![Begin Time] ***Etc for other columns
i = i + 1
XL_RSet.MoveNext
Loop Until XL_RSet.EOF
End With

VBA ComboBox Value by Index

I've got a form that contains a combobox which contains the values
With io6
.AddItem "90°"
.AddItem "180°"
.AddItem "270°"
.AddItem "360°"
End With
The member of this method I am actually using is .ListIndex. When a user selects 270 degrees, I am sending a 2 to a variable. When I read the variable, I want have the combobox correctly show the appropriate value. In other words, if the variable is 1, I want the combo box to show 180 degrees. Is that achievable without a select\if statement that writes into .Value?
Sub varChange(val as integer)
With comboBox
Select Case val
Case 1
.value = "90°"
Case 2
.value = "180°"
Case 3
.value = "270°"
Case 4
.value = "360°"
End Select
End With
End Sub
The most direct I can ask this question is, can the method element be set based on the index? Can I set the combo box element based on the index rather than having to write in a new value?
I'm not sure if I got it right, but I think you want to get the list index of the selected item.
You can get that by
x = io6.ListIndex + 1
The +1 is there since ListIndex is 0 based, but you wanted it 1 based.
And something completely different,
Remove the brackets. Method calls without return values does not use them in VBA
Edit after comment
To take this the other way, i.e getting a value from an index value, do like this:
y = io6.List(x - 1)
sValue = combobox.List(combobox.ListIndex,0)
The most direct I can ask this question is, can the method element be set based on the index? Can I set the combo box element based on the index rather than having to write in a new value?
Yes,
I used the following line:
io6.ListIndex = val - 1
So in your code it's:
Sub varChange(val as integer)
comboBox.ListIndex = val - 1
End Sub

Finding last items of a multi-column listview

I have a multi-column listview that a colleague created in a useform. It gets populated once the useform opens. I want to find the items in the last row of the populated listview. For example:
header1 header2 header3
11 12 13
21 22 23
31 32 33
I want a line (or a few lines) to store 31, 32, and 33 in separate variables.
My ListView is named lsvLots. I am capable of storing 31 in the String strProduct with the following:
strProduct = lsvLots.ListItems.Item(lsvLots.ListItems.Count)
HoweverI can only returns the last item of the first column that way. How can I access the other columns?
The last line in the list is determined by the ListCount and can be referenced as follows:
UserForm1.lsvLots.List(UserForm1.lsvLots.ListCount - 1, 0)
Note, that the ListCount will be 1 if there is one row in the ListBox. Yet, the index to reference this (first & last row) starts with 0 and not with 1. Therefore, you have to subtract 1 from the ListCount to convert it to an index (starting with 0).
The 0 at the end means, that you want to get the first column from that row. Once again, the columns are 0 for the first column, 1 for the second column, 2 for the third column, etc.
In short, to get all the items from the last row you should use:
strProduct1 = UserForm1.lsvLots.List(UserForm1.lsvLots.ListCount - 1, 0)
strProduct2 = UserForm1.lsvLots.List(UserForm1.lsvLots.ListCount - 1, 1)
strProduct3 = UserForm1.lsvLots.List(UserForm1.lsvLots.ListCount - 1, 2)
OR (to be neater):
With UserForm1.lsvLots
strProduct1 = .List(.ListCount - 1, 0)
strProduct2 = .List(.ListCount - 1, 1)
strProduct3 = .List(.ListCount - 1, 2)
End With
Try this
strProduct = lsvLots.ListItems(lsvLots.ListItems.Count).SubItems(1)

Range based on cell value

I need code to change the range based on a cell value:
I can get it to work where the row number is dependent on the cell value as shown below, but I need the column value to be variable:
For Nassets = 1 To ws_data.Range("d2")
ws_data.Range("B" & Nassets).Value = 3
Next Nassets
If "d2" have the value 4, range B1:B4 = 3, however I want range B4:E4 = 3
Thanks in advance!
Based on the comments below, it look like you need to change the code so that the value in D2 represents the column within your loop, not the row - In which case:
For Nassets = 1 To ws_data.Range("d2")
ws_data.Cells(4, Nassets).Value = 3 '// Where 4 is the row number
Next Nassets
This can be re-written to exlude the loop altogether like so:
ws_data.Range(Cells(4, 2), Cells(4, ws_data.Range("D2").Value)).Value = 3

Unable to select values in second column listview

I've a listview that dynamically populates values between two columns in this fashion:
column 1 | column 2
value1 | value 2
value3 | value 4
however I'm unable to select any values in column 2 - is there a property of the listview that's preventing me from doing this or is it the way I populate these columns? Here's my code to populate the column:
For k = 0 To UBound(tempValues)
Dim itm As New ListViewItem(tempValues(k))
If k Mod 2 = 0 Then
listview1.AutoResizeColumns(ColumnHeaderAutoResizeStyle.HeaderSize)
itm.SubItems.Add(tempValues(k + 1))
listview1.Items.Add(itm)
End If
listview1.AutoResizeColumns(ColumnHeaderAutoResizeStyle.HeaderSize)
Next
Any ideas?
The closest I can see for this is to set listView1.FullRowSelect = true (I assume you have listView1.View = View.Details?)
This however will only give you full row selecting - Remember the 2nd column represents the 1st Sub Item of the listview's items.
If you want multiple columns of data, you might be better off setting listView1.View = View.Details = View.List, which will cause it to wrap a single list of items onto multiple columns when it runs out of vertical space.
Edit:
If you use listView1.View = View.List, your population would need to change to the following:
For k = 0 To UBound(tempValues)
listview1.Items.Add(new ListViewItem(tempValues(k))
Next
listview1.AutoResizeColumns(ColumnHeaderAutoResizeStyle.HeaderSize)
But it would mean you end up with the list like so:
Value 1
Value 2
Value 3
Value 4
And if ListView was made too short to display, all these, it would wrap them:
Value 1 Value 4
Value 2
Value 3