Object Required Error

  • Thread starter Thread starter Steph
  • Start date Start date
S

Steph

Hello. Anyone see a problem with the below code? A button calls the
Userform. The enter button on the user form calls the Private sub, and if
the if statement is validated, the private calls the Sub Write_Data. Is the
problem with variables across sub modules?

If UserForm1.Caption = "New Project" Then

With Worksheets("Data")
Set newRow = .Cells(Rows.Count, "F").End(xlUp).Offset(1, 5)
End With
Write_Data
Else

Sub Write_Data()
newRow.Offset(0, 4).Value = NewBase.TextBox1.Text
End Sub
 
the variable/object NewBase is not defined in Write_Data, so unless it is a
global variable or the name of a userform, it would be undefined.
the easiest way to check your variable visbility is to put Option Explict at
the top of all your modules and then compile the project.
 
Hi Tom,

Sorry, missed that typo. Should have read:

If NewBase.Caption = "New Project" Then
With Worksheets("Data")
Set newRow = .Cells(Rows.Count, "F").End(xlUp).Offset(1, 5)
End With
Write_Data
Else

Sub Write_Data()
newRow.Offset(0, 4).Value = NewBase.TextBox1.Text
End Sub

Where NewBase is the name of the form. I tried to compile after option
explicit, but got an out of memory error?
 
Is suspect Write_Data can not see the variable NewRow (unless it is global
to the module).

Dim newRow as Range
If NewBase.Caption = "New Project" Then
With Worksheets("Data")
Set newRow = .Cells(Rows.Count, "F").End(xlUp).Offset(1, 5)
End With
Write_Data newRow
Else

Sub Write_Data(Cell as Range)
cell.Offset(0, 4).Value = NewBase.TextBox1.Text
End Sub
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top