I have a listbox with values that I want to move to another listbox so the user can sort the items. When the last item is selected, it only moves that item and erases the values above it. How can I have it function like the other items in the list?
FYI, it is a single item selection, if that changes anything
Option Explicit
Option Base 1
Private Sub Add_Click()
Dim x As Integer, count As Integer
count = Me.Unsorted.ListCount
For x = 0 To count
If Me.Unsorted.Selected(x) = True Then
Me.Sorted.AddItem Me.Unsorted.List(x)
End If
Next x
For x = count To 0 Step -1
If Me.Unsorted.Selected(x) = True Then
Me.Unsorted.RemoveItem x
End If
Next x
End Sub
you could use this
Private Sub Add_Click()
Dim x As Integer
Dim nSelecteds As Long
With Me
With .Unsorted
ReDim selecteds(1 To .ListCount) As Long
For x = .ListCount - 1 To 0 Step -1
If .Selected(x) Then
nSelecteds = nSelecteds + 1
selecteds(nSelecteds) = x
Me.Sorted.AddItem .List(x)
End If
Next x
If nSelecteds > 0 Then
For x = 1 To nSelecteds
.RemoveItem selecteds(x)
Next x
End If
End With
End With
End Sub
Related
I have multiple checkboxes (1 to 8) in Userform (VBA) and their values are mentioned in Ride.Caption (1 to 8). If I check any checkbox, then its value mentioned in Ride should add in Total Value and If I uncheck any checkbox then the value should deduct from the Total Value
As per the below code, I have added all values in Total. But, Checkbox coding is still pending, and I do not know how I can make this code complete. Help me out with your expertise.
Private Sub TotalValue_Click()
Dim X As Double
X = 0
If Len(Ride1.Caption) > 0 Then X = X + Ride1.Caption
If Len(Ride2.Caption) > 0 Then X = X + Ride2.Caption
If Len(Ride3.Caption) > 0 Then X = X + Ride3.Caption
If Len(Ride4.Caption) > 0 Then X = X + Ride4.Caption
If Len(Ride5.Caption) > 0 Then X = X + Ride5.Caption
If Len(Ride6.Caption) > 0 Then X = X + Ride6.Caption
If Len(Ride7.Caption) > 0 Then X = X + Ride7.Caption
If Len(Ride8.Caption) > 0 Then X = X + Ride8.Caption
TotalValue.Caption = X
End Sub
https://ibb.co/7jh7mJh
I check any checkbox, then its value mentioned in Ride should add in Total Value and If I uncheck any checkbox then the value should deduct from the Total Value
You can handle all your checkboxes in a loop. Also you do not need to deduct. Simply recalculate.
Is this what you are trying?
Private Sub CommandButton1_Click()
Dim TotalSum As Double
Dim i As Long
For i = 1 To 8
If Controls("Ride" & i).Value = True Then
TotalSum = TotalSum + Val(Controls("CheckBox" & i).Caption)
End If
Next i
TotalValue.Caption = TotalSum
End Sub
Is this what you're trying?
I've done it in a loop as it's cleaner and easier to handle.
You need to change "CheckBox" to whatever your checkboxes are called.
Private Sub TotalValue_Click()
Dim X As Double, i As Integer
X = 0
For i = 1 To 8
If Me.Controls("CheckBox" & i).Value = True And Len(Me.Controls("Ride" & i).Caption) > 0 Then
X = X + Me.Controls("Ride" & i).Caption
End If
Next i
TotalValue.Caption = X
End Sub
Alternatively, if you want your total to update whenever someone checks/unchecks a checkbox then you can add a change event for each of your textboxes like so:
Private Sub CheckBox1_Change()
If Me.CheckBox1.Value = True Then
Me.TotalValue.Caption = Me.TotalValue.Caption + Val(Me.Ride1.Caption)
Else
Me.TotalValue.Caption = Me.TotalValue.Caption - Val(Me.Ride1.Caption)
End If
End Sub
Again, just change "CheckBox1" to the relevant checkbox 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
I'm a VBA newcomer. I wonder why does sum1() show 10 while sum2() show 11? Thanks in advance.
Sub sum1()
Dim x As Integer
x = 0
For x = 5 To 5
x = x + x
MsgBox x
Next
End Sub
Sub sum2()
Dim x As Integer
x = 0
For x = 5 To 5
x = x + x
Next
MsgBox x
End Sub
A vba for-loop increments the index variable at the end of each iteration. That is what the Next keyword does. In sum1() You get:
Sub sum1()
Dim x As Integer
x = 0 ' x is 0
For x = 5 To 5 ' x set to 5
x = x + x 'x gets 5+5=10
Next 'x gets 10+1=11
MsgBox x 'display x=11
End Sub
but in sum2() you get:
Sub sum2()
Dim x As Integer
x = 0 ' x is 0
For x = 5 To 5 ' x is 5
x = x + x 'x gets 5+5=10
MsgBox x 'display x=10
Next 'x gets 10+1 and is now 11
End Sub
I agree with #MitchWheat, it usually not good practice to modify your index variable while inside a loop. A better approach would be this:
Sub sum3()
Dim x as Integer
Dim i as Integer
x= 1
For i = 5 To 5
x = x + x
Next
MsgBox x
End Sub
For loops increment the loop variable at the end of the loop.
In the second code snippet, the for loop increments x from 10 to 11, and then you display it.
Whereas the first code snippets increments x AFTER you display it. This is not something specific to VBA.
To see this, run:
Sub sum1_1()
Dim x As Integer
x = 0
For x = 5 To 5
x = x + x
MsgBox x
Next
MsgBox x
End Sub
As an aside: It's not best practice to modify loop counters from inside a loop. It can lead to code that is hard to understand.
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
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