If Statement Help

  • Thread starter Thread starter Ed
  • Start date Start date
E

Ed

I recorded a macro that inputs the current time. The user will click in the
"Start Time" cell and run the macro, then when the user is finished he will
click in the "End Time" and run the macro again. What I want to do is make
sure the user does not run the macro in a cell that is not blank. I'm afraid
that the user will attempt to run the macro in a cell that already has a
value. I was thinking that an If statement, to determine if the is cell is
not blank would work.

T.I.A.

Ed



Sub Now_1()
'
' Now_1 Macro
' '

'
ActiveCell.FormulaR1C1 = "=NOW()"
Selection.Copy
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone,
SkipBlanks _
:=False, Transpose:=False
Application.CutCopyMode = False
End Sub
 
Ed,

Try something like

If Range("StartTime").Value = "" Then
Range("StartTime").Value = Now
End If


--
Cordially,
Chip Pearson
Microsoft MVP - Excel
Pearson Software Consulting, LLC
www.cpearson.com
 
Sub Now_1()
'
' Now_1 Macro
' '

'
if Isempty(activecell) then
activeCell.Value = now ' vba NOW function
Else
msgbox "The cell already contains a value"
End if

End Sub

You don't need to put in a formula then convert it to a value.

If you want to go that route however:
Sub Now_1()
'
' Now_1 Macro
' '

'
if Isempty(activecell) then
activeCell.Formula = "=now()"
activeCell.Formula = ActiveCell.Value
Else
msgbox "The cell already contains a value"
End if

End Sub
 
Hi Ed,

Sub Now_1()
With ActiveCell
If .Value = "" Then
.Value = Now
Else
MsgBox "Already filled"
End If
End With
End Sub


--

HTH

Bob Phillips
... looking out across Poole Harbour to the Purbecks
(remove nothere from the email address if mailing direct)
 
Back
Top