+ Reply to Thread
Results 1 to 15 of 15

Im new to vba and am having some issues with errors

  1. #1
    Registered User
    Join Date
    04-14-2014
    Location
    Baltimore, Maryland
    MS-Off Ver
    Excel 2010
    Posts
    7

    Im new to vba and am having some issues with errors

    Hi all,

    I am new to VBA in excel and am not the best. I follow these steps to create a user form but always get an error (more at bottom):

    1. Open a blank worksheet of your workbook.

    2. Press [Alt][F11].

    3. Go to Insert | User Form.

    4. If necessary, press [F4] to display Userform1 properties.

    5. Click in the Caption property box and change the name to Get Name and Education.

    6. Click the Label control and drag it to the form.

    7. Click in the Label's Caption property box and type Name:.

    8. Click the Text Box control in the Control toolbox and drag it to the form. Locate it to the right of the Label control.

    9. Click in the Name property of the Text Box control and type TextName.

    10. Click and drag the Frame control from the Control toolbox to the form and locate it below the text box.

    11. Click in the Caption property of the Frame control and type Education Level:.

    12. Click and drag the Check box control from the Control toolbox to the form and locate it within the frame.

    13. Change the Check box Name property to OptionHS.

    14. Change the Caption property of the Check box control to High School.

    15. Click and drag the Check box control to the form and position it below the first check box.

    16. Change the Check box Name property to OptionCollege.

    17. Change the Caption property of the Check box control to College.

    18. Click and drag the Check box control from the Control toolbox to the form and locate it below the second check box.

    19. Change the Check box Name property to OptionGrad.

    20. Change the Caption property to Graduate School.

    21. Click and drag the right border of the form to increase the form's size.

    22. Click the Command Button control in the Control toolbox, drag it to the form, and locate it next to the TextName control.

    23. Change the Caption property of the Command button to OK.

    24. Change the Name property to OKButton.

    25. Click in the Default Property box and select True.

    26. Click and drag the Command Button control to the form from the Control toolbox and locate it below the OK button.

    27. Change the Caption property to Cancel.

    28. Change the Name property to CancelButton.

    29. Click in the Cancel property box and select True.

    30. Press [Alt][F11].

    31. Right-click a toolbar and select Control Toolbox.

    32. Click the Command tool in the Control Toolbox.

    33. Click and drag to create the command button in your worksheet.

    34. Right-click the Command button, point to Object, and then select Edit.

    35. Select the default name and change it to Data Entry.

    36. Click outside the button to deselect it and then double-click the button.

    37. Enter Userform1.Show at the prompt.

    38. Double-click Userform1 in the VBA Project Window. (To open the form so you can add code to the rest of the form objects. Double-click the name of the form -- Userform1 -- listed under the Forms folder in the VBA Project window.)

    39. Double-click the Cancel button.

    40. Enter Unload UserForm1 at the prompt.

    41. Press [Shift][F7].

    42. Double-click the OK button.

    43. Enter the following code at the prompt:

    Sheets("Sheet1").Activate

    NextRow =Application WorksheetFunction.CountA(Range("A:A"))+1

    Application.WorksheetFunction.CountA(Range("A:A")) +1

    Cells(NextRow, 1)=TextName.Text

    If OptionHS Then Cells(NextRow, 2) ="High School"

    If OptionCollege Then Cells(NextRow, 2) ="College"

    If OptionGrad Then Cells(NextRow, 2)="Grad School"

    TextName.Text =""

    TextName.SetFocus

    44. Press [Alt]Q.

    45. Click the Exit Design Mode button in the Control toolbox.

    I keep getting a syntax error on the "Private Sub" line that pops up right before the first line of code "Sheets("Sheet1").Activate" and I cannot figure out why. Any help would be appreciated!

    Thanks in advance!

  2. #2
    Forum Contributor
    Join Date
    03-04-2014
    Location
    Canada Eh
    MS-Off Ver
    Excel 2010
    Posts
    199

    Re: Im new to vba and am having some issues with errors

    Can you post all your code and just your code?
    Click the * to give Rep to a post you like.

  3. #3
    Registered User
    Join Date
    04-14-2014
    Location
    Baltimore, Maryland
    MS-Off Ver
    Excel 2010
    Posts
    7

    Re: Im new to vba and am having some issues with errors

    Sure! I have:

    Private Sub OKButton_Click()


    Sheets("Sheet1").Activate

    NextRow =Application WorksheetFunction.CountA(Range("A:A"))+1

    Application.WorksheetFunction.CountA(Range("A:A")) +1

    Cells(NextRow, 1)=TextName.Text

    If OptionHS Then Cells(NextRow, 2) ="High School"

    If OptionCollege Then Cells(NextRow, 2) ="College"

    If OptionGrad Then Cells(NextRow, 2)="Grad School"

    TextName.Text =""

    TextName.SetFocus

    End Sub

  4. #4
    Forum Guru MarvinP's Avatar
    Join Date
    07-23-2010
    Location
    Woodinville, WA
    MS-Off Ver
    Office 365
    Posts
    16,334

    Re: Im new to vba and am having some issues with errors

    Hi Pearl and welcome to the forum,

    This sounds like a Scope problem to me... Read:
    http://vbahowto.com/2012/09/26/vba-variable-scope/
    http://www.cpearson.com/Excel/Scope.aspx

    I'm not sure the above will fix your problem exactly but because it is a Public vs Private problem it might.
    Also - where the code is located may also relate to your problem which I also call Scope.

    You can attach your workbook by clicking on the "Go Advanced" button below the message area and then on the Paper Clip Icon above the Advanced Message area. That would be much more helpful to us in figuring out your problem.
    Last edited by MarvinP; 04-14-2014 at 02:57 PM.
    One test is worth a thousand opinions.
    Click the * Add Reputation below to say thanks.

  5. #5
    Forum Contributor
    Join Date
    03-04-2014
    Location
    Canada Eh
    MS-Off Ver
    Excel 2010
    Posts
    199

    Re: Im new to vba and am having some issues with errors

    Try the following although I'm not sure if it will work because it depends on where it is being placed. I'm assuming a userform:
    Please Login or Register  to view this content.

  6. #6
    Registered User
    Join Date
    04-14-2014
    Location
    Baltimore, Maryland
    MS-Off Ver
    Excel 2010
    Posts
    7

    Re: Im new to vba and am having some issues with errors

    Hi, I tried that code and it didn't do anything, not even generate an error. Been looking at it this morning and still cnant figure it out.
    When I put in the original code, Private Sub OKButton_Click() gets highlighted and "NextRow =Application WorksheetFunction.CountA(Range("A:A"))+1" gets shown in red if that helps at all

  7. #7
    Registered User
    Join Date
    04-14-2014
    Location
    Baltimore, Maryland
    MS-Off Ver
    Excel 2010
    Posts
    7

    Re: Im new to vba and am having some issues with errors

    Ok so I was just trying different things and got it to work somwehat. This is what i have and it works, it writes out to the excel sheet, BUT it just keeps going. Everytime I hit the OK Button it just writes it to excel over and over as many times as you hit the OK Button. How would I fix it so that you're only allowed to hit the oK button once?

    Here's what I have:

    Private Sub OKButton_Click()
    Sheets("Sheet3").Activate

    NextRow = Application.WorksheetFunction.CountA(Range("A:A")) + 1

    Cells(NextRow, 1) = TextName.Text

    If OptionHS Then Cells(NextRow, 2) = "High School"

    If OptionCollege Then Cells(NextRow, 2) = "College"

    If OptionGrad Then Cells(NextRow, 2) = "Grad School"

    TextName.Text = ""

    TextName.SetFocus


    Thanks

  8. #8
    Forum Contributor
    Join Date
    03-04-2014
    Location
    Canada Eh
    MS-Off Ver
    Excel 2010
    Posts
    199

    Re: Im new to vba and am having some issues with errors

    Can you upload a sample workbook for us to look at? It should be the same as your current workbook except it should have no private or sensitive data in it.

  9. #9
    Registered User
    Join Date
    04-14-2014
    Location
    Baltimore, Maryland
    MS-Off Ver
    Excel 2010
    Posts
    7

    Re: Im new to vba and am having some issues with errors

    Mad-Mizer, How would I upload (or create) a sample workbook to upload? When I save my excel workbook, the code never saves.

    Also, forgive me if found inappropriate but random question. I see you're from Canada, Have you ever seen Gowan (Lawrence Gowan) live? Absolutely love him, he's such a good artist!

    Anyway, thanks for your help!!

  10. #10
    Forum Contributor
    Join Date
    03-04-2014
    Location
    Canada Eh
    MS-Off Ver
    Excel 2010
    Posts
    199

    Re: Im new to vba and am having some issues with errors

    Hi pearl27, as for creating the workbook just replace sensitive or personal data with non-sensitive or non-personal data and save it as a different name. As for saving the code in it, you must make sure you are saving as a Macros enabled workbook. To do so, click save as, and where it says "Save as type:" click and select "Excel Macro-Enabled Workboo (*.xlsm). Then upload it to the site (see forum rules about that).

    With respects to Gowan, lol, that's so 80s! I don't even know if he tours anymore but no I haven't seen him live. Back in the 80s I was into heavy music like Judas Priest and other bands of similar genres. I didn't mind Gowan though but he just wasn't heavy enough for me.

  11. #11
    Registered User
    Join Date
    04-14-2014
    Location
    Baltimore, Maryland
    MS-Off Ver
    Excel 2010
    Posts
    7

    Re: Im new to vba and am having some issues with errors

    Sample.xlsm



    Lol the 70's/80's were the best years for music. Gowan is actually with Styx now, ever since '95 I believe. Seen him quite a few times with them lol but really hoping to see an actual Gowan show sometime!

  12. #12
    Forum Contributor
    Join Date
    03-04-2014
    Location
    Canada Eh
    MS-Off Ver
    Excel 2010
    Posts
    199

    Re: Im new to vba and am having some issues with errors

    You had the button procedure in the wrong module, it should have been placed within the userform module. See the workbook, it should be working how you want it now.
    Attached Files Attached Files

  13. #13
    Registered User
    Join Date
    04-14-2014
    Location
    Baltimore, Maryland
    MS-Off Ver
    Excel 2010
    Posts
    7

    Re: Im new to vba and am having some issues with errors

    I tried it now and it it prints the school out twice when I click OK multiple times. The name part is right because that only prints out once no matter how many times the OK button is pressed but the school gets printed twice

  14. #14
    Forum Contributor
    Join Date
    03-04-2014
    Location
    Canada Eh
    MS-Off Ver
    Excel 2010
    Posts
    199

    Re: Im new to vba and am having some issues with errors

    Sorry, I jumped the gun in sending that as i am at work and got sidetracked. Give me a few and I'll get you sorted out.

  15. #15
    Forum Contributor
    Join Date
    03-04-2014
    Location
    Canada Eh
    MS-Off Ver
    Excel 2010
    Posts
    199

    Re: Im new to vba and am having some issues with errors

    Hi pearl27, I've finished this and there are no duplicate entries. Give it a try and let me know how it goes.
    Attached Files Attached Files

+ 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. Data Validation List Issues/Sorting Issues
    By taylorbe in forum Excel Formulas & Functions
    Replies: 2
    Last Post: 03-12-2014, 09:53 AM
  2. Listbox Issues / Errors with UserForm
    By Chris270 in forum Excel Programming / VBA / Macros
    Replies: 15
    Last Post: 01-04-2012, 07:14 AM
  3. [SOLVED] Excel Throwing Circular Errors When No Errors Exist
    By MDW in forum Excel Formulas & Functions
    Replies: 1
    Last Post: 08-10-2006, 09:20 AM
  4. Various errors
    By knowtrump in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 01-22-2006, 08:15 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