vba - Excel vba 进度条不工作 : invalid property value

标签 vba excel

我正在尝试使用 excel vba 教程中的以下代码,但失败了:ProgressBat 未更新,并且 UserForm1.ProgressBar1.Value = UserForm1.ProgressBar1.Value + 1 行突出显示错误“运行时错误 380。属性值无效”。

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

可能出了什么问题?

最佳答案

那是因为您超出了最大值。试试这个

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

顺便说一句,我猜你忘了在UserForm1.Show之后提及vbModeless

说明

当您设置进度条的最小值或最大值时,您无法为其分配不在该范围内的值。例如,如果最小值 = 1 且最大值 = 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's values
    'Unload UserForm1
End Sub

关于vba - Excel vba 进度条不工作 : invalid property value,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44781352/

相关文章:

vba - 删除列中所有有错误的行

vba - 如何在 VBA 中查找多个字符串

Excel - 复杂验证的策略

java - 比较不同文件(CSV,Excel)Java Apache commons 和 poi 中两列的日期以了解日期相似的地方

java - Apache POI autoSizeColumn() 无法正常工作

vba - 在 VBA 中对多个键进行排序;运行时错误 450 : Wrong number of arguments or invalid property assignment

vba - If-then/为单元格引用中定义的日期范围选择案例

xml - SharePoint 列表、GetListItems、XML 和 VBA - 我只想交叉引用!

c# - 如何使用 oledb c#.net 在 excel 中插入新行

excel - 为什么我在 Excel Vba 中的这个函数的 for 循环中得到了额外的选择?