|
-
Jul 24th, 2009, 10:04 AM
#1
Thread Starter
Fanatic Member
[RESOLVED] Date in Excel
Hi
I tried to use code below, but when the data array is date , no work fine
Code:
Dim lMyArray(2, 1) As Longl
MyArray(0, 0) = format(now,"dd/mm/yyyy")
MyArray(0, 1) = format(now +1,"dd/mm/yyyy")
MyArray(1, 0) = format(now +2,"dd/mm/yyyy")
MyArray(1, 1) = format(now +3"dd/mm/yyyy")
MyArray(2, 0) = format(now +4,"dd/mm/yyyy")
MyArray(2, 1) = format(now +5,"dd/mm/yyyy")
XLSheet.Range("D7:E9").Value = lMyArray
In Excel date show all column as "0/1/1900"
Why ?
I see code in http://www.vbforums.com/showthread.php?t=391665 for other data
work fine
-
Jul 24th, 2009, 10:13 AM
#2
Re: Date in Excel
Try this
vb Code:
Sub Sample()
Dim MyArray(2, 1) As Date
MyArray(0, 0) = Format(Now, "dd/mm/yyyy")
MyArray(0, 1) = Format(Now + 1, "dd/mm/yyyy")
MyArray(1, 0) = Format(Now + 2, "dd/mm/yyyy")
MyArray(1, 1) = Format(Now + 3, "dd/mm/yyyy")
MyArray(2, 0) = Format(Now + 4, "dd/mm/yyyy")
MyArray(2, 1) = Format(Now + 5, "dd/mm/yyyy")
XLSheet.Range("D7:E9").Value = MyArray
XLSheet.Range("D7:E9").NumberFormat = "dd/mm/yyyy"
End Sub
A good exercise for the Heart is to bend down and help another up...
Please Mark your Thread " Resolved", if the query is solved
MyGear:
★ CPU ★ Ryzen 5 5800X
★ GPU ★ NVIDIA GeForce RTX 3080 TI Founder Edition
★ RAM ★ G. Skill Trident Z RGB 32GB 3600MHz
★ MB ★ ASUS TUF GAMING X570 (WI-FI) ATX Gaming
★ Storage ★ SSD SB-ROCKET-1TB + SEAGATE 2TB Barracuda IHD
★ Cooling ★ NOCTUA NH-D15 CHROMAX BLACK 140mm + 10 of Noctua NF-F12 PWM
★ PSU ★ ANTEC HCG-1000-EXTREME 1000 Watt 80 Plus Gold Fully Modular PSU
★ Case ★ LIAN LI PC-O11 DYNAMIC XL ROG (BLACK) (G99.O11DXL-X)
★ Monitor ★ LG Ultragear 27" 240Hz Gaming Monitor
★ Keyboard ★ TVS Electronics Gold Keyboard
★ Mouse ★ Logitech G502 Hero
-
Jul 24th, 2009, 10:16 AM
#3
Re: Date in Excel
formatted dates are strings not longs
myarray is not lmyarray
you may need to format the workshee cells for that range
i tested this appeared correct
vb Code:
Dim lMyArray(2, 1) As Variant lMyArray(0, 0) = Format(Now, "dd/mm/yyyy") lMyArray(0, 1) = Format(Now + 1, "dd/mm/yyyy") lMyArray(1, 0) = Format(Now + 2, "dd/mm/yyyy") lMyArray(1, 1) = Format(Now + 3, "dd/mm/yyyy") lMyArray(2, 0) = Format(Now + 4, "dd/mm/yyyy") lMyArray(2, 1) = Format(Now + 5, "dd/mm/yyyy") Range("D7:E9").Value = lMyArray
i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next
dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part
come back and mark your original post as resolved if your problem is fixed
pete
-
Jul 24th, 2009, 10:40 AM
#4
Thread Starter
Fanatic Member
Re: Date in Excel
sorry, in my code are Date
Code:
Dim lMyArray(2, 1) As date
-
Jul 24th, 2009, 10:44 AM
#5
-
Jul 24th, 2009, 12:47 PM
#6
Thread Starter
Fanatic Member
Re: Date in Excel
Thank, but the problem is not resolved
-
Jul 24th, 2009, 02:06 PM
#7
Re: Date in Excel
If they are supposed to be Dates then put dates into them, not Strings (which is what Format returns). eg:
Code:
Dim lMyArray(2, 1) As Date
lMyArray(0, 0) = now
lMyArray(0, 1) = now +1
lMyArray(1, 0) = now +2
lMyArray(1, 1) = now +3
lMyArray(2, 0) = now +4
lMyArray(2, 1) = now +5
XLSheet.Range("D7:E9").Value = lMyArray
This will ensure that the correct values are passed, but they might not display properly - if that is the case, change the format of the cells in Excel (and record a macro so that you can see the code to do it).
Last edited by si_the_geek; Jul 25th, 2009 at 06:13 AM.
Reason: fixed mistake in copy+paste!
-
Jul 24th, 2009, 08:47 PM
#8
Re: Date in Excel
si copied the original code
turn option explicit on, it may resolve some of your problems
i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next
dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part
come back and mark your original post as resolved if your problem is fixed
pete
-
Jul 25th, 2009, 06:14 AM
#9
Re: Date in Excel
oops, fixed. 
Here's an FAQ article on what westconn1 mentioned: What is Option Explicit, and why should I use it?
-
Jul 27th, 2009, 05:42 AM
#10
Thread Starter
Fanatic Member
-
Jul 27th, 2009, 06:32 AM
#11
Thread Starter
Fanatic Member
Re: Date in Excel
 Originally Posted by si_the_geek
