1

I've searched a lot of pages and don't have a clear answer to this.

I have VBA code which does a lot of processing of Word revisions and I'd like to display a userform to inform the user of what's happening.

I track two variables (TotalRevisionCount and RevisionIndex) which count the total number of revisions being processed, and the individual revision number respectively. I've declared these as public variables in my main module.

I'd like my userform to display "Processing revision 2 of 36". I think the code to do this is really simple but I just can't join the dots.

Help?

***UPDATE This is a cut down version of the calling code:

Sub TestSub()
updateTotal = 10000

PlsWaitForm.Show (False)

For i = 1 To 10000
    UpdateNum = i
    PlsWaitForm.Repaint
Next i
Unload PlsWaitForm

End Sub

...and this is what I have in my userform:

Sub DisplayStuff()
    PlsWaitText.Caption = "Currently processing " & UpdateNum & " of " UpdateTotal & " records."
End Sub
6
  • 1
    Since you have declared TotalRevisionCount and RevisionIndex as public variables in your module, you should be able to use it from your Userform directly. If there are public variables sharing the same name across multiple modules (which is a bad practice) or you have a module-level (or even sub-level) variable of the same name (also a bad practice) in your Userform, then you can be specific by calling your module name followed by the variable e.g. Module1.TotalRevisionCount Commented Oct 1, 2021 at 14:50
  • So what does the userform code look like? I tried just displaying a messagebox with the two parameters, and then repainting the form from the main module, but that did nothing. Commented Oct 1, 2021 at 15:11
  • Are you calling the code from a Module or in the Userform? and how do you want to display the message? Like in a Label control? the Userform's Caption? You need to give more specific details to help you properly or else all you can get from us is generic answer. @StuartL Commented Oct 1, 2021 at 15:17
  • If all you want to display is simply a message to show the user its progress, you can consider using Application.Statusbar word.tips.net/… @StuartL Commented Oct 1, 2021 at 15:21
  • Please edit your question and put your code there. As you can see, it's usually difficult to read codes off comment. Commented Oct 1, 2021 at 15:28

3 Answers 3

1

Your code is not calling DisplayStuff in the loop, if you step through your code you would notice that the code did not reach DisplayStuff at all and thus, unable to update the Caption.

Code in Module1:

Public UpdateTotal As Long
Public UpdateNum As Long

Sub TestSub()
    UpdateTotal = 10000
    
    PlsWaitForm.Show (False)
    
    For i = 1 To 10000
        UpdateNum = i
        PlsWaitForm.DisplayStuff
    Next i
    
    Unload PlsWaitForm

End Sub

Code in PlsWaitForm:

Public Sub DisplayStuff()
    Me.Caption = "Currently processing " & UpdateNum & " of " & UpdateTotal & " records."
End Sub

Note: To reiterate what I said in my comment, if all you want to display is this message to your user of its progress, you can consider using Application.StatusBar instead.

Sign up to request clarification or add additional context in comments.

3 Comments

Thankyou for that. Yes, I considered Application.StatusBar but I wanted something a bit more in the user's face. This is very nearly what I want, but I have a label on my form that I want to hold the updated text. I thought I could just use PlsWaitForm.PlsWaitText.Caption = but that just results in a grey box. How do I update the label caption, rather than the form caption?
Try replacing Me.Caption to PlsWaitText.Caption. @StuartL
@StuartL Good to know, please accept the answer since it resolves your problem, thanks
0

On the form, implement a function to update the displayed information on the form, like this:

Public Function UpdateDisplayInfo()

   labCurrentSelectedCell.Caption = Sheet1.selectedCellAddress 

   If Not Me.Visible Then
      Me.Show False
   End If

   DoEvents

End Function

In code doing the processing, insert a call to UserFormName.UpdateDisplayInfo() at a convenient point in the processing loop, like this:

    Public selectedCellAddress as string

    Private Sub Worksheet_SelectionChange(ByVal Target As Range)

        Me.selectedCellAddress = Target.Address

        UserForm1.UpdateDisplayInfo

    End Sub
    

Comments

0

The missing piece was the RePaint statement. So the final module code looks like this:

Sub TestSub()
updateTotal = 100000

PlsWaitForm.Show (False)

For i = 1 To 1000000
    UpdateNum = i
    PlsWaitForm.DisplayStuff
Next i
Unload PlsWaitForm

End Sub

...and the final form code looks like this:

Public Sub DisplayStuff()

    PlsWaitForm.PlsWaitText.Caption = "Currently processing " & UpdateNum & " of " & updateTotal & " records."
    PlsWaitForm.Repaint
    
End Sub

Thanks for all your help and patience, everyone.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.