Using With...End statement to set fields for two userforms

mhessnm

Board Regular
Joined
Apr 12, 2019
Messages
57
Office Version
  1. 365
Platform
  1. Windows
Hello,

I am a VBA neophyte learning on the job. I have two userforms that I am programming for a spreadsheet. Each user form uses many of the same fields, and so in the code for each userform I have to set the fields up in the same way, i.e. below

txtTodayDate.Text = Format(Now(), "m/d/yyyy")

cboDischargeType.AddItem "Administrative"

I have a number of these items, and to streamline I thought that it might be best to put the code in its own module so that I could have the userform_initialize in each userform call that module to set the fields - rather than needing duplicate code in each userform. After testing and getting an object error, I realized that outside of the userform_initialize, I need to prefix each field with the name of the userform, like so:

frmInput.txtTodayDate...
frmInput.cboDischargeType...

Is it possible, using a global variable and a With...End command, to run this code so that fields are set for the proper userform, therefore using the same field settings for both userforms? If so, how do I structure the With...End? In summary, what I envision is something as follows:

Calling module code
Sets global variable formtype "iform" or "eform"
Calls userform

Userform code
Calls field setup module

Field setup module
If iform
Sets prefix variable to "frmInput"
Else
Sets prefix variable to "frmEdit"
End If
Runs With...End setting fields using proper form (frmInput or frmEdit)
(I don't know how to structure that. Would it be "With prefix...field setting commands...End"?


Am I making this too complex? Is With...End not appropriate? My goal is for a more streamlined product.

Thank you in advance for any assistance.
 

Excel Facts

Why does 9 mean SUM in SUBTOTAL?
It is because Sum is the 9th alphabetically in Average, Count, CountA, Max, Min, Product, StDev.S, StDev.P, Sum, VAR.S, VAR.P.
You could do something like
Rich (BB code):
Private Sub UserForm_Initialize()
   Call UfSetup(Me)
End Sub

and then in a standard module
Rich (BB code):
Sub UfSetup(Uf As MSForms.UserForm)
  Uf.TextBox1.Value = "Fluff"
  Uf.ComboBox1.AddItem "Test"
End Sub
 
Upvote 0
Solution
Thanks Fluff, this looks promising! I'll give it a try. Still learning, so I had no idea about this type of solution!
 
Upvote 0
Any problems let me know.
 
Upvote 0

Forum statistics

Threads
1,223,903
Messages
6,175,289
Members
452,631
Latest member
a_potato

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