ProgressBar Help

Phil1981

New Member
Joined
May 27, 2011
Messages
27
I have been messing with a userform progressbar for quiet some time now and checked all references and threads possible and can not seem to find a solution that will work for me.

My code takes approximately 3 minutes or so to run and wanted to setup a progressbar to indicate what is happening so users dont feel like it crashed or froze on them.

I have previously used the statusbar change option which many users neglected, so I was hoping for an alternative.

Below is the code running
Code:
Sub UpdateMAPs()
    Dim LR As Long
    Dim i As Long
    Dim r As Integer
    Dim sh As Worksheet
    Dim shCount As Integer
    Dim AltKey As String
    Dim TabKey As String
    Dim EnterKey As String
    '--------------------------
    
    AltKey = "%"
    TabKey = "{TAB}"
    EnterKey = "~"
    shCount = Application.Sheets.Count
    '--------------------------
    
    Application.DisplayAlerts = False
    Application.ScreenUpdating = False
    '--------------------------
    'Check for existing MAPs and delete them
    
    If shCount > 11 Then
        For Each sh In ThisWorkbook.Sheets '
            Select Case sh.Name
                Case "Team List", "Targets", "Call Data", "Quality", "SQM", "SmartLink", "ICV", "2nd Lines", "Credits per Call", "ARC", "Blank MAP"
            'Do nothing
    
            Case Else
                sh.Delete
            End Select
        Next sh
    Else
    End If
    '--------------------------
    
    Application.DisplayAlerts = True
    '--------------------------
    'Create MAPs from team list and add agent name, emp id, avaya and coaches name
    
    Sheets(1).Select
        Range("D2").Select
        Range(Selection, Selection.End(xlToRight)).Select
        Range(Selection, Selection.End(xlDown)).Select
        Selection.Sort Key1:=Range("D2"), Order1:=xlDescending, Header:=xlGuess, _
            OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom, _
            DataOption1:=xlSortNormal
    With Sheets(1)
        LR = .Range("D" & Rows.Count).End(xlUp).Row
        For i = 3 To LR
        
            Sheets(11).Activate
            Range("$A$1:$W$63").Select
            Selection.Copy
            Sheets(11).Select
            Sheets.Add
            ActiveSheet.Name = .Range("D" & i).Value
            ActiveSheet.Paste
                Selection.PasteSpecial Paste:=xlPasteColumnWidths, Operation:=xlNone, _
                SkipBlanks:=False, Transpose:=False
            
            ActiveSheet.Range("E5") = .Range("d" & i).Value
            ActiveSheet.Range("E5:H5").Select
            Selection.Merge
            Selection.HorizontalAlignment = xlCenter
        
            ActiveSheet.Range("E7") = .Range("e" & i).Value
            ActiveSheet.Range("E7:G7").Select
            Selection.Merge
            Selection.HorizontalAlignment = xlCenter
        
            ActiveSheet.Range("E9") = .Range("D1").Value
            ActiveSheet.Range("E9:G9").Select
            Selection.Merge
            Selection.HorizontalAlignment = xlCenter
        
            ActiveSheet.Range("M7") = .Range("f" & i).Value
            ActiveSheet.Range("M7:N7").Select
            Selection.Merge
            Selection.HorizontalAlignment = xlCenter
            
            Range("$IV$65536").Select
    '--------------------------
    
    'Set Print Area and Page Setup Options
    ActiveSheet.PageSetup.PrintArea = "$A$1:$W$63"
    Application.SendKeys AltKey & "FU"
    Application.Wait Now + TimeValue("00:00:02")
    Application.SendKeys AltKey & "T"
    Application.Wait Now + TimeValue("00:00:01")
    Application.SendKeys AltKey & "F"
    Application.Wait Now + TimeValue("00:00:01")
    Application.SendKeys "1" & TabKey
    Application.SendKeys "1"
    Application.Wait Now + TimeValue("00:00:01")
    Application.SendKeys EnterKey
    
        Application.SendKeys "(ENTER)", False
        Application.Dialogs(xlDialogPageSetup).Show _
            Arg1:="", _
            Arg2:="", _
            Arg3:=0.15, _
            Arg4:=0.15, _
            Arg5:=0.15, _
            Arg6:=0.15, _
            Arg9:=True, _
            Arg10:=True, _
            Arg18:=0.15, _
            Arg19:=0.15
            
    Range("$A$1").Select
        Next i
    End With
    '--------------------------
    
    'calculate and clear any unnecessary keystrokes caused by sendkeys
    Application.Calculation = xlCalculationAutomatic
    Application.ScreenUpdating = True
    Application.Calculation = xlCalculationManual
