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
Related
Here's one that I'm stuck on.
I have a macro that opens all workbooks in a folder/directory one at a time, and performs certain actions, one of them is it unchecks every checkbox found in .Range("K25:U56"), but there is one checkbox in that range that I would like it to skip (or what I have it doing now is, store it's current value before the unchecking of all the boxes in the range happens, and then re-instate that value after all the unchecking happens, whichever). I receive "The item with the specified name wasn't found." using the below line:
' Store checkbox50's value to return later
Dim checkbox As Boolean
If sh.CheckBoxes("Check Box 50").Enabled = True Then checkbox = True
If sh.CheckBoxes("Check Box 50").Enabled = False Then checkbox = False
I've also tried:
' Store checkbox50's value to return later
Dim checkbox As Boolean
If sh.Shapes("Check Box 50").OLEFormat.Object.Value = True Then checkbox = True
If sh.Shapes("Check Box 50").OLEFormat.Object.Value = False Then checkbox = False
...and I get "Unable to get the Value property of the CheckBox class." Here is a screenshot of what I'm working with (notice Check Box 50 as the name?).
Any ideas? If you want the full code posted, just leave a comment, but I'm wondering if there's a different way of searching for that one particular checkbox? Thanks!
Thanks to #BigBen, we discovered the issue was because I was looping through all sheets in the workbook, but not every worksheet has a "Check Box 50" in it, so it was looking for something that didn't exist in those secondary sheets. I will work on my code to reflect this. Thanks!!!!
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.
I have a VBA form which is used to enter data on a sheet. I am currently coding the form so as it will load any data already existing in the sheet back into the form.
For simple text strings it works perfectly.
e.g.
ReqSetup.ReqText = Application.Worksheets("Req Sheet").Range("F11").Value
However, I have some combo boxes, that on the form, when they are selected will enter a number in the corresponding cell.
Fail 1. - Run Time Error 380 - Invalid property value.
ReqSetup.MinPerKgCB = Application.Worksheets("Req Sheet").Range("C27").Value
Fail 2.
Dim MinPerKg As Range
Set MinPerKg = Application.Worksheets("Req Sheet").Range("C27")
ReqSetup.MinPerKgCB = MinPerKg
I'm obviously doing something really simple wrong but I can't work out what it is!!
Kind Regards!
I have some combo boxes, that on the form, when they are selected will
enter a number in the corresponding cell
Then you'd need to do the opposite of your code attempt, i.e.:
Worksheets("Req Sheet").Range("C27").Value = ReqSetup.MinPerKgCB.Value
That you'd better wrap inside a check that any combobox value is actually selected :
With ReqSetup.MinPerKgCB
If .ListIndex <> -1 Then Worksheets("Req Sheet").Range("C27").Value = .Value
End With
I am trying to create a menu with list boxes in order to be able to select a number of customers from a list in an excel sheet. There are two list boxes, one with all the (default) data and one with the selected customers.
There is no problem adding one customer but when I add a second customer the graphic interface shows nothing, but after some debugging, the SelectedCustomers.RowSource still have the two rows in its data:
?SelectedCustomers.RowSource
$8:$8,$11:$11
This would have me believe there is some error with how I save the data or some limitations to Excel that I am not aware of. This is the code I use to save the data:
Private Sub CommandButton5_Click()
Dim temp As Range
For i = 0 To DefCustomers.ListCount - 1
If DefCustomers.Selected(i) = True Then
If temp Is Nothing Then
Set temp = Range(Rows(i + 4).Address)
Else
Set temp = Application.Union(temp, Range(Rows(i + 4).Address))
End If
End If
Next i
SelectedCustomers.RowSource = temp.Address
End Sub
Has someone experienced this before or know what the problem might be?
Instead of RowSource use AddItem method:
For i = 0 To DefCustomers.ListCount - 1
If DefCustomers.Selected(i) Then
SelectedCustomers.AddItem DefCustomers.Selected(i)
End If
Next i
There are known issues with ListBox.RowSource property in Excel VBA.
[EDIT]
After the discussion...
No matter of number of columns, you can copy rows from source sheet into another sheet, then bind SelectedCustomers listbox to that data
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 ...