加载屏幕userform

时间:2015-10-13 13:42:08

标签: excel vba excel-vba

我已经尝试了几乎所有无模式选项都可以让它工作我只是无法理解它。我所拥有的是具有脚本的Userform(Main_Window),并且在命令按钮上执行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

此表格完美无缺。我有另一个名为mod_loading的Userform,看起来像这样

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

这两种形式都按原样工作,我遇到的问题是将mod_loading.Show放在Master_Flex_Filter_Generate()中的任何位置。无论我为无模式设置什么,mod_loading将一直运行直到它完全结束。它不会让Main_Window中的代码继续运行,因此它会停止该代码。有任何想法吗?我在想它也许是因为我没有在循环中播放mod_loading.Show,但由于我的连接字符串没有循环,我无法弄清楚如何去做。

我将代码置于If Not rs.EOF THEN中,但99%的代码加载到循环外的rs.Open上。

修改

好的,所以我已经改变了我的脚本,并将所有SQL逻辑放在userform中,用于加载对话框。所以我有Main_Window这是表单,然后它从加载userform调用sub弹出就好了。所以我现在弹出加载对话框并且按预期的那样消失。我唯一的问题是现在,它不会循环我的睡眠命令。它只是在第一个。

这是我加载userform的代码

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

End Sub

0 个答案:

没有答案