View Single Post
  #3   Report Post  
Posted to microsoft.public.excel.programming
RyanH RyanH is offline
external usenet poster
 
Posts: 586
Default Entering value into cell via VB code if cell is empty

This should work for you. This code finds the last row that has a value in
it in Sheet1 in Col. A. It then applies the time to all blank cells.

Sub TimeInCell()

Dim LastRow As Long
Dim myRange As Range
Dim cell As Range

'finds last row in Sheet1 Col. A
LastRow = Sheets("Sheet1").Cells(Rows.Count, 1).End(xlUp).Row

'sets the range to apply times
Set myRange = Sheets("Sheet1").Range("A2:A" & LastRow)

'applies time to all empty cells
For Each cell In myRange
If IsEmpty(cell) Then cell = Format(Now, "hh:mm AM/PM")
Next cell

End Sub

Hope this helps! If so click "yes" below.

--
Cheers,
Ryan


"taco" wrote:

Hi there everyone;

Again thanks a lot in advance for the time and help. This time the question
is;
I'm trying to write a code which should check the cell if it's empty or not.
If it's empty, code should write the current time into cell. If the cell is
not empty, code should go to the other cell to check if that one is empty or
not.

Here is my unsuccessful code;

________________________________________
Function zaman()
ActiveCell = Time()
ActiveCell.NumberFormat = "hh:mm"
End Function
__________________________________________

Sub ar1open()

Range("D9").Select
If D9 = "" Then
D9 = zaman
Else
Range("D10").Select
If D10 = "" Then
D10 = zaman
End If
End If

End Sub

Best Regards.