I am stumped I tried the following and for the life of me I cant figure this out....Need to use a comand button on a listbox on an excel vba form.
Iniatilizing the form on load.....it load fine
Sub UserForm_Initialize()
UserForm1.LbNumbers.RowSource = "Sheet2!A1:A3"End Sub
Items display in listbox fine
I have a command button under the list box to select all the code
Sub CbSelectall_Click()
For i = 0 To LbNumbers.ListCount - 1
LbNumbers.Selected(i) = True
Next i
End Sub
If I click on button it jumps to the last line but it doesnt select all the numbers in the listbox. Can someone tell me how can I rectify it to select all the numbers in the listbox.Thank you
Private Sub lbTraderId_Change()
If ResetListBox(lbTraderId) Then
Exit Sub
ElseIf lbTraderId.Selected(0) Then
For i = 1 To lbTraderId.ListCount - 1
If lbTraderId.Selected(i) = False Then
lbTraderId.Selected(i) = True
End If
Next i
ElseIf lbTraderId.Selected(0) = False Then
For i = 1 To lbTraderId.ListCount - 1
If lbTraderId.Selected(i) = True Then
lbTraderId.Selected(i) = False
End If
Next i
End If
End Sub
Private Function ResetListBox(lbx As MSForms.ListBox) As Boolean
Dim x As Long
Static bExit As Boolean
If Not bExit Then
x = lbx.ListIndex
If x >= 0 And Not lbx Is Me.lbTraderId Then
bExit = True
lbx.Selected(x) = Not lbx.Selected(x)
bExit = False
ResetListBox = True
End If
End If
End Function
Related
Dears,
I am preparing a simple userform to let a teacher to enter each student’s name and height in a class. There are two textboxes (textbox_name and textbox_height) and one “add to record” command button. I tried to make some validation control to prevent empty string to be entered. My validation control is in case textbox is empty, when the click the “add to record” button, the corresponding empty textbox turns pink; and when both textboxes are empty, both textboxes should turn pink, however only the first textbox turns pink in my below codes. Can you tell me why and how to correct it? Thanks.
Private Sub Cmd_addtorecord_Click()
If TextBox_Name = "" Then
TextBox_Name.BackColor = rgbPink
Exit Sub
End If
If TextBox_height = "" Then
TextBox_height.BackColor = rgbPink
Exit Sub
End If
'....
End sub
You can do it like this:
Private Sub Cmd_addtorecord_Click()
'ensure required fields have content
If FlagEmpty(Array(TextBox_Name, TextBox_height)) > 0 Then
MsgBox "One or more required values are missing", vbExclamation
Exit Sub
End If
'....
End Sub
'check required textboxes for content, and flag if empty
' return number of empty textboxes
Function FlagEmpty(arrControls) As Long
Dim i As Long, numEmpty As Long, clr As Long
For i = LBound(arrControls) To UBound(arrControls)
With arrControls(i)
clr = IIf(Len(Trim(.Value)) > 0, vbWhite, rgbPink)
.BackColor = clr
If clr = rngpink Then numEmpty = numEmpty + 1
End With
Loop
FlagEmpty = numEmpty
End Function
You're Exiting the sub before you get to the second textbox. Change your code to something like this:
Private Sub Cmd_addtorecord_Click()
If TextBox_Name = "" or TextBox_height = "" Then
If TextBox_Name = "" Then
TextBox_Name.BackColor = rgbPink
End If
If TextBox_height = "" Then
TextBox_height.BackColor = rgbPink
End If
Exit Sub
End If
'....
End sub
I am wondering is there any solution to select / highlight every second pair in the list with some piece of code?
I have sat up listbox to MultiSelect:
And I want to achieve something like this:
By clicking button on UserForm:
Private Sub CommandButton1_Click()
' Select every second pair
End Sub
I have tried to play with:
Private Sub CommandButton1_Click()
' Select every second pair
sameCustomerComparison.Selected(1) = True
sameCustomerComparison.Selected(2) = True
sameCustomerComparison.Selected(5) = True
sameCustomerComparison.Selected(6) = True
End Sub
but it is giving an error... debugger pointing at sameCustomerComparison.Selected(1) = True
You could achieve this simply with a loop
Private Sub CommandButton1_Click()
Dim i As Long
With Me.ListBox1
.MultiSelect = fmMultiSelectMulti
For i = 0 To .ListCount - 1 Step 4
If i <= .ListCount Then .Selected(i) = True
If i + 1 <= .ListCount Then .Selected(i + 1) = True
Next i
End With
End Sub
I am currently trying work on a code that can loop through all the check boxes and uncheck a box if the box next to it is checked.
Currently, I have something written out, and I know it's no where near what I need it to be. I know how to do it individually by box:
Private Sub CheckBox1_Click()
If CheckBox1.Value = True Then
CheckBox2.Value = False
End If
End Sub
Private Sub CheckBox2_Click()
If CheckBox2.Value = True Then
CheckBox1.Value = False
End If
End Sub
However, I have several hundred lines of check boxes that I'd have to write this for so if I could just loop through the check boxes it would be a life saver!So I started out with:
Dim i As Integer
i = 1
a = 2
If CheckBoxi.Value = True Then
CheckBoxa.Value = False
End If
If CheckBoxa.Value = True Then
CheckBoxi.Value = False
End If
i = i + 2
a = a + 2
End
However this doesn't seem to work and I have no idea where to go from here. Any help would be greatly appreciated!
I have 2 DropDowns on an excel sheet which work independently of each other. The selections on both drop down's can cause confusion during reporting generation. So I need to set DropDown#1 to "select" if DropDown2 is being used by the user and vice versa.
I am trying to use the DropDown.Text property but it does not do the trick.
Sub PDropDown_Click()
Dim DropDownP As DropDown
Dim DropDownD As DropDown
Set DropDownD = Me.DropDowns("DDropDown")
Set DropDownP = Me.DropDowns("PDropDown")
DropDownD.Text ="Select"
DropDownP.Text = DropDownP.List(DropDownP.ListIndex)
Call Report_Generator.Create_Graph(DropDownP.List(DropDownP.ListIndex))
End Sub
You will have to add the value select to you combo boxes.
Dim bExit as Boolean
Private Sub ComboBox1_Change()
If bExit = True Then
bExit = False
Exit Sub
End If
bExit = True
ComboBox2.Text = "Select"
End Sub
Private Sub ComboBox2_Change()
If bExit = True Then
bExit = False
Exit Sub
End If
bExit = True
ComboBox1.Text = "Select"
End Sub
I have 60 check boxes on one worksheet. They are form control checkboxes. They all do a similar function which is hide rows. When you click the check box it shows the rows
When unchecked the rows are hidden. Is there an easy if else or case statement I could write for this:
Sub CheckBox1_Click()
If Range("B4").Value = True Then
Rows("5:62").EntireRow.Hidden = False
Else
Rows("5:62").EntireRow.Hidden = True
End If
End Sub
Sub CheckBox2_Click()
If Range("B63").Value = True Then
Rows("64:102").EntireRow.Hidden = False
Else
Rows("64:102").EntireRow.Hidden = True
End If
End Sub
Sub CheckBox3_Click()
If Range("B103").Value = True Then
Rows("104:129").EntireRow.Hidden = False
Else
Rows("104:129").EntireRow.Hidden = True
End If
End Sub
Sub CheckBox4_Click()
If Range("B130").Value = True Then
Rows("131:160").EntireRow.Hidden = False
Else
Rows("131:160").EntireRow.Hidden = True
End If
End Sub
Sub CheckBox5_Click()
If Range("B161").Value = True Then
Rows("162:183").EntireRow.Hidden = False
Else
Rows("162:183").EntireRow.Hidden = True
End If
End Sub
Sub CheckBox6_Click()
If Range("B184").Value = True Then
Rows("185:222").EntireRow.Hidden = False
Else
Rows("185:222").EntireRow.Hidden = True
End If
End Sub
Sub CheckBox7_Click()
If Range("B223").Value = True Then
Rows("224:244").EntireRow.Hidden = False
Else
Rows("224:244").EntireRow.Hidden = True
End If
End Sub
........etc etc
Also, if I need to add more rows in or delete rows, how would I code that in?
I would appreciate a format that I could use to create my own check boxes in the future without having to manually fix and edit them individually
You could use something like this:
Sub CheckBox1_Click()
HideRows "B63", "64:102"
End Sub
Sub HideRows(controllerRange, rowRange)
Rows(rowRange).EntireRow.Hidden = Not Range(controllerRange).Value
End Sub
You could also call HideRows directly from the checkbox.
In CheckBox1 assign macro, you would enter the following:
'HideRows "B63","64:102"'
Note: The single quotes MUST be entered as shown.