我正在尝试使用 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/