VBA Make a Listbox Loop - vba

I want to make a Listbox Loop but i get always a syntax error dont know why.
This is the actual Code:
'deselektion Listbox1
Dim i As Long
For i = 0 To ListBox1.ListCount - 1
ListBox1.Selected(i) = False
Next i
'deselektion Listbox2
Dim f As Long
For f = 0 To ListBox2.ListCount - 1
ListBox2.Selected(f) = False
Next f
.
.
.
.
and so on until listbox 10
I thought it would be nice to rewrite the code.
For g= 1 To 10
Dim i As Long
For i = 0 To "ListBox & (g).ListCount - 1"
ListBox & (g).Selected(i) = False
Next i
Next g
Can anybody help me out please?

You need to assign each listbox control to an index in a array of varients.
Dim listboxes(1 to 10) as Variant
Set listboxes(1) = Listbox1
...
Set listboxes(10) = Listbox10
Now you can Loop through the array you created with your code which I will borrow from above with some modifications
For g= 1 To 10
Dim i As Long
For i = 0 To listboxes(g).ListCount - 1
listboxes(g).Selected(i) = False
Next i
Next g

Related

how to get listbox selected item name?

How do I get the item name on listbox?
I have this code:
Dim x2 As Long
Dim OriginalCount2 As Long
'Store original ListBox count
OriginalCount2 = ListBox1.ListCount
'Temporarily hide ListBox (runs faster)
ListBox1.Visible = False
'Delete selected line items
For x2 = OriginalCount2 - 1 To 0 Step -1
If ListBox1.Selected(x2) = True Then MsgBox x2
Next x2
'Unhide ListBox
ListBox1.Visible = True
But it only gets the item index.
Would help to know what is event or action is triggering the code but this should get you started in the right direction:
Private Sub ListBox1_Click()
Dim LBItem As Long
For LBItem = 0 To ListBox1.ListCount - 1
If ListBox1.Selected(LBItem) = True Then
MsgBox (ListBox1.List(LBItem))
End If
Next
End Sub

How to loop through userform frames with variable object name

I am attempting to loop the a specific set of frames within each Multipage page in my VBA User_Form. However, it does not seem like I can use a variable object name with each frame control like I can with the pages.
I am getting an error
object doesn't support this property or method
at the following line
For Each cCont in Me.MultiPage1.Pages(PageName).Frames(DataFrame).Controls
My Code
Do While x <= Me.MultiPage1.Pages.Count
PageName = "Page" & CStr(x)
DataFrame = "DataFrame" & CStr(x)
For Each cCont In Me.MultiPage1.Pages(PageName).Frames(DataFrame).Controls
You actually can't iterate the way you would think you could.
First, you need to iterate through all Pages of your MultiPage1.
Second, loop through all Controls inside the current Page , and check if they are of type Frame, if they are you can iterate inside the Frame but the syntax is a little different (see in the code below).
Code
Option Explicit
Private Sub IterateIn_MultiPage()
Dim x As Long, j As Long
Dim cCont As Control
For x = 0 To Me.MultiPage1.Pages.Count - 1 ' <-- loop through all MultiPage Pages
For Each cCont In Me.MultiPage1.Pages(x).Controls ' <-- loop through controls of current page
If TypeOf cCont Is Frame Then ' <-- check if control type is Frame
For j = 0 To cCont.Controls.Count - 1 '<-- loop through all items related to the current Frame collection
MsgBox cCont.Controls(j).Name '<-- display a message box
Next j
End If
Next cCont
Next x
End Sub
Thanks for the help on putting together the code #Shai, for anyone else wondering what the final code looks like here it is. This loops through each frame within each multipage and pastes the caption of checked checkboxes in my desired range.
Option Explicit
Sub IterateIn_MultiPage()
Dim x As Long, j As Long
Dim cCont As Control
Dim counter As Integer
Dim y As Integer
Dim Range As String
y = 1
For x = 0 To ImportData.MultiPage1.Pages.Count - 1
counter = 0
For Each cCont In ImportData.MultiPage1.Pages(x).Controls
Do While counter < 1
If TypeOf cCont Is Frame Then
For j = 0 To cCont.Controls.Count - 1
If cCont.Controls(j).Value = True Then
Range = "E" & y
If counter = 0 Then
Worksheets("Calculations").Range(Range) = cCont.Controls(j).Caption
counter = counter + 1
ElseIf counter = 1 Then
Worksheets("Calculations").Range(Range) = Worksheets("Calculations").Range(Range) & " & " & cCont.Controls(j).Caption
y = y + 1
End If
End If
Next j
End If
Loop
Next cCont
Next x
End Sub

How can i describe buttons in matrix?

