2016-10-07 4 views
3

진행률 막대 사용 방법에 대해 stackoverflow 통해 읽었습니다.진행 표시기, vba에서 응답하지 않게 됨

괜찮 았지만, 6 %의 짧은 시간이 지나면 235 행의 14 번째 행에 응답이 없다고 표시되고 루프가 완료 될 때까지 행해집니다.

그래서 실행하고 그것은 VBModeless 여기서 :

ProgressBar.Show vbModeless 

ProgressBar.Label_WhatsGoingOn.Caption = "Reading data from database.." 

projectNumber = "32966" 
docOutArray = Post.helpRequest("xdnjgjrdin.asp?Dok4=" & projectNumber) 

If CPearson.IsArrayEmpty(docOutArray) Then 
    MsgBox "No document registered in database!" 
End If 

ProgressBar.Label_WhatsGoingOn.Caption = "Creating Docout.." 

Set doc_ = NEwDocOut.createDocOutDocument(projectNumber) 

numOfRows = UBound(docOutArray, 1) 


For i = LBound(docOutArray, 1) To numOfRows 
    ProgressBar.Label_WhatsGoingOn.Caption = "Creating Row.." 

    sPercentage = (i/numOfRows) * 100 
    ProgressBar.progress (sPercentage) 

내 진행 막대 코드 :

Private Sub UserForm_Initialize() 
Me.Text.Caption = "0% Completed" 
End Sub 

Public Sub progress(pctCompl As Single) 

Me.Text.Caption = Format(pctCompl, "##") & "% Completed" 
Me.Bar.Width = pctCompl * 2 
Me.Repaint 
DoEvents 

End Sub 

이런 일이 발생하는 이유 어떤 아이디어?

enter image description here

답변

2

이 잠시 동안 나를 성가신하고 당신은 답변 - 감사 찾기 위해 저 자극했습니다.

당신의 경우, 루프 내부 DoEvents을 실제로 상당히 간단한 드롭 것 :

여기
For i = LBound(docOutArray, 1) To numOfRows 
    'Add this line here: 
    DoEvents 

    ProgressBar.Label_WhatsGoingOn.Caption = "Creating Row.." 

    sPercentage = (i/numOfRows) * 100 
    ProgressBar.progress (sPercentage) 
+0

나는 "DoEvents는"모든 이벤트가 아닌 간주 생각 진행률 표시 줄을 업데이트하는 이벤트 만 일반적으로 이것은 문제가되지 않지만 사람들이 클릭 할 수있는 다른 단추/양식이있는 경우 해당 클릭이 이미 실행중인 코드를 방해하지 않도록 예방 조치를 취해야합니다. – Shimeon

+0

@skatun이 답변을 찾았습니까? – User632716

+0

예, @tompreston 일종의, 문제는 지금은 몇 번 progressbar가 백그라운드에서 도착하고 때로는 progressbar가 이미 존재하거나 sth이기 때문에 progressbar가 표시 될 수 없다는 코드가 실패합니다. 나는 지금 이것을보고있다. – skatun

2

내 업데이트 된 코드의 제안입니다 :

Public Sub progress(pctCompl As Single) 
    Me.Text.Caption = Format(pctCompl, "##") & "% Completed" 
    Me.Bar.Width = pctCompl * 2 
    Me.Repaint 
End Sub 

Public Sub setMessage(message As String, Optional percentage As Single = 0#) 
    If Not ProgressBar.Visible Then ProgressBar.Show vbModeless 
    ProgressBar.Label_WhatsGoingOn.Caption = message 
    If percentage <> 0 Then Call ProgressBar.progress(percentage) 
End Sub