我正在尝试使用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
顺便说一句,我想你忘了在
vbModeless
之后提及UserForm1.Show
说明
当您设置进度条的最小值或最大值时,则不能为其分配不属于该范围的值。例如,如果最小值= 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