Focus Rectangle Appears on All Listbox Items in VBA - excel

I've created a Userform in Excel VBA, on which there is an unbound Listbox that has its MultiSelect property set to Extended. When that listbox receives focus by any means other than clicking a list item, all items in that list appear with the dotted focus rectangle around them.
Here is some code that shows the phenomenon beside another listbox with MultiSelect set to Single for comparison. Create a Userform, put two Listboxes on it, and add the code to the form. When you launch the form, tab between listboxes to see what I've described.
Private Sub UserForm_Activate()
ListBox1.MultiSelect = fmMultiSelectSingle
ListBox2.MultiSelect = fmMultiSelectExtended
Dim i As Integer
For i = 1 To 15
ListBox1.AddItem String(i, Chr(i + 64))
ListBox2.AddItem String(i, Chr(i + 64))
Next
End Sub
Is there a way to remove the focus rectangles or prevent their appearing?
Thanks,

I have experimented with your code in Excel 2010 and confirm your observation. If I create two list boxes, enter the code provided, start the form and press tab to focus on ListBox2, the dotted lines appear around all rows.
If I create the two list boxes as before, manually set ListBox2/Properties/Multiselect to 2 - fmMultiSelectExtended, run and tab to ListBox2 the nasty lines disapperar.
For me this is rather stable, the form now survives multiple window activation changes, jumpng back/forth etc.
don't ask me why ...

Related

How to Start Cursor After 3rd character of text box in UserForm

