加载屏幕用户窗体

我已经尝试了无模式的每一个选项来实现这个工作,我只是无法弄清楚。 我有一个用户窗体(Main_Window)有一个脚本,并在命令button执行Master_Flex_Filter_Generate()

在那个代码中,我有

' Connect and execute the SQL rs.Open sqlString, conn, adOpenStatic ' Check if we have data. If Not rs.EOF Then ' Dump column names in first row. For i = 0 To rs.Fields.count - 1 ActiveSheet.Cells(1, i + 1) = rs.Fields(i).Name Next i Do ' Transfer results beginning at A2 from rs ActiveSheet.Range("A2").CopyFromRecordset rs Loop Until rs.EOF ' Close the recordset rs.Close ' Define Record_Count as the amount of records returned. Subtract 1 for the header Record_Count = Cells(Rows.count, "A").END(xlUp).Row - 1 ' Return box on success Answer = MsgBox((Record_Count & " Records returned") & vbCrLf & vbCrLf & "Reminder" & vbCrLf & "- This data set is already defined as a range. This allows you to create a new pivot table and use the named range: " & ActiveSheet.Name & vbCrLf & vbCrLf & "Do you want to create a Pivot Table from this data?", vbYesNo + vbQuestion, "Query generated succesfully") ' close user form Unload mod_loading Unload Main_Window ' Clean up If CBool(conn.State And adStateOpen) Then conn.Close Set conn = Nothing Set rs = Nothing 'Now lets define this data as a range for pivot table use Dim ActSheet As Worksheet Dim ActSheetName As String 'This sets up an object reference to the activesheet Set ActSheet = Sheets(ActiveSheet.Name) 'This places a string value in the variable ActSheetName = ActiveSheet.Name Set wb = ActiveWorkbook Set ws = ActiveSheet Set sheetname = ActiveWorkbook.ActiveSheet ws.Range("A1").Select ActiveWorkbook.Names.Add Name:="data", RefersToR1C1:= _ "=OFFSET(R1C1,0,0,COUNTA(C1),COUNTA(R1))" wb.Names("data").Name = ActSheetName 'Filter, freeze pain, and align columns Rows("1:1").Select Selection.AutoFilter Selection.Font.Bold = True Columns("A:IV").AutoFit With ActiveWindow .SplitColumn = 0 .SplitRow = 1 End With ActiveWindow.FreezePanes = True 'Take answer form msgbox and do something with it If Answer = vbYes Then 'If yes, create pivot table Sheets("Pivot").Select ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:= _ "data", Version:=xlPivotTableVersion14).CreatePivotTable TableDestination _ :="Pivot!R1C1", TableName:="PivotTable1", DefaultVersion:= _ xlPivotTableVersion14 Sheets("Pivot").Select Cells(1, 1).Select End If Else mod_loading.Hide MsgBox "Error: No records returned for your criteria.", vbCritical End If 

这种forms完美地工作在这里。 我有另一个用户窗体称为mod_loading,看起来像这样

 Sub UserForm_Activate() DoEvents Do mod_loading.loading_beag.Visible = False mod_loading.loading_splines.Visible = True Sleep 2000 DoEvents mod_loading.loading_splines.Visible = False mod_loading.loading_sheep.Visible = True Sleep 5000 DoEvents mod_loading.loading_sheep.Visible = False mod_loading.loading_meteor.Visible = True Sleep 5000 DoEvents mod_loading.loading_meteor.Visible = False mod_loading.loading_ozone.Visible = True Sleep 5000 DoEvents mod_loading.loading_ozone.Visible = False mod_loading.loading_terrain.Visible = True Sleep 5000 DoEvents mod_loading.loading_terrain.Visible = False mod_loading.loading_gravity.Visible = True Sleep 5000 DoEvents mod_loading.loading_gravity.Visible = False mod_loading.loading_advisor.Visible = True Sleep 5000 DoEvents mod_loading.loading_advisor.Visible = False mod_loading.loading_pool.Visible = True Sleep 5000 DoEvents mod_loading.loading_pool.Visible = False mod_loading.loading_leaks.Visible = True Sleep 5000 DoEvents mod_loading.loading_leaks.Visible = False mod_loading.loading_beag.Visible = True Sleep 5000 DoEvents Loop End Sub 

这两种forms的工作原样,我遇到的问题是放置mod_loading.Show Master_Flex_Filter_Generate()中的任何地方。 不pipe我为非模态设置mod_loading将运行,直到它完全结束。 它不会让Main_Window中的代码继续运行,因此会暂停该代码。 有任何想法吗? 我想这可能是因为我没有玩mod_loading.Show内循环,但由于我的连接string没有循环,我不知道该怎么做。

我把代码放在If Not rs.EOF THEN中,但是99%的代码加载在循环外的rs.Open上。

编辑

好吧,所以我已经改变了我的脚本,并把所有的SQL逻辑放在加载对话框的用户表单中。 所以我有Main_Window这是窗体,然后它从加载的用户窗体调用子popup弹得很好。 所以我现在popup加载对话框,并按照预期的方式消失。 我唯一的问题是现在,它不循环我的睡眠命令。 它仍然是第一个。

这是我的加载用户表单的代码

 Sub UserForm_Activate() 

DoEvents调用Master_Flex_Filter_Generate

  DoEvents If Cancel = True Then Unload loading_masterflexfilter Exit Sub End If loading_masterflexfilter.loading_beag.Visible = False loading_masterflexfilter.loading_splines.Visible = True Sleep 500 DoEvents If Cancel = True Then Unload loading_masterflexfilter Exit Sub End If loading_masterflexfilter.loading_splines.Visible = False loading_masterflexfilter.loading_sheep.Visible = True Sleep 1000 DoEvents If Cancel = True Then Unload loading_masterflexfilter Exit Sub End If loading_masterflexfilter.loading_sheep.Visible = False loading_masterflexfilter.loading_meteor.Visible = True Sleep 1000 DoEvents If Cancel = True Then Unload loading_masterflexfilter Exit Sub End If loading_masterflexfilter.loading_meteor.Visible = False loading_masterflexfilter.loading_ozone.Visible = True Sleep 1000 DoEvents If Cancel = True Then Unload loading_masterflexfilter Exit Sub End If loading_masterflexfilter.loading_ozone.Visible = False loading_masterflexfilter.loading_terrain.Visible = True Sleep 1000 DoEvents If Cancel = True Then Unload loading_masterflexfilter Exit Sub End If loading_masterflexfilter.loading_terrain.Visible = False loading_masterflexfilter.loading_gravity.Visible = True Sleep 1000 DoEvents If Cancel = True Then Unload loading_masterflexfilter Exit Sub End If loading_masterflexfilter.loading_gravity.Visible = False loading_masterflexfilter.loading_advisor.Visible = True Sleep 1000 DoEvents If Cancel = True Then Unload loading_masterflexfilter Exit Sub End If loading_masterflexfilter.loading_advisor.Visible = False loading_masterflexfilter.loading_pool.Visible = True Sleep 1000 DoEvents If Cancel = True Then Unload loading_masterflexfilter Exit Sub End If loading_masterflexfilter.loading_pool.Visible = False loading_masterflexfilter.loading_leaks.Visible = True Sleep 1000 DoEvents If Cancel = True Then Unload loading_masterflexfilter Exit Sub End If loading_masterflexfilter.loading_leaks.Visible = False loading_masterflexfilter.loading_beag.Visible = True Sleep 1000 DoEvents 

结束小组