Search code examples
excelvbauserform

Using multiple instances of single Userform


I have a data collection project where users can edit entries. The edit function loads data from various worksheets into a UserForm. I'd like to allow the user to open multiple forms at the same time to edit multiple entries. Id doesn't seem like Excel can do this. The below code puts all the unique entries selected into a collection. I have tried a couple of ways to call the elements in the collection. Excel will only open the UserForm and wait until it's closed to open the next one in the collection. How can I open all of them at once?

Option Explicit

Public editcoll As Collection

Public Sub Edit_Entry()

Dim dc As Worksheet, pr As Worksheet, con As Worksheet, pf As Worksheet
Dim ldc As Worksheet, lpr As Worksheet, lcon As Worksheet
Dim req As Object
Dim i, j, k, l, x, rw, rwary()
Dim clm As Long, crw As Long, prw As Long
Dim prgn As String, vari As String, vsplit() As String, vsplit2() As String, poc As String
Dim threshname As String, threshunits As String, objunits As String
Dim msg As Integer, prgid As String
Dim arybool As Boolean, bl As Boolean
Dim dte As Date
Dim newform As DE_Form, nf As Object, nf2 As Object, nf3 As Object

'code execution timer
Dim dTime As Double
dTime = MicroTimer

'Sets worksheet variables
Set ldc = ThisWorkbook.Worksheets("Latest_DC")
Set lpr = ThisWorkbook.Worksheets("Latest_PR")
Set lcon = ThisWorkbook.Worksheets("Latest_CON")
Set pf = ThisWorkbook.Worksheets("PF")

If Left(ActiveSheet.name, 6) = "Latest" Then
    Set dc = ThisWorkbook.Worksheets("Latest_DC")
    Set pr = ThisWorkbook.Worksheets("Latest_PR")
    Set con = ThisWorkbook.Worksheets("Latest_CON")
Else
    Set dc = ThisWorkbook.Worksheets("DC")
    Set pr = ThisWorkbook.Worksheets("PR")
    Set con = ThisWorkbook.Worksheets("CON")
End If

dc.AutoFilterMode = False
pr.AutoFilterMode = False
con.AutoFilterMode = False
pf.AutoFilterMode = False
ldc.AutoFilterMode = False
lpr.AutoFilterMode = False
lcon.AutoFilterMode = False

Set editcoll = New Collection

'builds array of selected row numbers from largest to smallest
If Selection.Rows.Count > 1 Then
    i = 0
    If ActiveCell.Row = Selection.Rows(1).Row Then
        For rw = Selection.Rows.Count To 1 Step -1
            ReDim Preserve rwary(0 To i)
            rwary(i) = Selection.Row + Selection.Rows.Count - i - 1
            i = i + 1
        Next rw
    Else
        For rw = 1 To Selection.Rows.Count
            ReDim Preserve rwary(0 To i)
            rwary(i) = Selection.Row + Selection.Rows.Count - i - 1
            i = i + 1
        Next rw
    End If
Else
    'if only one row/cell is selected then sets array for that row
    ReDim rwary(0)
    rwary(0) = ActiveCell.Row
End If
code

    '************Loads Form******************
    Set newform = New DE_Form
    newform.Tag = dc.Cells(rw, "d").value & " " & dc.Cells(rw, "ae").value
    editcoll.Add Item:=newform, Key:=newform.Tag
    
'***********Bunch of code assigning form values


Next x

'First attempt to open 
'For Each newform In editcoll
'    newform.Show
'Next newform

'second attempt to open nultiple userforms at once
Select Case editcoll.Count
Case Is = 1:
    Set nf = editcoll(1)
    nf.Show
Case Is = 2:
    Set nf = editcoll(1)
    Set nf2 = editcoll(2)
    
    nf.Show
    nf2.Show
Case Is = 3:
    Set nf = editcoll(1)
    Set nf2 = editcoll(2)
    Set nf3 = editcoll(3)
    
    nf.Show
    nf2.Show
    nf3.Show
End Select


bottom:

Application.ScreenUpdating = True
Application.DisplayStatusBar = True
Application.EnableEvents = True
Application.DisplayAlerts = True

End Sub

Solution

  • You must set the modality of the forms, to open multiple forms after each other they must all be opened with the vbModeless argument, like so:

    nf.Show vbModeLess