I have a 16*16 matrix and I'm trying to define them as a matrix series in vb.net. Then i make some visual shows with this matrix like led shows.
Dim n As Integer = 16
Dim l As Integer = 16
Dim x(n - 1, l - 1) As Integer
Dim i, j, k As Integer
For i = 0 To n - 1
For j = 0 To l - 1
For k = 1 To 256
If j= k mod16 then
buton(k) = x(i, j)
end if
Next
Next
Next***
I try to apply an algorithm. But it doesn't work. How can i accomplish this? Thanks for your interests..
OK I wrote something like this a while ago - I've adapted it to your needs and it runs ok on my pc. You need to create you array X like this
Dim x(15,15) As Button
To fill the array with buttons, use this method..
Private Sub InitializeArray()
Dim btnslist As New List(Of Button)
Dim btnNum As Integer
Dim btnName As String
Dim splitButtonName() As String
'add all the buttons to a list
For Each btnControl As Object In Controls
Dim btn As Button
'get button number and add it to the list if it is >0 and <=256
If TypeOf btnControl Is Button Then
btn = CType(btnControl, Button)
'get the button number
splitButtonName = Split(btn.Name, "n")
If CInt(splitButtonName(1)) > 0 And CInt(splitButtonName(1)) <= 256 Then
btnslist.Add(btn)
End If
End If
Next
'add the buttons to the matrix in the right order
For i As Integer = 0 To 15
For j As Integer = 0 To 15
For k As Integer = 0 To 255
btnNum = i * 16 + j + 1
btnName = "Button" & btnNum.ToString
If btnslist(k).Name = btnName Then
x(i, j) = btnslist(k)
Exit For
End If
Next
Next
Next
End Sub

how to execute for loop for items checked in 3 checkedlistbox

i made a report for taking output of employees in a company.i made a code for that.but it only show the first items checked.how to impliment for loop in this.
Dim i As Integer
Dim j As Integer
Dim k As Integer
For i = 0 To Employee_Bank_dtl.CheckedListBox1.Items.Count - 1 Step i + 1
If Employee_Bank_dtl.CheckedListBox1.GetItemCheckState(i) = CheckState.Checked Then
Dim xx As String = (CType(Employee_Bank_dtl.CheckedListBox1.Items(i), DataRowView))("VC_BRNAME")
For j = 0 To Employee_Bank_dtl.CheckedListBox2.Items.Count - 1 Step j + 1
If Employee_Bank_dtl.CheckedListBox2.GetItemCheckState(j) = CheckState.Checked Then
Dim yy As String = (CType(Employee_Bank_dtl.CheckedListBox2.Items(j), DataRowView))("vc_empstatus")
For k = 0 To Employee_Bank_dtl.CheckedListBox3.Items.Count - 1 Step k + 1
If Employee_Bank_dtl.CheckedListBox3.GetItemCheckState(k) = CheckState.Checked Then
Dim zz As String = (CType(Employee_Bank_dtl.CheckedListBox3.Items(k), DataRowView))("vc_value")
Dim str = "xxxxxxxxxxxxxx"
conobj.readdata(str)
conobj._adpt.Fill(Me.DataSet10.BRANCH_MAST)
Me.ReportViewer1.RefreshReport()
End If
Next
End If
Next
End If
Next i
You should increment through the collection of items like this not the way you are doing it. You should be able to find the properties you are looking for. Set a break point within the inner loop and right click on quickwatch on the item and you will see all the properties of that item which will contain what your looking for.
For Each item In CheckedListBox1.Items
'set the item property to the right property that holds "VC_BRNAME"
If item.property = "VC_BRNAME" Then
End If
If item.checkstate.checked = True Then
End If
Next
Its my mistake replacing k + 1 to +1 in the 3rd loop will got the right answer

Search DataGridView for Integer Then Select Row

The below code should search DataGridView1 which is on the LeaderAccessTable form for an integer that the user inputs into SendFromID, if the DataGridView1's first column contains what the integer that the user has entered into SendFromID then the entire row should be selected. However it doesn't select any rows at all... Can anyone see why? This code is ran from a separate form.
Dim intcount As Integer
For Each Row As DataGridViewRow In LeadersAccessTable.DataGridView1.Rows
If LeadersAccessTable.DataGridView1.Rows(intcount).Cells(0).Value.ToString = SendFromID.Text Then
LeadersAccessTable.DataGridView1.Rows(intcount).Selected = True
End If
Next
MsgBox("Done.")
In the end this code worked.
Dim v_SelectRow As Integer
For counter = 0 To (LeadersAccessTable.DataGridView1.Rows.Count - 1)
For counter2 = 0 To (LeadersAccessTable.DataGridView1.Columns.Count - 1)
If (LeadersAccessTable.DataGridView1.Rows(counter).Cells(0).Value.ToString.Contains(SendFromID.Text)) Then
LeadersAccessTable.DataGridView1.Rows(counter).Cells(0).Selected = True
v_SelectRow = LeadersAccessTable.DataGridView1.CurrentRow.Index
CurrentPoints.Text = LeadersAccessTable.DataGridView1.Item(8, v_SelectRow).Value
'Do Something
Else
'Do Something
End If
Next
Next