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

mhessnm

New Member
Joined
Apr 12, 2019
Messages
25
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.
 

Some videos you may like

Excel Facts

Back into an answer in Excel
Use Data, What-If Analysis, Goal Seek to find the correct input cell value to reach a desired result

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
44,587
Office Version
365
Platform
Windows
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
 

mhessnm

New Member
Joined
Apr 12, 2019
Messages
25
Thanks Fluff, this looks promising! I'll give it a try. Still learning, so I had no idea about this type of solution!
 

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
44,587
Office Version
365
Platform
Windows
Any problems let me know.
 

Subscribe on YouTube

Watch MrExcel Video

Forum statistics

Threads
1,106,070
Messages
5,509,103
Members
408,707
Latest member
Gabe B

This Week's Hot Topics

  • Turn fraction around
    Hello I need to turn a fraction around, for example I have 1/3 but I need to present as 3/1
  • TIme Clock record reformatting to ???
    Hello All, I'd like some help formatting this (Tbl-A)(Loaded via Power Query) [ATTACH type="full" width="511px" alt="PQdata.png"]22252[/ATTACH]...
  • TextBox Match
    hi, I am having a few issues with my code below, what I need it to do is when they enter a value in textbox8 (QTY) either 1,2 or 3 the 3 textboxes...
  • Using Large function based on Multiple Criteria
    Hello, I can't seem to get a Large formula to work based on two criteria's. I can easily get a oldest value based one value, but I'm struggling...
  • Can you check my code please
    Hi, Im going round in circles with a Compil Error End With Without With Here is the code [CODE=rich] Private Sub...
  • Combining 2 pivot tables into 1 chart
    Hello everyone, My question sounds simple but I do not know the answer. I have 2 pivot tables and 2 charts that go with this. However I want to...
Top