VBA Form coding issue

emers21

New Member
Joined
Feb 8, 2017
Messages
10
Hi Guys,

I have created a form in excel using VBA. The information completed is to be loaded in to a mastersheet via a command button. I have written the code, which ?I have copied here but have an issue with the part in blue bold

Private Sub Cmdbutton_data_Click()
Dim iRow As Long
Dim ws As Worksheet
Set ws = Worksheets("Mastersheet")
'find first empty row in database
iRow = ws.Cells.Find(What:="*", SearchOrder:=xlRows, SearchDirection:=xlPrevious, LookIn:=xlValues).Row + 1


'check for a Name number
If Trim(Me.frm_pupil.Value) = "" Then
Me.frm_pupil.SetFocus
MsgBox "Please complete the form"
Exit Sub
End If
'copy the data to the database
ws.Cells(iRow, 1).Value = Me.frm_pupil.Value
ws.Cells(iRow, 2).Value = Me.frm_time.Value
ws.Cells(iRow, 3).Value = Me.frm_staff.Value
ws.Cells(iRow, 4).Value = Me.frm_date.Value
ws.Cells(iRow, 5).Value = Me.frm_intreason.Value
ws.Cells(iRow, 6).Value = Me.frm_eventsprior.Value
ws.Cells(iRow, 7).Value = Me.frm_behaviour.Value
ws.Cells(iRow, 8).Value = Me.frm_routine.Value
ws.Cells(iRow, 9).Value = Me.frm_risk.Value
ws.Cells(iRow, 10).Value = Me.frm_bestaction.Value
ws.Cells(iRow, 11).Value = Me.frm_restrainttype.Value
ws.Cells(iRow, 12).Value = Me.frm_bestaction.Value
ws.Cells(iRow, 13).Value = Me.frm_post.Value
MsgBox "Data added", vbOKOnly + vbInformation, "Data Added"
'clear the data
Me.frm_pupil.Value = ""
Me.frm_time.Value = ""
Me.frm_staff.Value = ""
Me.frm_date.Value = ""
Me.frm_intreason.Value = ""
Me.frm_eventsprior.Value = ""
Me.frm_behaviour.Value = ""
Me.frm_routine.Value = ""
Me.frm_risk.Value = ""
Me.frm_bestaction.Value = ""
Me.frm_restrainttype.Value = ""
Me.frm_bestaction.Value = ""
Me.frm_post.Value = ""
Me.frm_pupil.SetFocus
End Sub
Private Sub cmdbutton_cancel_Click()
Unload Me
End Sub

Private Sub frm_pupil_Change()
End Sub
Private Sub UserForm_Click()
End Sub


Can anyone see if I'm missing anything?

Thanks

John
 

dmt32

Well-known Member
Joined
Jul 3, 2012
Messages
5,275
Office Version
2013
Platform
Windows
Hi,
welcome to forum.
You have not said what error you are getting but I guess it's "object or with block" error 91? If so, try adding some data to your worksheet manually & see if this resolves issue for you.

Dave
 

emers21

New Member
Joined
Feb 8, 2017
Messages
10
Hi,
welcome to forum.
You have not said what error you are getting but I guess it's "object or with block" error 91? If so, try adding some data to your worksheet manually & see if this resolves issue for you.

Dave
Hi Dave,

Schoolboy error by me, sometimes the most obvious answer is staring you in the face! Cheers for your help mate much appreciated! Too many late nights and not enough caffeine!

John
 

dmt32

Well-known Member
Joined
Jul 3, 2012
Messages
5,275
Office Version
2013
Platform
Windows
Hi Dave,

Schoolboy error by me, sometimes the most obvious answer is staring you in the face! Cheers for your help mate much appreciated! Too many late nights and not enough caffeine!

John
No worries I do much dafter things in my old age!
- you could wrap that line of code within an on error trap to prevent reoccurrence.

Dave
 

Forum statistics

Threads
1,082,139
Messages
5,363,362
Members
400,730
Latest member
cookie123

Some videos you may like

This Week's Hot Topics

  • populate from drop list with multiple tables
    Hi All, i have a drop list that displays data, what i want is when i select one of those from the list to populate text from different tables on...
  • Find list of words from sheet2 in sheet1 before a comma and extract text vba
    Hi Friends, Trying to find the solution on my task. But did not find suitable one to the need. Here is my query and sample file with details...
  • Dynamic Formula entry - VBA code sought
    Hello, really hope one of you experts can help with this - i've spent hours on this and getting no-where. .I have a set of data (more rows than...
  • Listbox Header
    Have a named range called "AccidentsHeader" Within my code I have: [CODE]Private Sub CommandButton1_Click() ListBox1.RowSource =...
  • Complex Heat Map using conditional formatting
    Good day excel world. I have a concern. Below link have a list of countries that carries each country unique data. [URL...
  • Conditional formatting
    Hi good morning, hope you can help me please, I have cells P4:P54 and if this cell is equal to 1 then i want row O to say "Fully Utilised" and to...
Top