End Sub

And here is the code I am trying to manipulate to run through my loops to show progress while keeping the % running.

Code:
Sub ProgressBar()
    Dim frm As frmSplash
    Dim x As Integer
    Dim j As Long
 
    Set frm = New frmSplash

    frm.CloseMe = False
    frm.ProgressBar.Value = 0
    frm.Show False
 
    'value x will change depending on which loop it is placed in
    For x = 0 To 100 Step 5
        frm.ProgressBar.Value = x
        frm.lblPercent.Caption = x & " %"

    'My code would go here ... the below is simply wasting time for testing purposes.
        For j = 1 To 100000
            DoEvents
        Next j
        
    Next x
    frm.CloseMe = True
    Unload frm
End Sub

Any help or suggestions would be greatly appreciated as I have all but given up hope ... fortunately the application.wait now + timevalue lines must remain, even though they slow down the code quite a bit as the sendkeys pagesetup code will fail without any delays in place.
 

Excel Facts

Create a Pivot Table on a Map
If your data has zip codes, postal codes, or city names, select the data and use Insert, 3D Map. (Found to right of chart icons).
I'm not sure about the status bar as I have very little experience with forms, but you probably don't need it. There are a few main issues with your code - fix these and your macro probably should take a few seconds, not three minutes...

1-using 'select' and 'activate' is almost never necessary and significantly slows things down
2-looping thru i=3 to LR and setting the print area in the same way each time for the same sheet
3-using sendkeys to set the print settings??
4-Application.Calculation = xlCalculationManual at the bottom of the code; this should be at the top if you want to turn off calculation while running the code - you already switch it on close to the end of your code, which is what you normally should do.

1. example follows, many instances of unnecessary selecting...
Rich (BB code):
            Sheets(11).Activate
            Range("$A$1:$W$63").Select
            Selection.Copy
could be, which will run faster:
Rich (BB code):
            Sheets(11).Range("$A$1:$W$63").Copy

2. You are looping through a bunch of rows but only need to do this once per sheet. Instead of having "next i" where it is, put it right above this line, which will save (depending on how many rows there are) many, many seconds:
Rich (BB code):
'Set Print Area and Page Setup Options

3. This part of your code is just horifying... Record a macro where you set the print settings that you want, then take the code from that macro and use it to replace the red code below. Sendkeys is to be avoided wherever possible as it is unreliable - plus you intentionally wait for 5 seconds here, which is probably about how long the entire macro should take to run.
Rich (BB code):
    ActiveSheet.PageSetup.PrintArea = "$A$1:$W$63"
   Application.SendKeys AltKey & "FU"
   Application.Wait Now + TimeValue("00:00:02")
   Application.SendKeys AltKey & "T"
   Application.Wait Now + TimeValue("00:00:01")
   Application.SendKeys AltKey & "F"
   Application.Wait Now + TimeValue("00:00:01")
   Application.SendKeys "1" & TabKey
   Application.SendKeys "1"
   Application.Wait Now + TimeValue("00:00:01")
   Application.SendKeys EnterKey
 
       Application.SendKeys "(ENTER)", False
       Application.Dialogs(xlDialogPageSetup).Show _
           Arg1:="", _
           Arg2:="", _
           Arg3:=0.15, _
           Arg4:=0.15, _
           Arg5:=0.15, _
           Arg6:=0.15, _
           Arg9:=True, _
           Arg10:=True, _
           Arg18:=0.15, _
           Arg19:=0.15
 
   Range("$A$1").Select

4. If you are in fact wanting to temporarily disable calculation while the macro runs, move the last line of code up to right after your dim statements.

Hope that helps...
Tai
 
Upvote 0
This is what I do:

Dim k as integer
k=1

For i = 1 to 100

Application.StatusBar = "Progress Percentage " & Application.Rept(Chr(8), (k / 2))

