View Single Post
  #5   Report Post  
Posted to microsoft.public.excel.programming
rcalvanese rcalvanese is offline
external usenet poster
 
Posts: 9
Default Macro to run if cell empty

Greg,
"On Error Resume Next" is not a good programming practice. Also the code is
over kill for what you want to do. You do not need to declare a bool and
then cast the cells contents into another bool to compare to the declared
bool. You only need to check to see if the cell has something in it.

Put this code into the On_Click event on the Forms CommandButton1. If you
want to validate what is entered on the form like numbers and such... you
will have to add code to do that.

Private Sub CommandButton1_Click()
Worksheets(1).Cells(1, 1).Value = Me.TextBox1.Text
ThisWorkbook.Save
End
End Sub

Add the following code to the Workbook's On_Load event. I have added a
simple error handler as well. You want to "handle erreors" not "ignore
them".

Private Sub Workbook_Open()
On Error GoTo ErrorHandler
If Worksheets(1).Cells(1, 1).Value = "" Then
UserForm1.Show Modal
End If
Exit Sub
ErrorHandler:
MsgBox Err.Description
End Sub

Let me know if this helps.
Bob Calvanese
"Chip Pearson" wrote in message
...
Greg,

Put the following in the ThisWorkbook code module:

Private Sub Workbook_Open()
On Error Resume Next
Dim NameExists As Boolean
If Me.Worksheets("SHeet1").Range("A1").Value = "" Then
NameExists = CBool(Len(ThisWorkbook.Names("RunOnce").Name))
If NameExists = False Then
UserForm1.Show
ThisWorkbook.Names.Add Name:="RunOnce", RefersTo:="Yes"
End If
End If
End Sub


--
Cordially,
Chip Pearson
Microsoft MVP - Excel
Pearson Software Consulting, LLC
www.cpearson.com





"Greg" wrote in message
...
I wanted to run a userform once when this workbook is loaded for the first
time. I needed the user to place some information information in this
form.
Once this userform is used I did not want it to load up again.

Not sure how to do this exactly though maybe it could look up the cell
where
this information would be placed. If empty run a macro and then once the
form has been used it will have the information in the cell so it would
not
be run again.

Thanks again any help is appreciated

Greg