i have two Listboxes in Access VBA.
I want to compare These two listboxes and want to delete Items from the second listbox, if the same Item isn't listed at the first listbox.
For example:
Listbox 1 Values: "Item 1", "Item 3"
Listbox 2 Values: "Item 1", "Item 2", Item 3"
Now i want a function that compares These two Listboxes and deletes "Item 2" from the Listbox 2, because it isn't listed in the Listbox 1.
I tried some code, but the only Thing i've got is this one:
If BR_TeamReport.ListCount > 0 Then
For i = 0 To BR_TeamReport.ListCount - 1
For y = 0 To BR_Team.ListCount - 1
If BR_TeamReport.ItemData(i) = BR_Team.ItemData(y) Then
MsgBox ("Don't Delete")
Else
MsgBox ("Delete")
End If
Next y
Next i
End If
Consider this:
If BR_TeamReport.ListCount > 0 Then
For i = 0 To BR_TeamReport.ListCount - 1
FoundItem = False
For y = 0 To BR_Team.ListCount - 1
If BR_TeamReport.ItemData(i) = BR_Team.ItemData(y) Then
FoundItem = True
End If
Next y
If Not FoundItem Then
' Delete item as it was not found in the first listbox
BR_Team.RemoveItem (y)
End If
Next i
End If
I think this will achieve what you want.
Why not clear 2nd list box, and then repopulate with first one?
For i = 0 To LstBox2.ListCount - 1
LstBox2.RemoveItem(i)
next i
For i = 0 TO LstBox1.ListCount - 1
lstBox2.AddItem LstBox1.ItemData(i)
Next i
Related
The first column in a DataGridView (dgv1) has checkboxes. Interestingly, when you click on the last checkbox desired, as soon a you loop through the rows for that column to evaluate checked items, the last cell checked (with a cursor on it) does not evaluate to true. The syntax for looping through the checkboxes in column 0 is below:
Dim NumCBChecked As Integer = 0
For i = 0 To dgv1.Rows.Count - 1
If CBool(dgv1(0, i).Value) = True Then
NumCBChecked += 1
End If
Next
Is there a way to set the focus to false if the cell containing the last checkbox checked is in edit mode?
BTW, refreshing the edit status (below) did not help before looping:
dgv1.RefreshEdit()
Per #JohnG's comment, the suggestion worked as follows:
dgv1.EndEdit()
Dim NumCBChecked As Integer = 0
For i = 0 To dgv1.Rows.Count - 1
If CBool(dgv1(0, i).Value) = True Then
NumCBChecked += 1
End If
Next
I need to select a list box on a form in Excel which contains both numbers and words. I need to use the userform code to Count many numbers are in the selected ListBox. I am using this code:
Private Sub RUNButton_Click()
S = 0
For i = 0 To ListBox1.ListCount - 1
If IsNumeric(ListBox1.List(i)) = True Then
S = S + ListBox1.List(i)
End If
Next
MsgBox ("Number quantity " & S)
End Sub
But this code sums the numbers, I want to count.
Like if there are 1, 3, 5, and 6 in the ListBox then msgBox would say 4
How I can get this?
I am trying to write a code that will loop through my selected listbox item and copy them to a specific column in my worksheet. I have 3 listboxes with all different listcounts (listbox1 has 4 choices, Listbox2 has 10 choices and listbox3 has 5 choices). I want the code to be able to loop through and copy the data on every row.
Here is an example of the results I am having:
Listbox1: item1 (selected) item2 (not selected) item3 (not selected) item 4 (not selected)
Listbox2: item1 (selected) (all other items not selected)
Listbox3: item1 to item5 (selected).
I would like to program to copy the selected items in the following way:
Column B (Refers to listbox1): Item 1; Item 1; Item 1; Item 1; Item 1: (Total of 5 times)
Column C (Refers to listbox2): Item 1; Item 1; Item 1; Item 1; Item 1: (Total of 5 times)
Column D (Refers to listbox3): Item 1; Item 2: Item 3: Item 4; Item 5
I have tried the code below, but it only copies the items 1 to items 5 of listbox3.
Private Sub Add_Level_Click()
Dim lItem As Long, lItem2 As Long, lItem3 As Long, ws As Worksheet
Set ws = Worksheets("Development Plan")
With ws
For lItem3 = 0 To Me.ListBox3.ListCount - 1
If Me.ListBox3.Selected(lItem3) Then .Cells(.Rows.Count, "D").End(xlUp).Offset(1) = Me.ListBox3.List(lItem3)
Next
For Item2 = 0 To Me.ListBox3.ListCount - 1
If Me.ListBox2.Selected(lItem2) Then .Cells(.Rows.Count, "C").End(xlUp).Offset(1) = Me.ListBox2.List(lItem2)
Next
For Item = 0 To Me.ListBox3.Selected(Item)
If Me.ListBox1.Selected(lItem) Then .Cells(.Rows.Count, "B").End(xlUp).Offset(1) = Me.ListBox1.List(lItem)
Next
Screenshot of Desired outcome:
Userform Selection:
I don't know what happens if more than one item is selected in all boxes, but on the assumption that only one is selected in the first two boxes, try this, amending control/button/ranges names as appropriate.
Private Sub CommandButton1_Click()
Dim i As Long, j As Long
With Me
For i = 0 To .ListBox3.ListCount - 1
If .ListBox3.Selected(i) Then
Range("C" & Rows.Count).End(xlUp)(2).Value = .ListBox3.List(i)
j = j + 1
End If
Next i
For i = 0 To .ListBox1.ListCount - 1
If .ListBox1.Selected(i) Then
Range("A" & Rows.Count).End(xlUp)(2).Resize(j).Value = .ListBox1.List(i)
End If
Next i
For i = 0 To .ListBox2.ListCount - 1
If .ListBox2.Selected(i) Then
Range("B" & Rows.Count).End(xlUp)(2).Resize(j).Value = .ListBox2.List(i)
End If
Next i
End With
End Sub
When I run code below, the selected row returned is always from the bottom up. How can I get it to loop from the top of the Data Grid table each time?
'Find the selected customer by code. Display closest match in grid.
Dim targetString As String = txtAccountCode.Text
For Each row As DataGridViewRow In frmCustomerLookUp.GSCUSTDataGridView.Rows
If row.Cells(0).Value.ToString().StartsWith(targetString) Then
frmCustomerLookUp.GSCUSTDataGridView.ClearSelection()
frmCustomerLookUp.GSCUSTDataGridView.Rows(row.Index).Selected = True
frmCustomerLookUp.GSCUSTDataGridView.FirstDisplayedScrollingRowIndex = frmCustomerLookUp.GSCUSTDataGridView.SelectedRows(0).Index
Dim selectedIndex = frmCustomerLookUp.GSCUSTDataGridView.SelectedRows(0).Index
frmCustomerLookUp.GSCUSTDataGridView.Rows(selectedIndex).Selected = True
frmCustomerLookUp.GSCUSTDataGridView.Rows(selectedIndex).Cells(0).Selected = True
Exit Sub
End If
Next
Use a For...Next loop, starting at the last row and stepping -1
For index As Integer = frmCustomerLookUp.GSCUSTDataGridView.Rows.Count - 1 To 0 Step -1 ' Or Count -2
If frmCustomerLookUp.GSCUSTDataGridView.Rows(index).Cells(0).Value.ToString().StartsWith(targetString) Then
As the other person said. (Just a bit shorter)
For x=frmCustomerLookUp.GSCUSTDataGridView.Rows.Count - 1 To 0 Step -1
if frmCustomerLookUp.GSCUSTDataGridView(0,x).ToString().StartsWith(targetString) then
'something
end if
next
I have a Userform in which I submit some data, from the data my box populates with more data, and after I select the data from that box I need to select data from for that item from a third box. The data gathered is from a PivotTable.
Box1 is just a supplied combobox while 2 and 3 are directly from a PivotTable. I have the functionality for Box2 working based off Box1 but that's because they are separated by sheets.
The goal is that Box1 can = A,B,C then if Box1=A then Box2 can = 1,2,3 then if Box2 = 1 then Box3= x,y,z.
The problem being in Box3 it returns the information for Box2= 1,2 AND 3 rather than just 1.
My current code is:
lCommentCount = Sheets(pt).PivotTables("Pivottable1").TableRange2.Rows.Count
For i = 1 To lCommentCount
If Me.ptDatabox.Value = Sheets(pt).PivotTables("PivotTable1").PivotFields("ptData").LabelRange.Offset(i, 0).List Then
OEEDataEntry.commentbox.AddItem Sheets(pt).PivotTables("PivotTable1").PivotFields("ptData").LabelRange.Offset(i, 1)
End If
Next i
For i = Me.commentbox.ListCount - 1 To 0 Step -1
If Me.commentbox.List(i) = "" Or Me.commentbox.List(i) = "Grand Total" Or Me.commentbox.List(i) = ("(blank)") Then
Me.commentbox.RemoveItem (i)
End If
Next i
I have been toying around with this all day and found that you have to make the second row visible. This code works well. Note showing the detail BEFORE counting.
Sheets(pt).PivotTables("PivotTable1").PivotFields("ptData").ShowDetail = _
True
For i = 1 To lCommentCount
pti = Sheets(pt).PivotTables("PivotTable1").PivotFields("ptData").LabelRange.Offset(i, 0).Value
If Me.ptDatabox.Text = pti Then
OEEDataEntry.commentbox.AddItem Sheets(pt).PivotTables("PivotTable1").PivotFields("ptData").LabelRange.Offset(i, 1)
End If
Next i
For i = Me.commentbox.ListCount - 1 To 0 Step -1
If Me.commentbox.List(i) = "" Or Me.commentbox.List(i) = "Grand Total" Or Me.commentbox.List(i) = ("(blank)") Then
Me.commentbox.RemoveItem (i)
End If
Next i
If for whatever reason you want to change the information given in the first box then you would need to make it so that on reexit of that first box that you collapse all the bullets so Sheets(pt).PivotTables("PivotTable1").PivotFields("ptData").ShowDetail = False
but that needs to be before you start counting items in the list.