<code>

next i

Application.StatusBar = ""

You may have to change the denominator (k/2) depending how big your loop is. Play around with it and you should get what you're looking for.
 
Upvote 0
taigovinda ... I am fairly new to coding excel sheets so I will take all your tips into account and try to speed up my code ... thank you. But I do not understand how to avoid sendkeys.

I have tried to record the macro for pagesetup and loop through while the new sheets are being created and it is horribly slow and does not tend to be reliable. The delay that I have set while running the sendkeys is to ensure that they are reliable because without the delays the code will run too fast for the sendkeys to work properly.

jgrillo04 .... thanks for the hint I will see what I can do with your code =)

I should have sometime today to attempt to mess around with my code and see what changes I can make to speed it up and to loop a progress bar into it as well .... thanks again guys!
 
Upvote 0
2. You are looping through a bunch of rows but only need to do this once per sheet. Instead of having "next i" where it is, put it right above this line, which will save (depending on how many rows there are) many, many seconds:
Rich (BB code):
'Set Print Area and Page Setup Options

3. This part of your code is just horifying... Record a macro where you set the print settings that you want, then take the code from that macro and use it to replace the red code below. Sendkeys is to be avoided wherever possible as it is unreliable - plus you intentionally wait for 5 seconds here, which is probably about how long the entire macro should take to run.
Rich (BB code):
    ActiveSheet.PageSetup.PrintArea = "$A$1:$W$63"
   Application.SendKeys AltKey & "FU"
  Application.Wait Now + TimeValue("00:00:02")
  Application.SendKeys AltKey & "T"
  Application.Wait Now + TimeValue("00:00:01")
  Application.SendKeys AltKey & "F"
  Application.Wait Now + TimeValue("00:00:01")
  Application.SendKeys "1" & TabKey
  Application.SendKeys "1"
  Application.Wait Now + TimeValue("00:00:01")
  Application.SendKeys EnterKey
 
      Application.SendKeys "(ENTER)", False
      Application.Dialogs(xlDialogPageSetup).Show _
          Arg1:="", _
          Arg2:="", _
          Arg3:=0.15, _
          Arg4:=0.15, _
          Arg5:=0.15, _
          Arg6:=0.15, _
          Arg9:=True, _
          Arg10:=True, _
          Arg18:=0.15, _
          Arg19:=0.15
 
  Range("$A$1").Select

4. If you are in fact wanting to temporarily disable calculation while the macro runs, move the last line of code up to right after your dim statements.

Hope that helps...
Tai[/QUOTE]


Tai I think maybe you do not fully understand what the purpose of my code is or maybe I am misunderstanding you.

Basically my workbook creates one worksheet for every name listed on a master list of employees and adds in their employee ID, hire date and the name of their supervisor.

The reason I have resorted to sendkeys and using the application.dialogs(x1DialogPageSetup) is because each sheet normally a minimum of 30 up to 100 requires page margins and fit to 1 page settings to print probably which can not be done for multiple pages and using any recorded macro to do this in a loop for each page is extremely slow.

My macro takes 2-3 minutes at most to run currently and if I move my next i above the print settings, I would then need another loop to set the print settings on each sheet. If I were to use the application.pagesetup or recorded function the macro will take a minimum of 15 minutes to run which is just insane.

I know there is probably a better way to add page setup options then using sendkeys but nothing I have tried is reliable or as near as quick as sendkeys.

If you have any advice on a better way to ensure that I can fit to 1 page for every sheet created I would be more then happy to try it =)
 
Upvote 0
My mistake Phil. I understand, you need a bunch of sheets with the print settings, not just one. I would suggest you make one "template sheet" that is pretty much blank but has the print settings and then, when you run your macro, make copies of that sheet instead of adding new sheets. This way you can avoid changing print settings at all within your macro.

As far as sendkeys to change print settings... I've always recorded my print settings changes and then retained only the portion(s) of recorded code that I needed (for instance, delete the lines of code that specify the left header and right header will be blank, retaining only the line that specifies the center header text), and never had any issues with reliability. I can't imagine anything where you purposely let seconds elapse would be faster.
 
Upvote 0

Forum statistics

Threads
1,224,586
Messages
6,179,728
Members
452,939
Latest member
WCrawford

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top