View Single Post
  #3   Report Post  
Posted to microsoft.public.excel.programming
zzxxcc zzxxcc is offline
external usenet poster
 
Posts: 27
Default Strange? VBA code returns wrong date format (not a date)

I also get 16 on both types of returned data. (The wrong and the correct).
That's because it is shown as 16 charters and spaces in the cell. Stll if you
go into the formula bar on top of the Excel window you can count 20 charters
and spaces.

Forget about counting. Just lokk at the code. Something is wrong, but I
can't see it. Here is a code that does a different task, but returns a
correct date / time value: (the full code is IRRELEVANT, just a reference):

Option Explicit
Private Sub CommandButton1_Click()
Dim NextCell As Range
With Me
Set NextCell = .Cells(.Rows.Count, "J").End(xlUp).Offset(1, 0)
End With

With NextCell
.Value = Now
.NumberFormat = "dd.mm.yyyy hh:mm"
End With

End Sub
--
Thanks


"Tom Ogilvy" wrote:

if I format a cell (C2) with your formatting option and put =now() in it,
then in another cell use the =len(C2) I get 16 for which would indicate
there is only one space between the date and time which confirms my previous
experience.

--
Regards,
Tom Ogilvy


"zzxxcc" wrote:

The following code returns a value that looks like a date/time, but is not a
date/time. Eksample: The code returns a cell value 28.09.2007 13:58 (as seen
in the formula field). Normally a date formated as "dd.mm.yyyy hh:mm" will
return a value in the formula field like this: 28.09.2007 13:58:00, and it
shows up as 28.09.2007 13:58 in the cell. I noticed that the code returns a
value with one space between the date and the time. A properly formated date
manually typed in has two spaces. When I manually change to two spaces
between the time and the date in the value placed by the code it
automatically changes from 28.09.2007 13:58 to 28.09.2007 13:58:00 in the
formula field. The apperance in the cell is unchanged. The reason I
discovered this was that I am checking for the largest value in two nearby
cells =IF(L21K21;"Overdue";"") and it returned an unlogic responce.

Whats wrong with this code?

Private Sub CommandButton2_Click()
.Value = Now
.NumberFormat = "dd.mm.yyyy hh:mm"
ActiveCell.Value = Format(Now(), "dd.mm.yyyy hh:mm")
End Sub
--
Thanks