Results 1 to 9 of 9

Thread: VBA USerform error
Thanks Thanks: 0 Likes Likes: 0

  1. #1
    New Member
    Join Date
    Jul 2019
    Posts
    5
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Default VBA USerform error

    I have created a couple of user forms in vba, they work fine until I click anywhere in the user form that is NOT a field, when I click outside of a field, I get the following error:

    Compile error, Sub or Function not defined. Again, this only happens when I click inside the userform, but not in one of the fields, my code is below. Thanks in advance for any help!


    Code:
    Private Sub UserForm_Activate()
    Me.txtDay.Value = Format(Now(), "MM/DD/YYYY")
    
    
    End Sub
    
    
    Private Sub txtMachine_Change()
    
    
    End Sub
    
    
    Private Sub UserForm_Click()
    Begin EggProdData
    Me.txtDay.Value = Format(Now(), "MM/DD/YYYY")
       Caption = "Data Entry"
       ClientHeight = 6360
       ClientLeft = 45
       ClientTop = 375
       ClientWidth = 7770
       OleObjectBlob = "EggProdData.frx":
       StartUpPosition = 1    'CenterOwner
    End Sub
    
    
    
    
    Private Sub cboGrower_DropButt*******()
        'Populate control.
        Me.cboGrower.AddItem "CW1 4004"
        Me.cboGrower.AddItem "CW1 4005"
        Me.cboGrower.AddItem "CW2 4020"
        Me.cboGrower.AddItem "CW2 4021"
        Me.cboGrower.AddItem "CG1 4024"
        Me.cboGrower.AddItem "CG1 4025"
        Me.cboGrower.AddItem "CG2 4026"
        Me.cboGrower.AddItem "CG2 4027"
        Me.cboGrower.AddItem "3R1 4032"
        Me.cboGrower.AddItem "3R1 4033"
        Me.cboGrower.AddItem "3R2 4034"
        Me.cboGrower.AddItem "3R2 4035"
        Me.cboGrower.AddItem "RM 4036"
        Me.cboGrower.AddItem "RM 4037"
        Me.cboGrower.AddItem "CLD 4038"
        Me.cboGrower.AddItem "CLD 4039"
        Me.cboGrower.AddItem "HICO1 4040"
        Me.cboGrower.AddItem "HICO1 4041"
        Me.cboGrower.AddItem "HICO2 4042"
        Me.cboGrower.AddItem "HICO2 4043"
        
        
    End Sub
    
    
    
    
    Private Sub cboWeek_DropButt*******()
        'Populate control.
        Me.cboWeek.AddItem "1"
        Me.cboWeek.AddItem "2"
        Me.cboWeek.AddItem "3"
        Me.cboWeek.AddItem "4"
        Me.cboWeek.AddItem "5"
       
    End Sub
    
    
    
    
    
    
    Private Sub cmdAdd_Click()
        'Copy input values to sheet.
        Dim lRow As Long
        Dim ws As Worksheet
        Set ws = Worksheets("LiveData")
        lRow = ws.Cells(Rows.Count, 2).End(xlUp).Offset(1, 0).Row
        With ws
            .Cells(lRow, 2).Value = Me.cboGrower.Value
            .Cells(lRow, 3).Value = Me.cboWeek.Value
            .Cells(lRow, 4).Value = Me.txtDay.Value
            .Cells(lRow, 5).Value = Me.txtMachine.Value
            .Cells(lRow, 6).Value = Me.txtEggsSet.Value
            .Cells(lRow, 7).Value = Me.txtHatch.Value
            
        End With
        
        'Clear input controls.
        Me.cboGrower.Value = ""
        Me.cboWeek.Value = ""
        Me.txtMachine.Value = ""
        Me.txtEggsSet.Value = ""
        Me.txtHatch.Value = ""
        
            
    End Sub
    
    
    Private Sub cmdClose_Click()
        'Close UserForm.
        Unload Me
        
    
    
    
    
    End Sub
    Last edited by Fluff; Jul 22nd, 2019 at 09:23 AM. Reason: Added code tags

  2. #2
    Board Regular
    Join Date
    Apr 2010
    Posts
    167
    Post Thanks / Like
    Mentioned
    6 Post(s)
    Tagged
    0 Thread(s)

    Default Re: VBA USerform error

    Code:
    Private Sub UserForm_Click()
    Begin EggProdData
    End Sub

    This will cause an error for sure. I am not familiar with the 'Begin' function of VBA. What was your intended result with that line?
    Last edited by Steve_; Jul 22nd, 2019 at 09:37 AM.

  3. #3
    Board Regular My Aswer Is This's Avatar
    Join Date
    Jul 2014
    Posts
    15,834
    Post Thanks / Like
    Mentioned
    28 Post(s)
    Tagged
    13 Thread(s)

    Default Re: VBA USerform error

    I would think it would be this code:
    Every time you click on the Userform this script runs:

    Code:
    Private Sub UserForm_Click() Begin EggProdData Me.txtDay.Value = Format(Now(), "MM/DD/YYYY") Caption = "Data Entry" ClientHeight = 6360 ClientLeft = 45 ClientTop = 375 ClientWidth = 7770 OleObjectBlob = "EggProdData.frx": StartUpPosition = 1 'CenterOwner End Sub
    Be sure and always test this script on sample data the first time you use this as to avoid any problems with your data. Always trying to learn more and help others where I can. I'm using Excel 2013.
    Patience please Iím not perfect yet. "Memories are forever"

  4. #4
    New Member
    Join Date
    Jul 2019
    Posts
    5
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Default Re: VBA USerform error

    Quote Originally Posted by Steve_ View Post
    Code:
    Private Sub UserForm_Click()
    Begin EggProdData
    End Sub

    This will cause an error for sure. I am not familiar with the 'Begin' function of VBA. What was your intended result with that line?
    To be honest I didnt understand that either, I copied that block of code from a website as I was learning to create the userform, then I just simply changed the name. But, you were exactly right, I removed that line and everything works perfectly! Thank you so much, as you can tell, I am new to the vba world, so I truly do appreciate you taking the time to help on this!

  5. #5
    New Member
    Join Date
    Jul 2019
    Posts
    5
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Default Re: VBA USerform error

    Quote Originally Posted by My Aswer Is This View Post
    I would think it would be this code:
    Every time you click on the Userform this script runs:

    Code:
    Private Sub UserForm_Click() Begin EggProdData Me.txtDay.Value = Format(Now(), "MM/DD/YYYY") Caption = "Data Entry" ClientHeight = 6360 ClientLeft = 45 ClientTop = 375 ClientWidth = 7770 OleObjectBlob = "EggProdData.frx": StartUpPosition = 1 'CenterOwner End Sub
    Thank you, it was the "Begin" statement, my inexperience Thank you all for your help!

  6. #6
    Board Regular
    Join Date
    Apr 2010
    Posts
    167
    Post Thanks / Like
    Mentioned
    6 Post(s)
    Tagged
    0 Thread(s)

    Default Re: VBA USerform error

    Quote Originally Posted by JoeyGaspard View Post
    To be honest I didnt understand that either, I copied that block of code from a website as I was learning to create the userform, then I just simply changed the name. But, you were exactly right, I removed that line and everything works perfectly! Thank you so much, as you can tell, I am new to the vba world, so I truly do appreciate you taking the time to help on this!

    This is purely a guess, but my first thought is that there is a code routine somewhere else in the code named "EggProdData" and this line was meant to call it. But I am speculating.

  7. #7
    New Member
    Join Date
    Jul 2019
    Posts
    5
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Default Re: VBA USerform error

    Quote Originally Posted by Steve_ View Post
    This is purely a guess, but my first thought is that there is a code routine somewhere else in the code named "EggProdData" and this line was meant to call it. But I am speculating.
    I do have one other quick question, I have a command button with a macro to clear the contents of the multiple worksheets, the code is below, is it possible to ask the user to confirm before it actually clears the contents? Maybe jsut something simple like when they click the button, excel pops up "Are you Sure?" or something like that?

    Sub stone()
    Dim a As Long
    For a = 1 To Sheets.Count
    Worksheets(a).Range("A2:Z2045").ClearContents
    Next a


    End Sub

  8. #8
    Board Regular sykes's Avatar
    Join Date
    May 2002
    Location
    Cornwall,England
    Posts
    1,560
    Post Thanks / Like
    Mentioned
    4 Post(s)
    Tagged
    0 Thread(s)

    Default Re: VBA USerform error

    Code:
    Sub stone()
    Dim a As Long
    If MsgBox("Are you really sure you want to clear ALL of the contents from ALL of the sheets?", 20) = vbNo Then Exit Sub
    
    For a = 1 To Sheets.Count
    Worksheets(a).Range("A2:Z2045").ClearContents
    Next a
    
    
    End Sub
    Sykes
    Windows 10 / XL 2016
    Members don't have crystal balls, so PLEASE describe your situation, code,
    and requirements ACCURATELY in your first post. It saves hair!
    Better still - upload your spreadsheet (and formulae!) with this great bit of kit.


    - forum use guidelines, forum rules and terms of use
    - Try searching for your answer first, see how
    - Read the FAQs
    - List of BB codes

  9. #9
    New Member
    Join Date
    Jul 2019
    Posts
    5
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Default Re: VBA USerform error

    Quote Originally Posted by sykes View Post
    Code:
    Sub stone()
    Dim a As Long
    If MsgBox("Are you really sure you want to clear ALL of the contents from ALL of the sheets?", 20) = vbNo Then Exit Sub
    
    For a = 1 To Sheets.Count
    Worksheets(a).Range("A2:Z2045").ClearContents
    Next a
    
    
    End Sub
    I figured this one out, thanks All!!!

Some videos you may like

User Tag List

Tags for this Thread

Like this thread? Share it with others

Like this thread? Share it with others

Posting Permissions

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