+ Reply to Thread
Results 1 to 9 of 9

Coding with Multiple userforms using modules

  1. #1
    Registered User
    Join Date
    04-12-2019
    Location
    Cardiff
    MS-Off Ver
    2016
    Posts
    11

    Coding with Multiple userforms using modules

    Hello to all in the forum, I am sorry if this question has already been asked but I'm new to the forum and don't know how to search.

    I have 20 different userforms that I use to output different data but the userforms have some commonality such as information relating to customers.
    I find that it's better to call the code from a module that repeats as its a cleaner way to code, but I find myself having to repeat the code for the same output.

    for example

    if userform1.combox1.value = "something" THEN
    COMBOBOX2.VALUE = "SOMETHING ELSE"

    if userform2.combox1.value = "something" THEN
    COMBOBOX2.VALUE = "SOMETHING ELSE"

    AND SO ON.

    COULD I NOT WRITE SOMETHING LIKE

    IF (WITH ACTIVE USERFORM).combox1.value = "something" THEN
    COMBOBOX2.VALUE = "SOMETHING ELSE"

    i hope this makes sense.

    ok look at this

    If UserForm3.Visible Then
    Email_Subject = "New" & " " & UserForm3.prodname & " " & UserForm3.TextBox58 & " " & "for" & " " & UserForm3.ComboBox18 & " " & "is ready for Sales Processing"
    BODY1 = "I am pleased to inform you that the" & " " & UserForm3.prodname & " " & UserForm3.TextBox58 & " " & "for" _
    & " " & UserForm3.ComboBox18 & " " & "is completed and available on the System. Customer Reference:" & " " & EMAIL1.TextBox1 & ", Order No:" & " " & EMAIL1.TextBox2
    BODY2 = "QUANTITY: " & " " & EMAIL1.TextBox3
    BODY3 = "CAPACITY: " & " " & UserForm3.ComboBox1 & " " & "KG"
    BODY4 = "HUB: " & " " & UserForm3.ComboBox7 & " " & "SERIES"
    BODY5 = "P.C.D: " & " " & UserForm3.ComboBox10
    BODY6 = "DROPARM: " & " " & UserForm3.ComboBox5 & " " & UserForm3.ComboBox6
    BODY7 = "ARM ANGLE: " & " " & UserForm3.ComboBox9 & " " & "º"
    BODY8 = "c - (OHF): " & " " & UserForm3.TextBox7 & "mm"
    BODY9 = "b - (MBC): " & " " & UserForm3.TextBox8 & "mm"
    BODY10 = "WHEEL FITTINGS: " & " " & UserForm3.ComboBox17 & " " & UserForm3.ComboBox42
    BODY11 = "PART NUMBER FOR FITTING: " & " " & UserForm3.ComboBox37 & " " & UserForm3.ComboBox53
    BODY12 = "====================================================================="
    BODY13 = "Reason? " & " " & UserForm3.ComboBox52
    'BODY14 = "Weight " & " " & UserForm3.TextBox57 & "KG"
    BODY15 = "ADDITIONAL INFORMATION: " & " " & EMAIL1.TextBox4

    End If

    If UserForm4.Visible Then

    Email_Subject = "New" & " " & UserForm4.prodname & " " & UserForm4.TextBox58 & " " & "for" & " " & UserForm4.ComboBox18 & " " & "is ready for Sales Processing"
    BODY1 = "I am pleased to inform you that the" & " " & UserForm4.prodname & " " & UserForm4.TextBox58 & " " & "for" _
    & " " & UserForm4.ComboBox18 & " " & "is completed and available on the System. Customer Reference:" & " " & EMAIL1.TextBox1 & ", Order No:" & " " & EMAIL1.TextBox2
    BODY2 = "QUANTITY:" & " " & EMAIL1.TextBox3
    BODY3 = "CAPACITY:" & " " & UserForm4.ComboBox1 & " " & "KG"
    BODY4 = "HUB:" & " " & UserForm4.ComboBox7 & " " & "SERIES"
    BODY5 = "P.C.D:" & " " & UserForm4.ComboBox10
    BODY6 = "DROPARM:" & " " & UserForm4.ComboBox5 & " " & UserForm4.ComboBox6
    BODY7 = "ARM ANGLE:" & " " & UserForm4.ComboBox9 & " " & "º"
    BODY8 = "c - (OHF):" & " " & UserForm4.TextBox7 & "mm"
    BODY9 = "b - (MBC):" & " " & UserForm4.TextBox8 & "mm"
    BODY10 = "WHEEL FITTINGS:" & " " & UserForm4.ComboBox17 & " " & UserForm4.ComboBox42
    BODY11 = "PART NUMBER FOR FITTING:" & " " & UserForm4.ComboBox37 & " " & UserForm4.ComboBox53
    BODY12 = "=================================================================================="
    BODY13 = "Reason?" & " " & UserForm4.ComboBox52
    'BODY14 = "Weight" & " " & UserForm4.TextBox57 & "KG"
    BODY15 = "ADDITIONAL INFORMATION:" & " " & EMAIL1.TextBox4
    ElseIf UserForm4.CheckBox9 = True Then
    BODY12 = "OP55? YES"
    ElseIf UserForm4.CheckBox10 = True Then
    BODY12 = "OP55? NO"

    End If


    see how I use the if visible see how I have to repeat the same code twice, once for userform3 and then again for userform4 but the outputs are near the same
    Last edited by Johndotcom; 10-05-2020 at 07:10 AM.

  2. #2
    Forum Expert rorya's Avatar
    Join Date
    08-13-2008
    Location
    East Sussex, UK
    MS-Off Ver
    365 Ent Monthly Channel / Insiders Beta
    Posts
    8,913

    Re: Coding with Multiple userforms using modules

    That's a bit vague, but it sounds like you should be passing the relevant controls as arguments to a routine, or using a function.
    Rory

  3. #3
    Forum Expert Greg M's Avatar
    Join Date
    08-16-2007
    Location
    Dublin. Ireland
    MS-Off Ver
    Office 2016
    Posts
    4,481

    Re: Coding with Multiple userforms using modules

    Hi John,

    Welcome to the forum.

    It's fairly likely that what you ask can be achieved, but it would be much easier to make meaningful suggestions if we could see what your UserForms look like.

    Can you post a copy of your workbook on here? We probably don't need to see the data, so you can delete them if they're sensitive, but seeing your UserForms and code would be very helpful.

    Regards,

    Greg M

  4. #4
    Registered User
    Join Date
    04-12-2019
    Location
    Cardiff
    MS-Off Ver
    2016
    Posts
    11

    Re: Coding with Multiple userforms using modules

    Hi Greg M thanks for the reply, I have added more to me request on the hope that its no longer vague, my excel file is rather large and comprehensive, and I'm subject to data protection in the company I work for, so I hope you can provide me with an answer with out. sorry.

  5. #5
    Registered User
    Join Date
    04-12-2019
    Location
    Cardiff
    MS-Off Ver
    2016
    Posts
    11

    Re: Coding with Multiple userforms using modules

    Hello Rotya,

    thanks for the swift prompt I have added more code does it make more sense now?

  6. #6
    Forum Expert rorya's Avatar
    Join Date
    08-13-2008
    Location
    East Sussex, UK
    MS-Off Ver
    365 Ent Monthly Channel / Insiders Beta
    Posts
    8,913

    Re: Coding with Multiple userforms using modules

    How are you calling this code?

  7. #7
    Forum Expert Greg M's Avatar
    Join Date
    08-16-2007
    Location
    Dublin. Ireland
    MS-Off Ver
    Office 2016
    Posts
    4,481

    Re: Coding with Multiple userforms using modules

    Hi again,

    Thanks for the prompt response. I quite understand if your workbook is too large/sensitive to post here.

    The code you posted seems (to me anyway ) to be a bit too vague to make useful suggestions.

    A mistake MANY people make is to pack many actions into their UserForm code, but in reality, the principal purpose of a UserForm is simply to retrieve data entered by a User. This data is then passed back to a calling routine which processes that data and takes whatever actions are appropriate. Using this approach means that a UserForm is more likely to be useable in several situations rather than being "tied" to a specific worksheet or range.

    If you keep the above principle in mind you may be able to design your UserForms to use code more efficiently.

    Sorry I can't make more specific suggestions.

    Hope this might be of some help.

    Regards,

    Greg M

  8. #8
    Registered User
    Join Date
    04-12-2019
    Location
    Cardiff
    MS-Off Ver
    2016
    Posts
    11

    Re: Coding with Multiple userforms using modules

    With a Button from the userforms, the button has a common reference such as button1 on both userforms.

  9. #9
    Forum Expert rorya's Avatar
    Join Date
    08-13-2008
    Location
    East Sussex, UK
    MS-Off Ver
    365 Ent Monthly Channel / Insiders Beta
    Posts
    8,913

    Re: Coding with Multiple userforms using modules

    If you are calling it from the userform, then you should either pass it as an argument (there wouldn't then be a need to check each userform in the code), or use something like a user-defined type or class to hold the information that you need in the routine and simply pass that. (You could pass all the relevant values as separate arguments, but that gets ugly with so many of them)

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. How to import multiple Modules and userforms from one workbook to another
    By ImranBhatti in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 02-27-2017, 01:03 PM
  2. Does not save Modules & Userforms
    By CobraLAD in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 02-05-2013, 12:11 PM
  3. Using Variables from Modules in Userforms
    By googlebot in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 09-09-2008, 02:57 PM
  4. Exporting UserForms and or Modules
    By Rick_Stanich in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 03-12-2008, 12:37 PM
  5. [SOLVED] Copying UserForms & Modules to another workbook
    By excelnut1954 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 05-24-2006, 05:25 PM
  6. comboboxes, userforms and class modules
    By natanz in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 11-29-2005, 11:10 AM
  7. Coding for Userforms
    By Abhay Sanan in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 05-27-2005, 03:05 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1