If they are supposed to be Dates then put dates into them, not Strings (which is what Format returns). eg:
Code:
Dim lMyArray(2, 1) As Date
lMyArray(0, 0) = now
lMyArray(0, 1) = now +1
lMyArray(1, 0) = now +2
lMyArray(1, 1) = now +3
lMyArray(2, 0) = now +4
lMyArray(2, 1) = now +5
XLSheet.Range("D7:E9").Value = lMyArray
This will ensure that the correct values are passed, but they might not display properly - if that is the case, change the format of the cells in Excel (and record a macro so that you can see the code to do it).
Thank , but I tried from VB and inside Excel the code below, and no work
Code:
Sub insere_datas()
Dim Dt_Expedicao() As Date
Dim i As Integer
ReDim Dt_Expedicao(0)
Do While i < 9
ReDim Preserve Dt_Expedicao(UBound(Dt_Expedicao) + 1)
Dt_Expedicao(UBound(Dt_Expedicao)) = Now() 'CStr(rs!data_expedicao)
i = i + 1
Loop
' Range("AF4:AF12").Select
Range("AF4:AF12").NumberFormat = "@"
Range("AF4:AF12").Value = Dt_Expedicao
Range("AF4:AF12").NumberFormat = "dd/mm/yyyy"
Range("AF4:AF12").HorizontalAlignment = xlCenter
End Sub
Show me as 00/01/1900

