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

Which lookup functions find a value equal or greater than the lookup value?
MATCH uses -1 to find larger value (lookup table must be sorted ZA). XLOOKUP uses 1 to find values greater and does not need to be sorted.

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
42,424
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
42,424
Office Version
365
Platform
Windows
Any problems let me know.
 

Watch MrExcel Video

Forum statistics

Threads
1,101,934
Messages
5,483,774
Members
407,410
Latest member
catherinejoy

This Week's Hot Topics

  • Finding issue in If elseif else with For each Loop
    Finding issue in If elseif else with For each Loop I have tried this below code but i'm getting in Y column filled with W005. Colud you please...
  • MsgBox Error
    Hi Guys, I have the below error show up when i try and run my macro in File1 but works fine if i copy and paste the same code into file2. [ATTACH...
  • CELL FORMAT - IF CONDITION
    My Cell Format is [B]""0.00" Cr". [/B]But in the cell, it is showing 123.00 for editing. (123 is entry figure). (Data imported from other...
  • Show numbers nearly the same
    Is this possible. I have a number that can change very time eg 0.00001234 Then I have a lot of numbers 0.0000001, 0.0000002, 0.00000004...
  • Please i need your help to create formula
    I need a formula in cell B8 to do this >>if b1=1 then multiply ( cell b8) by 10% ,if b1=2 multiply by 20%,if=3 multiply by 30%. Thank you in...
  • Got error while adding column and filter
    Got error while adding column and filter In column Z has some like "Success" and "Error". I want to add column in AA if the Z cell value is...
Top