+ Reply to Thread
Results 1 to 3 of 3

VBA Splash Screen As Macro Runs

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    05-26-2012
    Location
    United Kingdom
    MS-Off Ver
    Excel 2013
    Posts
    682

    VBA Splash Screen As Macro Runs

    Hi, I wonder whether someone may be able to help me please.

    I'm trying to put together a 'Splash Screen' which is visible to the user as a rather long macro runs.

    I've done quite a bit of research and found a suitable example here: http://www.vb-helper.com/howto_excel_show_splash.html.

    I've created a general module and inserted the following code:

    Sub ShowSplash()
    Dim frm As frmSplash
    Dim i As Integer
    Dim j As Integer
    
        ' Deactivate the keyboard.
        Application.OnKey "^d", "KeyboardOn"
        Application.DataEntryMode = True
    
        ' Display the splash form non-modally.
        Set frm = New frmSplash
        frm.TaskDone = False
        frm.prgStatus.Value = 0
        frm.Show False
    
        ' Perform the long task.
        For i = 0 To 100 Step 10
            frm.prgStatus.Value = i
    
            ' Waste some time.
            For j = 1 To 1000
                DoEvents
            Next j
        Next i
    
        ' Close the splash form.
        frm.TaskDone = True
        Unload frm
    
        ' Re-activate the keyboard.
        Application.DataEntryMode = False
    End Sub
    Then inserted the following code to the user form:

    ' Set true when the long task is done.
    Public TaskDone As Boolean
    
    Private Sub UserForm_QueryClose(Cancel As Integer, _
        CloseMode As Integer)
        Cancel = Not TaskDone
    End Sub
    I've run the user form in isolation and this works as expected, but I'm having a little difficulty in getting this to run whilst my macro runs.

    The code below is the macro which I'm wanting to run whilst the form is visible, then once finished allow the form to fade out:

    Sub CreateAllData()
        
        Dim cell As Range
        Dim cll As Range
        Dim DestWB As Workbook
        Dim dR As Long
        Dim excelfile As Variant
        Dim Fd As FileDialog
        Dim i As Long
        Dim LastRow As Long
        Dim LR As Long
        Dim MidFile As String
        Dim MyNames As Variant
        Dim sFile As String
        Dim sMidFile As Variant
        Dim SourceSheet As String
        Dim StartRow As Long
        Dim wb As Workbook
        Dim ws As Worksheet
    
        Set DestWB = ActiveWorkbook
    
        SourceSheet = "Input"
        StartRow = 2
    
        sMidFile = "January, February, March, April, May, June, July, August, September, October, November, December"
    
        MidFile = InputBox("Enter Folder Name e.g. 'January'", "Managers Data")
        If InStr(sMidFile, MidFile) = 0 Or MidFile = "" Then
            MsgBox "A valid month name was not entered"
            End
        End If
    
        Application.ScreenUpdating = False
    
        Set Ash = ActiveSheet
        Set newsht = Worksheets.Add(After:=Worksheets(1))
        newsht.Name = "All Data"
    
        With newsht
            With .Range("B5")
                .Value = "All Data"
                .Offset(2, 0).Resize(, 5).Value = Array("Resource LOB", "Staff Name", "Job Role", "Project Name", "Project ID")
            End With
            .Range("G7").Formula = "=B3"
            .Range("H7").Resize(, 13).Formula = "=EOMONTH(G7,0)+1"
            With Range("U7")
                .Value = "Flexible Resource"
                .Offset(, 1).Value = "Line Manager"
                .Offset(, 2).Value = "Date of Termination"
            End With
        End With
    
        Range("B7:W7").Select
        Selection.AutoFilter
    
        sFile = "\\Irf02200\ims r and d management\D&RM\Reporting\Clarity Extracts\" & MidFile & "\HUB\Resource Activity By Month\"
        excelfile = Dir(sFile & "*.xls")
        Do While excelfile <> ""
    
            Set wb = Workbooks.Open(Filename:=sFile & excelfile, ReadOnly:=True, Password:="master")
            For Each ws In wb.Worksheets
                If ws.Name = SourceSheet Then
                    With ws
                        If .UsedRange.Cells.Count > 1 Then
                            dR = DestWB.Worksheets("All Data").Range("B" & DestWB.Worksheets("All Data").Rows.Count).End(xlUp).Row + 1
                            If dR < 8 Then dR = 7  'destination start row
                            LastRow = .Range("A" & Rows.Count).End(xlUp).Row
                            If LastRow >= StartRow Then
                                .Range("A" & StartRow & ":R" & LastRow).Copy
                                DestWB.Worksheets("All Data").Cells(dR, "C").PasteSpecial xlValues
                                DestWB.Worksheets("All Data").Range("B8:W" & LastRow).Font.Name = "Lucida Sans"
                                DestWB.Worksheets("All Data").Range("B8:W" & LastRow).Font.Size = 10
                            End If
                        End If
                    End With
                    Exit For
                End If
                Next ws
                wb.Close savechanges:=False
                excelfile = Dir
            Loop
    
            Set Ash = ActiveSheet
            Set newsht = Worksheets.Add(After:=Worksheets(2))
            newsht.Name = "Resources List"
    
            With newsht
                With .Range("B5")
                    .Value = "Resources List"
                    .Offset(2, 0).Resize(, 6).Value = Array("Resource LOB", "Staff Name", "Job Role", "Flexible Resource", "Line Manager", "Date of Termination")
                End With
            End With
    
            Range("B7:G7").Select
            Selection.AutoFilter
    
            sFile = "\\Irf02200\ims r and d management\D&RM\Reporting\Clarity Extracts\" & MidFile & "\HUB\Resources List\"
            excelfile = Dir(sFile & "*.xls")
            Do While excelfile <> ""
    
                Set wb = Workbooks.Open(Filename:=sFile & excelfile, ReadOnly:=True, Password:="master")
                For Each ws In wb.Worksheets
                    If ws.Name = SourceSheet Then
                        With ws
                            If .UsedRange.Cells.Count > 1 Then
                                dR = DestWB.Worksheets("Resources List").Range("B" & DestWB.Worksheets("Resources List").Rows.Count).End(xlUp).Row + 1
                                If dR < 8 Then dR = 7  'destination start row
                                LastRow = .Range("A" & Rows.Count).End(xlUp).Row
                                If LastRow >= StartRow Then
                                    .Range("A" & StartRow & ":F" & LastRow).Copy
                                    DestWB.Worksheets("Resources List").Cells(dR, "B").PasteSpecial xlValues
                                    DestWB.Worksheets("Resources List").Range("B8:G" & LastRow).Font.Name = "Lucida Sans"
                                    DestWB.Worksheets("Resources List").Range("B8:G" & LastRow).Font.Size = 10
                                End If
                            End If
                        End With
                        Exit For
                    End If
                    Next ws
                    wb.Close savechanges:=False
                    excelfile = Dir
                Loop
    
                Call AdditionalData
                Call ResourcesListFormat
                Call AllDataFormat
                Call CreateUniques
                Call UniqueRecordsExtract
                Call UniqueRecordsFormat
                Call CreateSheets
                Call ApplySubtotalsandDelete
    
                Sheets("Macros").Select
                Application.ScreenUpdating = True
            End Sub
    I've been trying to get this to work for a few days now, sadly without any success, so I just wondered whether someone could possibly look at this please and offer some guidance on how I may integrate the two.

    Many thanks and kind regards

  2. #2
    Forum Guru Izandol's Avatar
    Join Date
    03-29-2012
    Location
    *
    MS-Off Ver
    Excel 20(03|10|13)
    Posts
    2,581

    Re: VBA Splash Screen As Macro Runs

    If you wish to show progress meter, you must have a way of determining percentage completion for main routine. I do not see accurate possibility for the code you have now but you may apply same percentage to each task. Code will be like:
    Userform:
    Option Explicit
    
    ' Set true when the long task is done.
    Public TaskDone As Boolean
    Private m_dProgress As Double
    
    Private Sub UserForm_QueryClose(Cancel As Integer, _
        CloseMode As Integer)
        Cancel = Not TaskDone
    End Sub
    
    Public Property Let Progress(ByVal dProgress As Double)
    
       m_dProgress = dProgress
       Me.prgStatus.Value = m_dProgress
    End Property
    Module:
    Sub CreateAllData()
    
       Dim cell                        As Range
       Dim cll                         As Range
       Dim DestWB                      As Workbook
       Dim dR                          As Long
       Dim excelfile                   As Variant
       Dim Fd                          As FileDialog
       Dim i                           As Long
       Dim LastRow                     As Long
       Dim LR                          As Long
       Dim MidFile                     As String
       Dim MyNames                     As Variant
       Dim sFile                       As String
       Dim sMidFile                    As Variant
       Dim SourceSheet                 As String
       Dim StartRow                    As Long
       Dim wb                          As Workbook
       Dim ws                          As Worksheet
       Dim frm                         As frmSplash
       Dim x                           As Integer
       Dim y                           As Integer
    
    
    
       Set DestWB = ActiveWorkbook
    
       SourceSheet = "Input"
       StartRow = 2
    
       sMidFile = "January, February, March, April, May, June, July, August, September, October, November, December"
    
       MidFile = InputBox("Enter Folder Name e.g. 'January'", "Managers Data")
       If InStr(sMidFile, MidFile) = 0 Or MidFile = "" Then
          MsgBox "A valid month name was not entered"
          Exit Sub
       End If
    
       ' Display the splash form non-modally.
       Set frm = New frmSplash
       frm.TaskDone = False
       frm.Progress = 0
       frm.Show False
    
       Application.ScreenUpdating = False
    
       Set Ash = ActiveSheet
       Set newsht = Worksheets.Add(After:=Worksheets(1))
       newsht.Name = "All Data"
    
       With newsht
          With .Range("B5")
             .Value = "All Data"
             .Offset(2, 0).Resize(, 5).Value = Array("Resource LOB", "Staff Name", "Job Role", "Project Name", "Project ID")
          End With
          .Range("G7").Formula = "=B3"
          .Range("H7").Resize(, 13).Formula = "=EOMONTH(G7,0)+1"
          With Range("U7")
             .Value = "Flexible Resource"
             .Offset(, 1).Value = "Line Manager"
             .Offset(, 2).Value = "Date of Termination"
          End With
       End With
    
       ' update progress
       frm.Progress = 3
    
       Range("B7:W7").Select
       Selection.AutoFilter
    
       sFile = "\\Irf02200\ims r and d management\D&RM\Reporting\Clarity Extracts\" & MidFile & "\HUB\Resource Activity By Month\"
       excelfile = Dir(sFile & "*.xls")
       Do While excelfile <> ""
    
          Set wb = Workbooks.Open(Filename:=sFile & excelfile, ReadOnly:=True, Password:="master")
          For Each ws In wb.Worksheets
             If ws.Name = SourceSheet Then
                With ws
                   If .UsedRange.Cells.Count > 1 Then
                      dR = DestWB.Worksheets("All Data").Range("B" & DestWB.Worksheets("All Data").Rows.Count).End(xlUp).Row + 1
                      If dR < 8 Then dR = 7  'destination start row
                      LastRow = .Range("A" & Rows.Count).End(xlUp).Row
                      If LastRow >= StartRow Then
                         .Range("A" & StartRow & ":R" & LastRow).Copy
                         DestWB.Worksheets("All Data").Cells(dR, "C").PasteSpecial xlValues
                         DestWB.Worksheets("All Data").Range("B8:W" & LastRow).Font.Name = "Lucida Sans"
                         DestWB.Worksheets("All Data").Range("B8:W" & LastRow).Font.Size = 10
                      End If
                   End If
                End With
                Exit For
             End If
          Next ws
          wb.Close savechanges:=False
          excelfile = Dir
       Loop
    
       ' update progress
       frm.Progress = 10
    
    
       Set Ash = ActiveSheet
       Set newsht = Worksheets.Add(After:=Worksheets(2))
       newsht.Name = "Resources List"
    
       With newsht
          With .Range("B5")
             .Value = "Resources List"
             .Offset(2, 0).Resize(, 6).Value = Array("Resource LOB", "Staff Name", "Job Role", "Flexible Resource", "Line Manager", "Date of Termination")
          End With
       End With
    
       Range("B7:G7").Select
       Selection.AutoFilter
    
       sFile = "\\Irf02200\ims r and d management\D&RM\Reporting\Clarity Extracts\" & MidFile & "\HUB\Resources List\"
       excelfile = Dir(sFile & "*.xls")
       Do While excelfile <> ""
    
          Set wb = Workbooks.Open(Filename:=sFile & excelfile, ReadOnly:=True, Password:="master")
          For Each ws In wb.Worksheets
             If ws.Name = SourceSheet Then
                With ws
                   If .UsedRange.Cells.Count > 1 Then
                      dR = DestWB.Worksheets("Resources List").Range("B" & DestWB.Worksheets("Resources List").Rows.Count).End(xlUp).Row + 1
                      If dR < 8 Then dR = 7  'destination start row
                      LastRow = .Range("A" & Rows.Count).End(xlUp).Row
                      If LastRow >= StartRow Then
                         .Range("A" & StartRow & ":F" & LastRow).Copy
                         DestWB.Worksheets("Resources List").Cells(dR, "B").PasteSpecial xlValues
                         DestWB.Worksheets("Resources List").Range("B8:G" & LastRow).Font.Name = "Lucida Sans"
                         DestWB.Worksheets("Resources List").Range("B8:G" & LastRow).Font.Size = 10
                      End If
                   End If
                End With
                Exit For
             End If
          Next ws
          wb.Close savechanges:=False
          excelfile = Dir
       Loop
       ' update progress
       frm.Progress = 20
    
       Call AdditionalData
    
       ' update progress
       frm.Progress = 30
    
       Call ResourcesListFormat
       ' update progress
       frm.Progress = 40
       Call AllDataFormat
       ' update progress
       frm.Progress = 50
       Call CreateUniques
       ' update progress
       frm.Progress = 60
       Call UniqueRecordsExtract
       ' update progress
       frm.Progress = 70
       Call UniqueRecordsFormat
       ' update progress
       frm.Progress = 80
       Call CreateSheets
       ' update progress
       frm.Progress = 90
       Call ApplySubtotalsandDelete
       ' update progress
       frm.Progress = 100
       ' Close the splash form.
       frm.TaskDone = True
       Unload frm
    
       Sheets("Macros").Select
       Application.ScreenUpdating = True
    End Sub
    I have not tested.
    • Please remember to mark threads Solved with Thread Tools link at top of page.
    • Please use code tags when posting code: [code]Place your code here[/code]
    • Please read Forum Rules

  3. #3
    Forum Contributor
    Join Date
    05-26-2012
    Location
    United Kingdom
    MS-Off Ver
    Excel 2013
    Posts
    682

    Re: VBA Splash Screen As Macro Runs

    Hi @Izandol, thank you very much for taking the time to reply to my post and for putting the solution together, I really appreciate it.

    The form works fine as does the progress bar. The only item which I can't display is the label which tells the user what is happening which seems a little strange, but I'm sure I'll be able to work it out.

    All the best and kind regards

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Replies: 4
    Last Post: 01-23-2014, 07:05 AM
  2. Splash screen for macro security settings
    By mcinnes01 in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 11-11-2010, 07:09 AM
  3. Splash Screen While Macro Runs?
    By Tyler_Durden in forum Excel Programming / VBA / Macros
    Replies: 9
    Last Post: 09-28-2009, 09:39 AM
  4. Turning off screen update while macro runs
    By dancingflames in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 11-04-2005, 06:50 PM
  5. splash screen macro problem
    By PKyle in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 02-09-2005, 12:06 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1