-
Jul 27th, 2009, 06:57 AM
#12
Re: Date in Excel
That is because:
1) You are using a one-dimensional array (which goes across the sheet, and gets copied downwards if apt) rather than a two-dimensional array (which goes across and down).
2) You do not set a value for the first array element, so it is the default value.
-
Jul 27th, 2009, 07:10 AM
#13
Thread Starter
Fanatic Member
Re: Date in Excel
Sorry, I did not understand
-
Jul 27th, 2009, 07:20 AM
#14
Thread Starter
Fanatic Member
Re: Date in Excel
Thank , eg when I tried code below It put only same date
vb Code:
Dim lMyArray(2) As Date lMyArray(0) = Now lMyArray(1) = Now + 1 lMyArray(2) = Now + 2 Plan1.Range("D7:D9").Value = lMyArray
-
Jul 27th, 2009, 07:36 AM
#15
Re: Date in Excel
You are using a one dimensional array:
Code:
Dim lMyArray(2) As Date
..it needs to be a two-dimensional array (as it was in post #7 and earlier):
Code:
Dim lMyArray(2, 1) As Date
-
Jul 27th, 2009, 07:59 AM
#16
Thread Starter
Fanatic Member
Re: Date in Excel
Thank You
But I must put only a column , in Left column and right column already data and can not to changed
tia
-
Jul 27th, 2009, 08:09 AM
#17
Re: Date in Excel
So?
There is nothing to stop you using a 2-dimensional array with just one column (eg: Dim lMyArray(2, 0) As Date )
-
Jul 27th, 2009, 08:16 AM
#18
Re: Date in Excel
you can use a 1 dimensional array like
vb Code:
Dim lMyArray(2) As Date lMyArray(0) = Now lMyArray(1) = Now + 1 lMyArray(2) = Now + 2 Range("D7:D9").Value = Application.WorksheetFunction.Transpose(lMyArray) Range("d7:d9").NumberFormat = "dd/mm/yy"
the transpose function swaps row and columns
i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next
dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part
come back and mark your original post as resolved if your problem is fixed
pete
-
Jul 27th, 2009, 08:45 AM
#19
Thread Starter
Fanatic Member
Re: Date in Excel
I tried to use your code , but put values (dates ) inconsistent
Code:
Dim lMyArray(3) As Variant
'ReDim Preserve lMyArray(0, 2) As Variant
lMyArray(0) = Now
lMyArray(1) = Now + 1
lMyArray(2) = Now + 2
Plan1.Range("D7:D9").Value = Application.WorksheetFunction.Transpose(Dt_Expedicao)
Plan1.Range("d7:d9").NumberFormat = "dd/mm/yy"
Show me
04/08/09
00/01/00
00/01/00
-
Jul 27th, 2009, 08:53 AM
#20
Re: Date in Excel
what is dt_expedicao
it is suppossed to be the array (lMyarray) with the dates that is transposed
i tested and works correctly
what is your local date format?
you could try changing to
Plan1.Range("d7:d9").NumberFormatLocal = "dd/mm/yy"
i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next
dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part
come back and mark your original post as resolved if your problem is fixed
pete
-
Jul 27th, 2009, 08:54 AM
#21
Thread Starter
Fanatic Member
Re: Date in Excel
 Originally Posted by si_the_geek
So?
There is nothing to stop you using a 2-dimensional array with just one column (eg: Dim lMyArray(2, 0) As Date )
Thank
Your Idea work fine , But I am using a loop with recordset and need resize array for each record
Code:
Do While Not rs.EOF
'Acrescenta uma linha no array
Dt_Expedicao(UBound(Dt_Expedicao)) = Now() 'CStr(rs!data_expedicao)]
ReDim Preserve Dt_Expedicao(UBound(Dt_Expedicao) + 1) As Date
loop
If I need put in two dimension I can to use Redim Preserve in second dimension
-
Jul 27th, 2009, 09:43 AM
#22
Re: Date in Excel
As it is coming from a recordset (assuming data_expedicao is the only field in it), there is no need to use an array or any loops - you can simply use CopyFromRecordset instead:
Code:
Plan1.Range("D7").CopyFromRecordset rs
-
Jul 27th, 2009, 09:50 AM
#23
Thread Starter
Fanatic Member
Re: Date in Excel
Sorry, variable error, Your solution woked fine !!!!
How can I to post like resolved ?
-
Jul 27th, 2009, 09:51 AM
#24
Re: Date in Excel
You can do it by clicking on "Thread tools" just above the first post in this thread, then "Mark thread resolved". (like various other features of this site, you need JavaScript enabled in your browser for this to work).
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|