|
1 | 1 |
|
2 | 2 | # 简明Excel VBA
|
3 |
| -Last update date:02/17/2022 17:55 |
| 3 | +Last update date:02/21/2022 15:52 |
4 | 4 |
|
5 | 5 | > `VBA` 缩写于 *Visual Basic for Applications*。
|
6 | 6 |
|
@@ -3397,7 +3397,7 @@ End Function
|
3397 | 3397 | <a name="95.02"></a>
|
3398 | 3398 | ### 95.02 VBA程序进度条(Process Bar)
|
3399 | 3399 |
|
3400 |
| -进图条式样如下图: |
| 3400 | +进图条式样1及其代码如下图: |
3401 | 3401 |
|
3402 | 3402 | 
|
3403 | 3403 |
|
@@ -3429,6 +3429,109 @@ End Sub
|
3429 | 3429 | ```
|
3430 | 3430 |
|
3431 | 3431 |
|
| 3432 | +进图条式样2及其代码如下图: |
| 3433 | + |
| 3434 | + |
| 3435 | + |
| 3436 | + |
| 3437 | + |
| 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 | +``` |
3432 | 3535 |
|
3433 | 3536 |
|
3434 | 3537 |
|
|
0 commit comments