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)
Related
I'm trying to read the number of characters in column "MRP element data" in tcode MD04.
Below code is giving me 1 but I want it to give 16 or it should give 16 provided that my code is right...If there is one more row with one more Purchase Order it should give 32 etc.
j = 0
Do
session.findById("wnd[0]/usr/subINCLUDE1XX:SAPMM61R:0750/tblSAPMM61RTC_EZ/txtMDEZ-EXTRA[5," & CStr(j) & "]").caretPosition = 0
objSheet.Cells(i, 2) = Len(Cstr(j))
Exit Do
j =j + 1
Loop
You currently calculate the length of the integer variable j which contains the value 0, it's why you get 1.
The table element you show is a GuiTableControl object. You will find the method GetCell to read the content of a cell, and after that you can calculate the length of its value.
Set tableControl = session.findById("wnd[0]/usr/subINCLUDE1XX:SAPMM61R:0750/tblSAPMM61RTC_EZ")
MsgBox Len(tableControl.GetCell(1,5).Text)
(row 1 = second row; column 5 = sixth column)
I'm having a certain layout like this:
0
0
0 5
1 6
1
0 7
1 8
1
0
0 9
1 10
1
0 11
1 12
Above is a list with combinations from 000, 001, 010, 011 to 111. The combinations all have a certain value, from 5 to 12.
(The enters are new rows, the spaces are indentlevels of cells, the 5, 6, 7, etc. are in a new column. The 0's and 1's are all in the same column.)
Now I need to have the sum of all the values in which the first 0/1 is 1, the sum of all the values in which the second 0/1 is 1, and the same for the last value. The results in this case must be: 42, 38 and 36
I can't find out how to programm this properly. I was hoping for something like this:
While not Sheets("Sheet 1").Cells(j, 1).indentlevel(2).Value = 0
sum = sum + cells(j,2)
j = j + 1
Wend
But obviously this doesn't work. I can't program this all out without loops, because the codes can be up to 5 didgets (ex. 01101)
Have you thought of using a for loop? Also I think you are using the IndentLevel syntax incorrectly.
With ThisWorkbook.Sheets("Sheet 1")
set rng = Range(.cells(1,1), .cells(3,10)) ' Change this to match your range
End With
For each c in rng
If c.IndentLevel = 2 Then
sum = sum + c
End If
Next c
This doesn't need VBA. The real problem is an inappropriate data format. Update your convention such that each indentation level is moved to a separate column. You'll then be able to proceed with simple Excel formulas.
I was wondering how to colour the first 8 rows of a datagridview. I have managed to sort the values in descending order and I wish to have the first 8 rows coloured to highlight the top 8 to the user, and I'm not sure how to go about doing this.
Dim count As Integer
For count = 0 To datagridsort.RowCount - 1
Do
datagridsort.Rows(0).Cells(0).Style.BackColor = Color.Coral
datagridsort.Rows(0).Cells(1).Style.BackColor = Color.Coral
Loop Until count = 8
Next
In the code you posted in your comment, you were never using the count variable. You were only updated the first row every time. Try it like this:
For i As Integer = 0 To Math.Min(datagridsort.RowCount - 1, 7)
For j As Integer = 0 To datagridsort.ColumnCount - 1
datagridsort.Rows(i).Cells(j).Style.BackColor = Color.Coral
Next
Next
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.
I have a table with a string field where than can be one number or mulitple numbers (delimited by a comma). I need to find the difference between the values (when converted to an integer) and an unspecified value. For simplicity sake for this question, I'll just say the value to be compared is a static value of 10.
Example Table:
iId vchStringNumbers vchSubtractedStringNumbers
1 20, 30, 40
2 50
3 20
Desired Results:
iId vchStringNumbers vchSubtractedStringNumbers
1 20, 30, 40 10, 20, 30
2 50 40
3 20 10
Is there a way to accomplish this in SQL? If it would be eaiser in excel or something like that, feel free to answer as well.
If one has access to Excel 2019 or Excel O365, one could also use (in B2):
=TEXTJOIN(", ",,FILTERXML("<t><s>"&SUBSTITUTE(B2,",","</s><s>")&"</s></t>","//s")-10)
Place you CS data in an Excel column, Select the cells and run this tiny VBA macro:
Sub SubtractCSV()
Dim r As Range
For Each r In Selection
ary = Split(r.Value, ",")
For i = LBound(ary) To UBound(ary)
ary(i) = CLng(Trim(ary(i))) - 10
Next i
r.Offset(0, 1).Value = Join(ary, ",")
Next r
End Sub