Excel vba Progress screen not working: invalid property value

I am trying to execute the following code from excel vba tutorial, but it fails: ProgressBat is not updated, plus the line UserForm1.ProgressBar1.Value = UserForm1.ProgressBar1.Value + 1gets highlited with the error `Run-time error 380. Invalid property value".

Sub ShowProgressBar()
    Dim lAllCnt As Long
    Dim rc As Range

    lAllCnt = Selection.Count

    UserForm1.Show
    UserForm1.ProgressBar1.Min = 1
    UserForm1.ProgressBar1.Max = lAllCnt

    For Each rc In Selection

        UserForm1.ProgressBar1.Value = UserForm1.ProgressBar1.Value + 1

    Next

    Unload UserForm1
End Sub

What could be wrong?

+4
source share
3 answers

This is because you have exceeded the maximum value. try it

For Each rc In Selection
    If UserForm1.ProgressBar1.Value < UserForm1.ProgressBar1.Max Then
        UserForm1.ProgressBar1.Value = UserForm1.ProgressBar1.Value + 1
    End If
Next

By the way, I forgot to mention vbModelessafterUserForm1.Show

Explanation

, , . , value = 1 value = 5, , , 1 5, .

Sub ShowProgressBar()
    Dim lAllCnt As Long
    Dim rc As Range

    lAllCnt = Selection.Count

    With UserForm1
        .Show vbModeless

        With .ProgressBar1
            .Min = 1
            .Max = lAllCnt

            For Each rc In Selection
                If .Value < .Max Then
                    .Value = .Value + 1
                End If
            Next
        End With
    End With

    '~~> I have uncommented it so that you can see the
    '~~> Userform with the progress bar with it values
    'Unload UserForm1
End Sub
+3

, min max. . , , , .

Option Explicit

Sub ShowProgressBar()

    Dim lAllCnt         As Long
    Dim rc              As Range

    lAllCnt = Selection.Count

    UserForm1.Show vbModeless
    UserForm1.ProgressBar1.Min = 1
    UserForm1.ProgressBar1.Max = lAllCnt

    For Each rc In Selection
        UserForm1.ProgressBar1.Value = fnBigOrSmallIncrement(UserForm1.ProgressBar1.Value, 1, lAllCnt)
        Application.Wait Now + #12:00:01 AM#
    Next

    Unload UserForm1

End Sub

Public Function fnBigOrSmallIncrement(lngCurrent As Long, lngMin As Long, lngMax As Long) As Long

    fnBigOrSmallIncrement = lngCurrent + 1

    If fnBigOrSmallIncrement < lngMin Then fnBigOrSmallIncrement = lngMin
    If fnBigOrSmallIncrement > lngMax Then fnBigOrSmallIncrement = lngMax

End Function

, - , .

+1

I'm not sure what your progress form looks like and what type of code you are using (maybe insert a link?), But when I used the progress bar, I changed the width property of the user form panel instead of its value, i.e., like http: / /www.excel-easy.com/vba/examples/progress-indicator.html .

-1
source

Source: https://habr.com/ru/post/1680211/


All Articles