+ Reply to Thread
Results 1 to 26 of 26

Excel VBA Form Coding Error

  1. #1
    Forum Contributor
    Join Date
    02-24-2008
    Location
    UK
    MS-Off Ver
    2012 and 2016
    Posts
    123

    Excel VBA Form Coding Error

    I have set up a form named AddNameForm in my workbook that has input boxes named Forename and SURNAME (see below).

    When I call the module of which the following code is part I get the line show below flagged as an error:

    Please Login or Register  to view this content.
    ' Etc, etc

    I cannot understand why it recognises this form on the previous line but fails when the same form name is used on the next line.

    Can anyone help?

    Also the next line in the above code needs to put the SURNAME and Forename into the string variable called Name. I want to ensure the result is Forename SURNAME (e.g. Joe BLOGGS) and ensure that whoever enters the name in the form does not inadvertently put any trailing blanks after the name.

    How can the be done? Thanks in anticipation.
    Attached Images Attached Images
    Last edited by kencoburn; 04-15-2017 at 10:15 AM.

  2. #2
    Forum Expert gmr4evr1's Avatar
    Join Date
    11-24-2014
    Location
    Texas
    MS-Off Ver
    Office 2010 and 2007
    Posts
    3,448

    Re: Excel VBA Form Coding Error

    I don't think you need both
    Please Login or Register  to view this content.
    One or the other will work as they both do the same thing. I think you are getting the error because it's trying to show the userform when it is already loaded.

    As for your other inquiry, you may need to attach a sample of the workbook with the userform and code you are using so we can see how it's all working...or not working and help you out better.
    Last edited by gmr4evr1; 04-15-2017 at 10:38 AM.
    1N73LL1G3NC3 15 7H3 4B1L17Y 70 4D4P7 70 CH4NG3 - 573PH3N H4WK1NG
    You don't have to add Rep if I have helped you out (but it would be nice), but please mark the thread as SOLVED if your issue is resolved.

    Tom

  3. #3
    Forum Guru
    Join Date
    07-25-2011
    Location
    Florida
    MS-Off Ver
    Excel 2003
    Posts
    9,653

    Re: Excel VBA Form Coding Error

    1.) Don't know why it errors, but you don't need to both Load and Show the userform. Just remove the .Show command.
    2.) The Application.Trim Function will remove any leading or trialing spaces if they exist.
    3.) Is bad programming practice to use the End function by itself as it abruptly ends all vba execution. It's safer to use Exit Sub instead.
    4.) The keyword Name is a VBA object. While it's leagle to have a variable called Name, it's not good programming practice to use variables that are the same as vba keywords.


    Please Login or Register  to view this content.
    Last edited by AlphaFrog; 04-15-2017 at 10:50 AM.
    Surround your VBA code with CODE tags e.g.;
    [CODE]your VBA code here[/CODE]
    The # button in the forum editor will apply CODE tags around your selected text.

  4. #4
    Forum Contributor
    Join Date
    02-24-2008
    Location
    UK
    MS-Off Ver
    2012 and 2016
    Posts
    123

    Re: Excel VBA Form Coding Error

    I hope that is sufficient information.
    Attached Images Attached Images

  5. #5
    Forum Contributor
    Join Date
    02-24-2008
    Location
    UK
    MS-Off Ver
    2012 and 2016
    Posts
    123

    Re: Excel VBA Form Coding Error

    Oops, it appears that the module code that uses the form input was not uploaded. Here is a sample (I have to remove repeating coded to get withing the upload limit). The areas were the code has been removed is shown by the embedded comments.

    Please Login or Register  to view this content.

  6. #6
    Forum Contributor
    Join Date
    02-24-2008
    Location
    UK
    MS-Off Ver
    2012 and 2016
    Posts
    123

    Re: Excel VBA Form Coding Error

    Many thanks for the assistance given (so far). I have changed the AddName module code to rename the Name variable
    to strName string variable. I have removed the second instance of Load AdNameForm and so do not get that error.

    I would now be grateful for your assistance (from anyone) to fix another problem that is preventing my AddName
    module code from running. I have attached an image of the line where the code fails (this is only a section of the full
    AddName module code). The Forename and SURNAME variables are the names I have given to the forename and surname
    text boxes in the AddNameForm form. When a members name is entered in the form I was expecting the Forename and SURNAME values
    to be available in the AddNames module so that I could trim the values (to ensure there was no leading or trailing blanks)
    and then concatenate the Forename and SURNAME to produce the strName variable.

    The line of code that fails produces a 424 "Object Required" error message.
    Attached Images Attached Images

  7. #7
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,645

    Re: Excel VBA Form Coding Error

    If SURNAME and Forename are controls on a userform then to refer to them you'll also need to reference the userform.

    For example, if the userform was named UserForm1.
    Please Login or Register  to view this content.
    By the way, why is all the code in a standard module - couldn't you have the code in the userform module?

    PS Using Load and Show shouldn't cause a problem - Load loads the userform into memory but doesn't display it, Show displays the userform.

    PPS If you are using the userform's Initialize event it will be triggered when you load it and if you are using the userform's Activate event that will be triggered when you show it.
    If posting code please use code tags, see here.

  8. #8
    Forum Contributor
    Join Date
    02-24-2008
    Location
    UK
    MS-Off Ver
    2012 and 2016
    Posts
    123

    Re: Excel VBA Form Coding Error

    Thanks Guru, that worked. I still have to add some verification to the code (i.e. to ensure that both the surname and forename have been entered in the form).

    The reason I did not embed the code in the userform's initialisation event was because I had already written the code in the AddName module when I was using the InputBox function to input the members' name. To extend my knowledge of Excel VBA I thought I would have a go at doing the same with a form which gives me more control of the look and verification of the input.

    After completing the verification coding I intend to produce a form/module to allow names to be deleted by selecting a name form a drop-down list. I already have the module code for doing this using the InputBox function but this relies on the user typing in the name exactly - something that is prone to error. I may need your help again!!

    Once again, many thanks.

  9. #9
    Forum Contributor
    Join Date
    02-24-2008
    Location
    UK
    MS-Off Ver
    2012 and 2016
    Posts
    123

    Re: Excel VBA Form Coding Error

    I now want to modify coding in a module that uses a InputBox to input the name of a member so that it can be deleted from a
    number of worksheets lists (of members). The Input box method is prone to typing errors and so I want the user to be able to select the
    members name from a ListBox in a Form. I have produced the form (see image) and named the following in the form:

    Frame: DeleteMember
    Form: DeleteNameForm
    ListBox Name: MemberList (set as a named range in list of members worksheet)
    Button: DeleteNameButton


    I have a module FIndAndDelete that works using the InputBox method. All I need to do is take the selected value from the DeleteNameForm and enter this as a
    string value.

    I want to obtain the selected value (the selected name) so that I can use in a module that finds the name in a long list of members and deletes the whole row.
    I have tried the following code but this produces an error 438 "Object does not support this property or method".

    Please Login or Register  to view this content.

    Any advice would be most welcome.
    Attached Images Attached Images

  10. #10
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,645

    Re: Excel VBA Form Coding Error

    Where do you get the error?

    Where is the code you posted located?

  11. #11
    Forum Guru
    Join Date
    07-25-2011
    Location
    Florida
    MS-Off Ver
    Excel 2003
    Posts
    9,653

    Re: Excel VBA Form Coding Error

    You're using two different variables.

    Please Login or Register  to view this content.

  12. #12
    Forum Contributor
    Join Date
    02-24-2008
    Location
    UK
    MS-Off Ver
    2012 and 2016
    Posts
    123

    Re: Excel VBA Form Coding Error

    Thanks for the responses. The code is in a module called FindAndDelete that I have use to enter a name, search for that name in 14 worksheets, and
    then delete the row that contains the name. I currently use an InputBox function in the FindAndDelte module to enter the name but I want to use a form to select
    the name from a ListBox in the DeleteNameForm to ensure that the correct name is selected (no chance of typing errors). The relevant section of code
    using the InputBox method is as follows:

    Please Login or Register  to view this content.
    I want to use the DeleteNameButton click event to 'call' the FindAndDelete module. What code do I need in the DeleteNameForm?

    How do I change the above module code to take the selected name from the form and make this the search string?

  13. #13
    Forum Guru
    Join Date
    07-25-2011
    Location
    Florida
    MS-Off Ver
    Excel 2003
    Posts
    9,653

    Re: Excel VBA Form Coding Error

    Quote Originally Posted by kencoburn View Post

    I want to use the DeleteNameButton click event to 'call' the FindAndDelete module. What code do I need in the DeleteNameForm?

    How do I change the above module code to take the selected name from the form and make this the search string?
    Please Login or Register  to view this content.

  14. #14
    Forum Contributor
    Join Date
    02-24-2008
    Location
    UK
    MS-Off Ver
    2012 and 2016
    Posts
    123

    Re: Excel VBA Form Coding Error

    Thanks for your assistance. I want to call the existing FindAndDelete module from the Delete Name button in the DeleteNameForm rather than
    place all the code in the DeleteNameForm click event area of code. I did a similar thing for adding names to many worksheets (Nov 2016,
    Dec 2016 ........Oct 2017) and it worked (see first image). I want to do the same to find and delete a name but I get an error message
    (see second image).

    Once I get the button click event to work (calling the FindAndDelete module it should work (hopefully). Here is your code that I modified
    which I think will work:


    Please Login or Register  to view this content.

  15. #15
    Forum Contributor
    Join Date
    02-24-2008
    Location
    UK
    MS-Off Ver
    2012 and 2016
    Posts
    123

    Re: Excel VBA Form Coding Error

    I forgot to add the images - here they are.
    Attached Images Attached Images

  16. #16
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,645

    Re: Excel VBA Form Coding Error

    You don't call modules, you call subs.

  17. #17
    Forum Guru
    Join Date
    07-25-2011
    Location
    Florida
    MS-Off Ver
    Excel 2003
    Posts
    9,653

    Re: Excel VBA Form Coding Error

    Looks like the module and procedure are both called FindAndDelete. Try renaming the module, or call the macro by qualifying the module and macro.

    Please Login or Register  to view this content.

  18. #18
    Forum Contributor
    Join Date
    02-24-2008
    Location
    UK
    MS-Off Ver
    2012 and 2016
    Posts
    123

    Re: Excel VBA Form Coding Error

    Thanks to you both - I now see the problem - the module and sub have the same name. When I used AlphaFrog's solution (Call FindAndDelete.FindAndDelete it worked. I will fix the confusion about module and subs by changing the name of the module. I am trying to learn how to use Forms in VBA and so all your help and advice has
    been very useful.

  19. #19
    Forum Contributor
    Join Date
    02-24-2008
    Location
    UK
    MS-Off Ver
    2012 and 2016
    Posts
    123

    Re: Excel VBA Form Coding Error

    It's me again!

    I have tried for hours to get the above code to work but have failed to do so. Your further assistance would be much appreciated.

    Here is the relevant section of code:

    Please Login or Register  to view this content.
    I have created the form shown in the attached image to 'call' the FindAndDeleteName2() subroutine of which the code above is part.

    When I run the subroutine the DeleteNameForm form appears on the screen. I have designated the range for the list box as VetsNames in the Members worksheet which displays the names as shown
    in the image but when I click on a name in the list nothing happens, the form remains on the screen and the only way to remove the form from the screen
    it to click on the Cancel button (or the X).

    The code for the "Delete Name" button on the form is shown below:


    Please Login or Register  to view this content.
    Attached Images Attached Images

  20. #20
    Forum Guru
    Join Date
    07-25-2011
    Location
    Florida
    MS-Off Ver
    Excel 2003
    Posts
    9,653

    Re: Excel VBA Form Coding Error

    Remove these two lines from FindAndDeleteName2

    Please Login or Register  to view this content.

    Remove this from DeleteNameButton_Click

    Please Login or Register  to view this content.

  21. #21
    Forum Contributor
    Join Date
    02-24-2008
    Location
    UK
    MS-Off Ver
    2012 and 2016
    Posts
    123

    Re: Excel VBA Form Coding Error

    Thanks for responding. I deleted the two lines in the FIndAndDeleteName2 module and The Unload DeleteNameForm line from the DeleteNameButton_Click event. Now the form does not appear and I get the "No Name Selected Message'. I would have thought the DeleteNameForm would need to be shown to allow a name to be chosen from the MemberList box.

  22. #22
    Forum Guru
    Join Date
    07-25-2011
    Location
    Florida
    MS-Off Ver
    Excel 2003
    Posts
    9,653

    Re: Excel VBA Form Coding Error

    Quote Originally Posted by kencoburn View Post
    Thanks for responding. I deleted the two lines in the FIndAndDeleteName2 module and The Unload DeleteNameForm line from the DeleteNameButton_Click event. Now the form does not appear and I get the "No Name Selected Message'. I would have thought the DeleteNameForm would need to be shown to allow a name to be chosen from the MemberList box.
    It does need to be shown, but don't show the userform from the same procedure that deletes the row.

    These steps should be in separate procedures.
    -UserForm Show
    -Delete the Row


    You could just move the code you listed to the DeleteNameButton_Click and add back DeleteNameForm.Show to FIndAndDeleteName2


    Please Login or Register  to view this content.

  23. #23
    Forum Contributor
    Join Date
    02-24-2008
    Location
    UK
    MS-Off Ver
    2012 and 2016
    Posts
    123

    Re: Excel VBA Form Coding Error

    I left the main code in the FindAndDeleteName2() module because I have lots of other code in this module. I commented out the load and show DeleteNameForm
    lines and added the DeleteNameForm.Show line to the button click event. The form now appears but when I click on the Delete Name button I get the following
    error message:

    Runtime Error 400. Form already displayed; can't show modally.

  24. #24
    Forum Guru
    Join Date
    07-25-2011
    Location
    Florida
    MS-Off Ver
    Excel 2003
    Posts
    9,653

    Re: Excel VBA Form Coding Error

    Quote Originally Posted by kencoburn View Post
    I left the main code in the FindAndDeleteName2() module because I have lots of other code in this module. I commented out the load and show DeleteNameForm
    lines and added the DeleteNameForm.Show line to the button click event. The form now appears but when I click on the Delete Name button I get the following
    error message:

    Runtime Error 400. Form already displayed; can't show modally.
    Just move the portion of code that deletes the row to DeleteNameButton_Click as I indicated post 22. Don't move all of the FindAndDeleteName2 code.


    Please Login or Register  to view this content.
    Please Login or Register  to view this content.

  25. #25
    Forum Contributor
    Join Date
    02-24-2008
    Location
    UK
    MS-Off Ver
    2012 and 2016
    Posts
    123

    Re: Excel VBA Form Coding Error

    I did not understand your latest suggestion to move some of the module code to the click event (my problem) and so I gave up trying to call the delete name module from the click event and pasted all the module code into the click event as you suggested earlier. That worked!! Many thanks for your help.

  26. #26
    Forum Contributor
    Join Date
    02-24-2008
    Location
    UK
    MS-Off Ver
    2012 and 2016
    Posts
    123

    Re: Excel VBA Form Coding Error

    I have now got to the stage (thanks to help from this forum) where I need to validate
    the selection made by the user to ensure that a member is not deleted if there has
    been a financial transaction in this financial year. The attached image shows the
    "Members" worksheet and the "Total Payments" column which is the sum of the 12 monthly
    worksheets against that particular name. I want to prevent the user from deleting
    a member's name if the "Total Payments" column entry is not zero. I tried the following
    code (in bold) but got a compile error. Perhaps there is a different way of achieving this aim.

    Please Login or Register  to view this content.
    'and same for December, January, February, etc worksheets
    Attached Images Attached Images
    Last edited by kencoburn; 04-26-2017 at 01:08 PM.

+ 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. VBA Coding to transfer data from Word Form to Excel
    By the_yeti in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 10-12-2016, 03:03 PM
  2. [SOLVED] In need of help regarding combo box coding and button coding (Access form project)
    By mailblade in forum Access Tables & Databases
    Replies: 2
    Last Post: 01-09-2016, 01:34 AM
  3. Replies: 3
    Last Post: 08-24-2015, 09:54 AM
  4. Using VBA to transfer data from Excel form to a web form. error 'Permission Denied'
    By danieldowds in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 03-14-2014, 08:14 AM
  5. Need Excel Form(s) and Coding Help
    By Celes** in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 06-14-2012, 05:24 PM
  6. Coding script error-cmd button to open form and for to add new entry to list
    By Southfish in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 04-30-2012, 05:43 AM
  7. [SOLVED] Excel Coding Error!
    By Killer in forum Excel Formulas & Functions
    Replies: 9
    Last Post: 04-23-2006, 08:55 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