Running code from userform

O

ordnance1

Is it possible to run the code assigned to commandbutton1 on userform1
without opening the userform?
 
C

Chip Pearson

You need to load the form into memory, but not show it.

Sub AAA()
Load UserForm1
UserForm1.CommandButton1_Click
End Sub

In the userform's code module, change the command button's Click
procedure from Private to Public.

Cordially,
Chip Pearson
Microsoft Most Valuable Professional,
Excel, 1998 - 2010
Pearson Software Consulting, LLC
www.cpearson.com
 
D

Dave Peterson

What do you mean by open?

If you mean load it into memory, then nope. You have to do that.

If you mean display it to the user, then you can run the code without doing that.

I create a small userform with a single commandbutton on it.

This was the code that was behind that userform:

Option Explicit
Private Sub CommandButton1_Click()
MsgBox "hi"
End Sub


Then I created a subroutine in a general module that looked like:

Option Explicit
Sub testme()
Load UserForm1
UserForm1.CommandButton1.Value = True
Unload UserForm1
End Sub
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top