Exploiting Multi-Select option in List Box

Unlike other blog, this won't be a long one. To get a quick overview of what list box are and what they can do, follow this link. To get an exhaustive and deep insight of Listbox in Excel-VBA, the provided link won't be effective. In that case, you need to practice and create a form loaded with listbox. Let me not get into all that. Else it will divert my attention in some other direction.

To make things easier, I have created a sample form with a Listbox. The values are populated from Sheet1, range varying from A1 to A8.

The sample Excel sheet values are as shown below.


RowSource property of ListBox shows range from A1 to A8. If you observe carefully, MultiSelect is set to frmMultiSelectSingle. This does not allow us to do multiple selection.




The value of MultiSelect was set to frmMultiSelectMulti ,as shown, to support multi-selection.

Apart from this, I added a command button "OK" in the form. When user clicks "OK", whatever is selected in listbox gets displayed. Following code snippet was updated to perform the activity.

Private Sub CommandButton1_Click()
Dim a As String
For list_count = 1 To ListBox1.ListCount
'ListBox1.ListCount gives a count of items in ListBox
'ListBox1.List(list_count - 1) returns the value for Index list_count-1
    If (ListBox1.Selected(list_count - 1) = True) Then a = a & ListBox1.List(list_count - 1) & " "
Next
MsgBox a
End Sub


On execution, I selected two rows as shown.



As soon as, I clicked "OK", I got the following Message.


Happy VBA..ing

Tips & Tricks : Looping through Controls in a Userform

This is quite interesting and goes well with user-form. To read and know more about user form follow these link1 and link2. Suppose we have a user-form and it has quite good number of controls. By controls, I mean TextBox, Radio Buttons, Selection Box etc. To simplify this, let's say we have number of Text Box. User has two enter non-numeric data in each and every box before he/she hits "Execute" command button.
The problem is how to ensure that every text box is filled with valid data.
The concept of looping through Controls in a user form is a savior. Below piece of code will simply our job.

'define a variable of control type
dim form_control_var as control
for each form_control_var in user_form_control
  if(typename(form_control_var)="TextBox") then
   if(istext(form_control_var.value)="False") then
     msgbox "Data format is wrong !!! Enter correct Data"
     form_control_var.setfocus
     exit sub
   end if
  endif
next 

This code snippet won't allow one to proceed until and unless input field of the text boxes (present on user_form_control form) are populated with text.

This prevents us from repeating lines of code to test data sanity. Hope it helps everybody.

Happy VBA..ing

Disabling Right Click in Excel VBA

Disabling right click in Excel is not a rocket science. But when I googled it, I found many answers. But none of them were precise or upto the point. Somewhere, too exhaustive and complex whereas other where not upto the mark. So, I thought why not add one more blog on the same.

This is quite easy.

1. Open an Excel -Workbook

2. Go to Developer Tab

3. Click on the "ThisWorkbook" in the left hand side of the Microsoft Visual Basic window







4. Select Workbook and SheetBeforeRightClick from the drop down list


5. Add "Cancel=True"  line in between Sub and End Sub


Try, right click in any sheet. Did you see anything??

Happy VBA..ing