Activating a Macro from a Userform Automatically

michaeldh

Board Regular
Joined
Jun 11, 2002
Messages
201
Mr Excel,

I would like to have a userform appear and then subsequently perform an operation automatically ie without clicking a command button on the userform. Is this the Userform_Initialise command??

I want to click a button on Sheet1, have the userform appear and then carry out an operation.

I am using a basic example to better understand what I am doing so that I can apply it to a bigger project.

The Additional control I am using is Microsoft Progress Bar 6.0 (SP4)


Example:

Click a button within Sheet1 and activate userform1. Using a loop macro, repeat 1000 times placing the value of each increment in cell A1 of sheet1. Whilst everything is happenning make a Progress Bar increment from 1 to 1000 at the same time.

My code is as follows:

Private Sub CommandButton1_Click() 'Don't want to use a button

ProgressBar1.Min = 0
ProgressBar1.Max = 1000

For Count = 1 To 1000
ProgressBar1.Value = Count
'my code here
Sheets("Sheet1").Select
Range("A1").Select
ActiveCell = Count + 1
Next

Unload Me

End Sub

If you could help me out with some pointers I will definately owe you one!!

Thanks.

Michael.
This message was edited by michaeldh on 2002-08-18 03:03
 

Excel Facts

Copy a format multiple times
Select a formatted range. Double-click the Format Painter (left side of Home tab). You can paste formatting multiple times. Esc to stop
<style>td{ font-size: 10pt}p{ font-size: 10pt}pre{font-size:10pt;color:blue;font-family:Arial}</style>
There are many ways to create a progress bar.

1) The simplest way is to use the Application.Statusbar to provide a progress report. Two examples of this follow.
<pre>Sub useStatusbar1()
Dim i As Long
For i = 1 To 10
Application.StatusBar = "Now on " & i & " out of 10"
'do stuff
Application.Wait (Now() + TimeSerial(0, 0, 1))
Next i
Application.StatusBar = False
End Sub

Sub useStatusbar2()
Dim i As Long
For i = 1 To 10
'do stuff
Application.Wait (Now() + TimeSerial(0, 0, 1))
Application.StatusBar = String(i, Chr(1)) _
& " (" & CInt(i / 10 * 100) & "%)"
Next i
Application.StatusBar = False
End Sub</pre>

2) If you must use a graphical display, there are three choices. The first two use the ProgressBar control.

2.1) The first is to have the user click something on the chart that initiates the process. For an example of that see the ProgressBar help in CMCTL198.chm.

2.2) The only way to create a progress bar programmatically and update it without user intervention is to create an asynchronous task. There are two ways to do that. Note that while the examples below may look simple, you are initiating parallel asynchronous tasks. This introduces a degree of complexity that may be masked by VBA but which can come back to haunt the inexperienced programmer.

2.2.1) Use a modeless userform. First, this requires XL2000 or newer. Second, creating a modeless userform initiates a concurrent asynchronous task. It becomes your responsibility to manage both your primary code 'stream,' so to say, and this new asynchronous stream that is displaying the progress bar.
<pre>Sub testProgBarModeless()
Dim i As Integer
With UserForm1
.Show vbModeless
.ProgressBar1.Value = 0
For i = 1 To 10
'do stuff
Application.Wait (Now() + TimeSerial(0, 0, 1))
.ProgressBar1.Value = CInt(i / 10 * 100)
Next i
.Hide
End With
End Sub</pre>

2.2.2) Use the OnTime method to intiate a asynchronous task. Something along the lines of
<pre>Sub testProgBarModal()
Application.OnTime Now() + TimeSerial(0, 0, 3), "updateProgBarModal"
With UserForm1
.ProgressBar1.Value = 0
.Show
End With
End Sub
Sub updateProgBarModal()
'This is called for async processing by testProgBarModal
Dim i As Integer
With UserForm1
For i = 1 To 10
'do stuff
Application.Wait (Now() + TimeSerial(0, 0, 1))
.ProgressBar1.Value = CInt(i / 10 * 100)
Next i
.Hide
End With
End Sub</pre>

2.3) Create your own pictorial display and don't use the ProgressBar control. There is no guarantee that the control will exist on every machine on which the code will run. For an example of this you will have to wait for me to create a tutorial on my web site that documents all of the above and this additional technique.
 
Upvote 0
Thanks to everyone for your input. I'm sure to have it solved with all the options you have provided me.
 
Upvote 0

Forum statistics

Threads
1,215,851
Messages
6,127,288
Members
449,373
Latest member
jesus_eca

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