VBA UserForm question - loading form

strattergize

New Member
Joined
Nov 24, 2007
Messages
12
I have written a simple macro to allow me to GoalSeek over a range instead of a single cell (requiring that the values all be goalseeked to zero variance).

It's been a long time since I have worked with forms, however, and there is a lot I don't recall.

At this point, I have the main macro written and have attached it to a simple userform that I created (named: GoalSeekAcrossRanges). The form has 2 RefEdit ranges to gather ranges from the user, an OK command button, and CANCEL command button.

***My question is how I can launch/show the userform when I want to access the form? *** Ideally, I would want to launch it from a custom menu that I have built that launches based on pushing Ctrl+m (accesses an .xlam file that I saved as an add-in). Any help would be appreciated. Thanks.

Jared
I have included a bit of my sample code below. It is within the code of the Form:
--------------------------------------------------------------
Private Sub UserForm_Initialize()
Me.Show
End Sub

Private Sub cmdCancel_Click()
Me.Tag = "Canceled"
Me.Hide
End Sub

Private Sub cmdOK_Click()
GoalSeek
Me.Hide
End Sub

Sub GoalSeek()
Set rRange1 = Range(Me.RefEdit1.Text)
Set rRange2 = Range(Me.RefEdit2.Text)

For Each c In rRange1.Cells
i = i + 1
Set SetToCell = rRange1.Cells(i)
Set ByChangingCell = rRange2.Cells(i)
SetToCell.GoalSeek Goal:=0, ChangingCell:=ByChangingCell
Next
rRange1.Cells(1).Select
End Sub
 

Excel Facts

Copy PDF to Excel
Select data in PDF. Paste to Microsoft Word. Copy from Word and paste to Excel.
In a standard module create a sub to create and show the form - you can then call this from a button, custom menu, and so on:

Code:
[COLOR="Navy"]Sub[/COLOR] Show_My_Userform()
[COLOR="Navy"]Dim[/COLOR] f [COLOR="Navy"]As[/COLOR] UserForm1
    [COLOR="Navy"]Set[/COLOR] f = [COLOR="Navy"]New[/COLOR] UserForm1
    f.Show
    [COLOR="SeaGreen"]'//UserForm does stuff now...[/COLOR]
    Unload f
[COLOR="Navy"]End[/COLOR] [COLOR="Navy"]Sub[/COLOR]

In this case I've assumed that the class name of your userform is UserForm1 (which it often is by default if its the first userform you create and you don't change the name).
 
Upvote 0

Forum statistics

Threads
1,223,228
Messages
6,170,871
Members
452,363
Latest member
merico17

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