Export of excel to csv

wagdownunder

New Member
Joined
Nov 19, 2010
Messages
8
Hi

I have downloaded this macro to export data from excel to a csv file. Everything works fine except when the date and time is exported. It seems that the date and time formats of excel is ignored are there a way of incoporating it in this macro?

any feedback would be appreciated

Daniel

Sub csvfile()
Dim fs As Object, a As Object, i As Integer, s As String, t As String, l As String, mn As String
Set fs = CreateObject("Scripting.FileSystemObject")
Set a = fs.CreateTextFile("c:\Quanta Data_" & Format(Now, "YYYYMMDD_HHMMSS") & ".csv", True)
For r = 26 To Range("A65536").End(xlUp).Row
s = ""
c = 1
While Not IsEmpty(Cells(r, c))
s = s & Cells(r, c) & ","
c = c + 1
Wend
a.writeline s 'write line
Next r

End Sub
 

Excel Facts

Copy formula down without changing references
If you have =SUM(F2:F49) in F50; type Alt+' in F51 to copy =SUM(F2:F49) to F51, leaving the formula in edit mode. Change SUM to COUNT.
try this:

Code:
Sub csvfile()
    Dim fs As Object, a As Object, i As Integer, s As String, t As String, l As String
    dim mn As String, Temp as string
    Set fs = CreateObject("Scripting.FileSystemObject")
    temp="c:\Quanta Data_" & Format(Now, "YYYYMMDD_HHMMSS") & ".csv"
    Set a = fs.CreateTextFile(Temp , True)
    For r = 26 To Range("A65536").End(xlUp).Row
        s = ""
        c = 1
        While Not IsEmpty(Cells(r, c))
            s = s & Cells(r, c) & ","
            c = c + 1
        Wend
        a.writeline s 'write line
    Next r
End Sub
 
Upvote 0
Hi

Thanks for the quick reply. However it does not seem to resolve the problem. Column 4 and 5 contains the date and time in the excel spreadsheet with the following formats.

dd/mm/yyyy
hh:mm

The date format in the CSV file is the same, but the time format has been changed to a value, see example.

13:34 (xls) 0.565277778 (csv)
 
Upvote 0
Code:
Sub csvfile()
    Dim fs As Object, a As Object, i As Integer, s As String, t As String, l As String
    dim mn As String, Temp as string
    Set fs = CreateObject("Scripting.FileSystemObject")
    temp="c:\Quanta Data_" & Format(Now, "YYYYMMDD_HHMMSS") & ".csv"
    Set a = fs.CreateTextFile(Temp , True)
    For r = 26 To Range("A65536").End(xlUp).Row
        s = ""
        c = 1
        While Not IsEmpty(Cells(r, c))
            if c=4 then s = s & format(Cells(r, c),"dd/mm/yyyy") & ","
            elseif c=5 then s = s & format(Cells(r, c),"hh:mm") & ","
            else
                s = s & Cells(r, c) & ","
            end if
            c = c + 1
        Wend
        a.writeline s 'write line
    Next r
End Sub
 
Upvote 0
try this:
Code:
Sub csvfile()
    Dim fs As Object, a As Object, i As Integer, s As String, t As String, l As String
    Dim mn As String, Temp As String
    Set fs = CreateObject("Scripting.FileSystemObject")
    Temp = "c:\Quanta Data_" & Format(Now, "YYYYMMDD_HHMMSS") & ".csv"
    Set a = fs.CreateTextFile(Temp, True)
    For r = 26 To Range("A65536").End(xlUp).Row
        s = ""
        c = 1
        While Not IsEmpty(Cells(r, c))
            If c = 4 Then
                s = s & Format(Cells(r, c), "dd/mm/yyyy") & ","
            ElseIf c = 5 Then
                s = s & Format(Cells(r, c), "hh:mm") & ","
            Else
                s = s & Cells(r, c) & ","
            End If
            c = c + 1
        Wend
        a.writeline s 'write line
    Next r
End Sub
 
Upvote 0

Forum statistics

Threads
1,223,246
Messages
6,170,988
Members
452,373
Latest member
TimReeks

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top