Skip to content

Commit dc93317

Browse files
committed
Add process bar function by UserForm.
1 parent 711fa4e commit dc93317

File tree

3 files changed

+105
-2
lines changed

3 files changed

+105
-2
lines changed

README.md

Lines changed: 105 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11

22
# 简明Excel VBA
3-
Last update date:02/17/2022 17:55
3+
Last update date:02/21/2022 15:52
44

55
> `VBA` 缩写于 *Visual Basic for Applications*
66
@@ -3397,7 +3397,7 @@ End Function
33973397
<a name="95.02"></a>
33983398
### 95.02 VBA程序进度条(Process Bar)
33993399

3400-
进图条式样如下图
3400+
进图条式样1及其代码如下图
34013401

34023402
![Alt text](doc/source/images/95/process_bar.gif)
34033403

@@ -3429,6 +3429,109 @@ End Sub
34293429
```
34303430

34313431

3432+
进图条式样2及其代码如下图:
3433+
3434+
![Alt text](doc/source/images/95/process_bar2-1.png)
3435+
3436+
3437+
![Alt text](doc/source/images/95/process_bar2-2.gif)
3438+
3439+
3440+
代码:
3441+
3442+
```
3443+
============== This code goes in Module1 ============
3444+
3445+
Sub ShowProgress()
3446+
UserForm1.Show
3447+
End Sub
3448+
3449+
============== Module1 Code Block End =============
3450+
3451+
```
3452+
3453+
3454+
Create a Button on a Worksheet; map button to "ShowProgress" macro
3455+
3456+
Create a UserForm1 with 2 Command Buttons and 3 Labels so you get the following objects
3457+
3458+
3459+
| Element | Purpose | Properties to set |
3460+
|---------|---------|-------------------|
3461+
| `UserForm1` | canvas to hold other 5 elements | |
3462+
| `CommandButton1` | Close UserForm1 | Caption: "Close" |
3463+
| `CommandButton2` | Run Progress Bar Code | Caption: "Run" |
3464+
| `Bar1` (label) | Progress bar graphic | BackColor: Blue |
3465+
| `BarBox` (label) | Empty box to frame Progress Bar | BackColor: White |
3466+
| `Counter` (label) | Display the integers used to drive the progress bar | |
3467+
3468+
Then add this code to UserForm1:
3469+
3470+
```
3471+
======== Attach the following code to UserForm1 =========
3472+
3473+
Option Explicit
3474+
3475+
' This is used to create a delay to prevent memory overflow
3476+
' remove after software testing is complete
3477+
3478+
Private Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
3479+
3480+
Private Sub UserForm_Initialize()
3481+
Bar1.Tag = Bar1.Width ' Memorize initial/maximum width
3482+
Bar1.Width = 0
3483+
End Sub
3484+
3485+
Sub ProgressBarDemo()
3486+
Dim intIndex As Integer
3487+
Dim sngPercent As Single
3488+
Dim intMax As Integer
3489+
'==============================================
3490+
'====== Bar Length Calculation Start ==========
3491+
3492+
'-----------------------------------------------'
3493+
' This section is where you can use your own '
3494+
' variables to increase bar length. '
3495+
' Set intMax to your total number of passes '
3496+
' to match bar length to code progress. '
3497+
' This sample code automatically runs 1 to 100 '
3498+
'-----------------------------------------------'
3499+
intMax = 100
3500+
For intIndex = 1 To intMax
3501+
sngPercent = intIndex / intMax
3502+
Bar1.Width = Int(Bar1.Tag * sngPercent)
3503+
Counter.Caption = intIndex
3504+
3505+
3506+
'======= Bar Length Calculation End ===========
3507+
'==============================================
3508+
3509+
3510+
DoEvents
3511+
'------------------------
3512+
' Your production code would go here and cycle
3513+
' back to pass through the bar length calculation
3514+
' increasing the bar length on each pass.
3515+
'------------------------
3516+
3517+
'this is a delay to keep the loop from overrunning memory
3518+
'remove after testing is complete
3519+
Sleep 10
3520+
3521+
Next
3522+
3523+
End Sub
3524+
3525+
Private Sub CommandButton1_Click() 'CLOSE button
3526+
Unload Me
3527+
End Sub
3528+
3529+
Private Sub CommandButton2_Click() 'RUN button
3530+
ProgressBarDemo
3531+
End Sub
3532+
3533+
================= UserForm1 Code Block End =====================
3534+
```
34323535

34333536

34343537

14.8 KB
Loading
37.8 KB
Loading

0 commit comments

Comments
 (0)