I am fairly new to VBA so any help would be greatly appreciated.
I have a UserForm that contains 3 labels and 3 text boxes. 2 of the text boxes (including the first one that is activated upon the form's activation) are empty. The third has 3 characters pre-filled into it. When the userform starts, I would like to fill in the first text box as normal then when I tab into the second text box, I would like the cursor to automatically go to the end of that 3 letters instead of highlighting the entire text box. I have looked around but cannot seem to find anything that can do this easily. Could you please let me know where exactly (which sub) to enter this code into as well?
My UserForm is called 'GetBIInfo2' and my text box is called 'NBIDText'
Thanks!
Select the textbox.
Go to property (F4).
Scroll till you find EnterFieldBehavior property.
Change it to 1 - FmEnterFieldBehaviorRecallSelection.
The direct approach is via the textbox'es .SelStart property; possibly you might call it via a sub procedure like this:
Sub fillAnyTextBox(myTextBox As MSForms.TextBox, ByVal firstCharacters As String)
With myTextBox
.Value = firstCharacters
.SetFocus
.SelStart = Len(firstCharacters)
.SelLength = Len(.Text) - Len(firstCharacters)
End With
End Sub
A simple alternative would be to make another text box to hold the first three characters and have the user enter the following characters into a separate textbox. Then you can write code that will take the contents of both and append them into a single string like this:
Dim finalString As String
finalString = TextBox1.Value & TextBox2.Value

Application.EnableEvents = False but textbox_Enter sub still firing in multipage userform VBA

I have a multipage userbox called MultiPage1 (as per default).
This comprises 6 pages, all of which have hundreds of textboxes on them, and they all work perfectly...
I have one problem. When Multipage.Value = 3 it seems to launch a textbox_Enter event, no matter what I do to try to ensure that the textbox doesn't have the focus throughout the serial.
I of course tried Application.EnableEvents = False, but I believe that userforms do not trigger events in the traditional sense of the term.
I cannot for the life of me figure out the problem. But it can be replicated on all of the machines that I have tried.
Interestingly.. when I renamed the troublesome textbox by deleting it, and replacing it with a new textbox with the exact same name.. the problem shifted to the next textbox in the sequence.
TextBoxTEST1_Enter no longer fires and now TextBoxTEST2_Enter fires instead.
With over 300 textboxes, I am loath to re-create them all!
Any ideas?!
Many thanks,
Phil
Avoid automatic textbox activation
A simple work around consists in assigning zero .Width, zero .Height and zero .TabIndex to only one dummy textbox per page.
A possible naming convention could be Dummy1 on 1st page, Dummy2 on 2nd page etc.
Code example
Private Sub UserForm_Initialize()
Dim i&
For i = 1 To Me.MultiPage1.Pages.Count
Me.Controls("Dummy" & i).Width = 0
Me.Controls("Dummy" & i).Height = 0
Me.Controls("Dummy" & i).TabIndex = 0
Next i
End Sub
Side note
Trying a similar approach moving the dummy textbox out of sight (e.g. via a negative .Left property) fails, as
it shows a blinking cursor at the left page border independant from the chosen value.

Multiple command buttons in sequence with one another

I am new to programming and I want to create an application for basketball coaches to be able to keep stats of their players by using VBA in Excel with command buttons.
Instead of manually inputting stats for each player in a game, I want to be able to click a command button with a players name on it to select that player's row. Then click a button that will have an action on it and input a number.
I will have 12 buttons for the players and 40 buttons for the actions (e.g. pts, reb,stl,etc.)
For example: When I click the player's name button, it will select the row that the player's attribute are in. Then when I select the action (e.g. points), it will add the number 2 to the column labeled points.
I want to use the action buttons for all 12 players so only put the number in when i click the player's name button. So the "pts" button will work for all 12 player buttons. Overall, I want to make a stat sheet for the coaches with command buttons instead of moving the cursor and inputting the information in manually.
Any suggestions on how to do so? Thank you in advance.
Clancy
Some example code, using a module-scoped variable to store which player is being processed, might be:
Option Explicit
Private CurrentPlayerRow As Long
Sub PlayerA_Click()
CurrentPlayerRow = 3
End Sub
Sub PlayerB_Click()
CurrentPlayerRow = 4
End Sub
Sub PlayerC_Click()
CurrentPlayerRow = 5
End Sub
Sub Action1_Click()
'Update column D by adding 2 to the cell value
Cells(CurrentPlayerRow, "D").Value = Cells(CurrentPlayerRow, "D").Value + 2
End Sub
Sub Action2_Click()
'Update column G by adding an inputted number to the cell value
Cells(CurrentPlayerRow, "G").Value = Cells(CurrentPlayerRow, "G").Value + CLng(InputBox("Enter a number:"))
End Sub
(Not knowing anything about basketball scoring and/or statistics, I wasn't sure what sort of actions you wanted to process.)

Excel VBA: Making userform with dynamic comboboxes, content of which changes depending on data of parental combobox

The idea is to build a userform that have numerous "lines" of two comboboxes to choose from: one let choose a category (main filed) and the second one (dependent filed) let you choose subcategory for the category user have chosen in main field.
Number of this comboboxes pairs may be variable and chosen by user - for adding or deleting line of two comboboxes there are buttons on the top. When pressing "Add Field" button, userform adds new pair at the bottom and gives each combobox name with ordinal number at the end, e.g.:
Main_field_1, Dependent_field_2;
Main_field_2, Dependent_Field_2
and so on.
Visual example of userform
But there is a problem: if no name of combobox is predetermined, how can a code be written for them?
For example I want to register a change in Main_field to be able to run a code, that will fill in data for Dependent_field, based on Main_field value. But if this comboboxes were created by user with "Add field" button there was no code written for them like:
Private Sub Main_field_1_Change()
Select Case UserForm1.Main_field_1.Value
Case "Category1"
UserForm1.Dependent_field_1.AddItem "SubCategory_1"
UserForm1.Dependent_field_1.AddItem "SubCategory_2"
Case "Category2"
UserForm1.Dependent_field_1.AddItem "SubCategory_88"
UserForm1.Dependent_field_1.AddItem "SubCategory_99"
Case Else
End Select
End Sub
because name of main and dependent fields have variables in them, like Main_field_999 and Dependent_field_999 (the numbers at the end will be identical for each line) and as i understand there is code can exist like this:
Private Sub Main_field_XX_Change()
Select Case UserForm1.Main_field_XX.Value
Case "Category1"
UserForm1.Dependent_field_XX.AddItem "SubCategory_1"
UserForm1.Dependent_field_XX.AddItem "SubCategory_2"
Case "Category2"
UserForm1.Dependent_field_XX.AddItem "SubCategory_88"
UserForm1.Dependent_field_XX.AddItem "SubCategory_99"
Case Else
End Select
End Sub
The question is: how can a code be written to recognize a change in combobox, that have a variable name to then execute different piece of code, based on value in this combobox?

VBA UserForm Array

Currently, I have an Excel spreadsheet with some data and a command button that creates a UserForm that displays a subset of that data. The UserForm has been designed in a way that makes the data easier to view. The problem that I'm having is that I would like the command button to make multiple instances of my UserForm so that each form can show a different set of data.
I'm pretty new to VBA so any suggestions or just a place for me to get started would be greatly appreciated. Thanks.
Make sure that the ShowModal property of the UserForm is set to False as otherwise only one instance of the UserForm can be shown at once.
After that, it is as simple as:
Dim ufArray(0 To 4) As UserForm1
Dim i As Integer
For i = 0 To 4
Set ufArray(i) = New UserForm1
Next i
For i = 0 To 4
Load ufArray(i)
ufArray(i).Show
Next i
to show five independent copies of UserForm1

Resources