VBA USerform error

JoeyGaspard

Board Regular
Joined
Jul 22, 2019
Messages
78
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 a moderator:

Some videos you may like

Excel Facts

Excel Wisdom
Using a mouse in Excel is the work equivalent of wearing a lanyard when you first get to college

Steve_

Board Regular
Joined
Apr 28, 2010
Messages
167
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:

My Aswer Is This

Well-known Member
Joined
Jul 5, 2014
Messages
16,762
Office Version
2013
Platform
Windows
I would think it would be this code:
Every time you click on the Userform this script runs:

Code:
[LEFT][COLOR=#333333][FONT=monospace]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
[/FONT][/COLOR][/LEFT]
 

JoeyGaspard

Board Regular
Joined
Jul 22, 2019
Messages
78
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!
 

JoeyGaspard

Board Regular
Joined
Jul 22, 2019
Messages
78
I would think it would be this code:
Every time you click on the Userform this script runs:

Code:
[LEFT][COLOR=#333333][FONT=monospace]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
[/FONT][/COLOR][/LEFT]
Thank you, it was the "Begin" statement, my inexperience:) Thank you all for your help!
 

Steve_

Board Regular
Joined
Apr 28, 2010
Messages
167
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.
 

JoeyGaspard

Board Regular
Joined
Jul 22, 2019
Messages
78
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
 

sykes

Well-known Member
Joined
May 1, 2002
Messages
1,660
Office Version
365
Platform
Windows
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
 

JoeyGaspard

Board Regular
Joined
Jul 22, 2019
Messages
78
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!!!
 

Watch MrExcel Video

Forum statistics

Threads
1,101,815
Messages
5,483,075
Members
407,377
Latest member
JennaWashburn

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