Update label caption in for next-loop

Stevenn

Active Member
Joined
Feb 8, 2012
Messages
259
I have a UserForm with a label and a for next-loop and a command button. How can I update the label caption in my for next-loop? When I try updating with lblPercentage = i & "%" it does not update until the for next-loop is ended.
 
You would need to load your form modeless in order to see the updating of your label. The syntax is <FORM>fmYourFormName.Show vbModeless.

Please note that processing continues even though the form is still displayed.</FORM>
 
Last edited:
Upvote 0
It seems like it updates when I use following code even though I open it directly from the UserForm

Code:
Option Explicit


Private Sub CommandButton1_Click()
    Dim i As Integer
    
    For i = 0 To 10
        Me.lblPercentage.Caption = i
        
        Application.Wait Now + TimeValue("00:00:01")
    Next i
End Sub

How is that possible when I don't open it modelessly?
 
Upvote 0
I misunderstood what you doing. What was your code doing when you did not see an update of your caption? Did you try inserting "DoEvents" into your original code?
 
Upvote 0
I misunderstood what you doing. What was your code doing when you did not see an update of your caption? Did you try inserting "DoEvents" into your original code?

I can't understand why it doesn't update. My code looks like

Code:
Private Sub CommandButton1_Click()
    Dim i As Integer, j As Integer
    
    For i = 1 To 10000
        Cells(i, 1).Value = "a"
        Cells(i, 2).Value = "b"
        Cells(i, 3).Value = "c"
        Cells(i, 4).Value = "d"
        
        For j = 0 To 30
            Me.lblPercentage.Caption = i
        Next j
    Next i
End Sub
 
Upvote 0
Can I somehow have disabled DoEvents? Even in my code

Code:
For Each objControl In .Controls
    objControl.Enabled = False
Next objControl

doesn't work. But when I add DoEvents inside the for each next-loop it works.

Code:
For Each objControl In .Controls
    objControl.Enabled = False
    
    DoEvents
Next objControl
 
Upvote 0
When I inserted DoEvents into your code it worked fine for me.
Code:
Private Sub CommandButton1_Click()
    Dim i As Integer, j As Integer
    
    For i = 1 To 10000
        Cells(i, 1).Value = "a"
        Cells(i, 2).Value = "b"
        Cells(i, 3).Value = "c"
        Cells(i, 4).Value = "d"
        
        For j = 0 To 30
            Me.lblPercentage.Caption = i
            DoEvents
        Next j
    Next i
End Sub
 
Upvote 0
But why is it suddently necessary to use DoEvents? I think some of my projects have worked just fine without using DoEvents, but suddently doesn't work anymore without? Am I wrong or is there a explanation? :-D
 
Upvote 0

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top