VBA USerform error

JoeyGaspard

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

Excel Facts

Whats the difference between CONCAT and CONCATENATE?
The newer CONCAT function can reference a range of cells. =CONCATENATE(A1,A2,A3,A4,A5) becomes =CONCAT(A1:A5)
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:
Upvote 0
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]
 
Upvote 0
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!
 
Upvote 0
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!
 
Upvote 0
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.
 
Upvote 0
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
 
Upvote 0
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
 
Upvote 0
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!!!
 
Upvote 0

Forum statistics

Threads
1,223,888
Messages
6,175,217
Members
452,619
Latest member
Shiv1198

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top