Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel,microsoft.public.excel.misc
external usenet poster
 
Posts: 107
Default Adding date to filename?

Hello

Please could I beg a little advice from the experts here

I'm using the following code to export data without quotation marks to text
files.

Sub Export()
Dim r As Range, c As Range
Dim sTemp As String
Open "MyOutput.txt" For Output As #1
For Each r In Selection.Rows
sTemp = ""
For Each c In r.Cells
sTemp = sTemp & c.Text & Chr(9)
Next c

'Get rid of trailing tabs
While Right(sTemp, 1) = Chr(9)
sTemp = Left(sTemp, Len(sTemp) - 1)
Wend
Print #1, sTemp
Next r
Close #1
End Sub

It works fine so long as the required cells are selected, but does anyone
know how to amend the VBA to add the time and date to the filename? I'd be
soooo grateful.

I tried

Open "MyOutput" & Format(Date, "yyyymmdd") & ".txt" For Output As #1

but that just crashed it. Any ideas?
Many thanks
tp


  #2   Report Post  
Posted to microsoft.public.excel,microsoft.public.excel.misc
external usenet poster
 
Posts: 1
Default Adding date to filename?

Open "MyOutput" & Format(Date, "yyyymmdd") & ".txt" For Output As #1

I don't see anything wrong with that line (well, except that you hard-coded
the #1 instead of using the FreeFile function, but that wouldn't stop the
line from working unless channel #1 were still open from a previous call to
the Open statement), although I would always include the path with the
filename. You say it crashed... describe the crash in more detail
(especially if there were any error messages generated).

Rick


"teepee" wrote in message
...
Hello

Please could I beg a little advice from the experts here

I'm using the following code to export data without quotation marks to
text files.

Sub Export()
Dim r As Range, c As Range
Dim sTemp As String
Open "MyOutput.txt" For Output As #1
For Each r In Selection.Rows
sTemp = ""
For Each c In r.Cells
sTemp = sTemp & c.Text & Chr(9)
Next c

'Get rid of trailing tabs
While Right(sTemp, 1) = Chr(9)
sTemp = Left(sTemp, Len(sTemp) - 1)
Wend
Print #1, sTemp
Next r
Close #1
End Sub

It works fine so long as the required cells are selected, but does anyone
know how to amend the VBA to add the time and date to the filename? I'd be
soooo grateful.

I tried

Open "MyOutput" & Format(Date, "yyyymmdd") & ".txt" For Output As #1

but that just crashed it. Any ideas?
Many thanks
tp


  #3   Report Post  
Posted to microsoft.public.excel,microsoft.public.excel.misc
external usenet poster
 
Posts: 107
Default Adding date to filename?


"Rick Rothstein (MVP - VB)" wrote

I don't see anything wrong with that line (well, except that you
hard-coded the #1 instead of using the FreeFile function, but that
wouldn't stop the line from working unless channel #1 were still open from
a previous call to the Open statement), although I would always include
the path with the filename. You say it crashed... describe the crash in
more detail (especially if there were any error messages generated).


Wow this is weird. Suddenly it's working for me too? Must have been operator
error. Sorry

However when I try

Open "MyOutput" & Format(Date, "yyyymmdd") & Format(Time, "hhmmss") &
".txt" For Output As #1

I get syntax error


  #4   Report Post  
Posted to microsoft.public.excel,microsoft.public.excel.misc
external usenet poster
 
Posts: 107
Default Adding date to filename?


"teepee" wrote

However when I try

Open "MyOutput" & Format(Date, "yyyymmdd") & Format(Time, "hhmmss") &
".txt" For Output As #1

I get syntax error



Ah got it.

Open "MyOutput" & Format(Date, "dd-mm-yy") + "_" + Format(Time, "hh-mm-ss")
& ".txt" For Output As #1

Thanks for all your help

tp



  #5   Report Post  
Posted to microsoft.public.excel,microsoft.public.excel.misc
external usenet poster
 
Posts: 35,218
Default Adding date to filename?

Or
Open "MyOutput" & Format(now, "dd-mm-yy_hh-mm-ss") & ".txt" For Output As #1

teepee wrote:

"teepee" wrote

However when I try

Open "MyOutput" & Format(Date, "yyyymmdd") & Format(Time, "hhmmss") &
".txt" For Output As #1

I get syntax error


Ah got it.

Open "MyOutput" & Format(Date, "dd-mm-yy") + "_" + Format(Time, "hh-mm-ss")
& ".txt" For Output As #1

Thanks for all your help

tp


--

Dave Peterson


  #6   Report Post  
Posted to microsoft.public.excel.misc,microsoft.public.excel
external usenet poster
 
Posts: 9,101
Default Adding date to filename?

The code works on my PC. give the following filename

MyOutput20080415.txt


"teepee" wrote:

Hello

Please could I beg a little advice from the experts here

I'm using the following code to export data without quotation marks to text
files.

Sub Export()
Dim r As Range, c As Range
Dim sTemp As String
Open "MyOutput.txt" For Output As #1
For Each r In Selection.Rows
sTemp = ""
For Each c In r.Cells
sTemp = sTemp & c.Text & Chr(9)
Next c

'Get rid of trailing tabs
While Right(sTemp, 1) = Chr(9)
sTemp = Left(sTemp, Len(sTemp) - 1)
Wend
Print #1, sTemp
Next r
Close #1
End Sub

It works fine so long as the required cells are selected, but does anyone
know how to amend the VBA to add the time and date to the filename? I'd be
soooo grateful.

I tried

Open "MyOutput" & Format(Date, "yyyymmdd") & ".txt" For Output As #1

but that just crashed it. Any ideas?
Many thanks
tp



Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
getting values from a filename as a date in a cell Me Excel Discussion (Misc queries) 12 October 30th 08 08:45 PM
Adding date for filename Wins07 Excel Discussion (Misc queries) 2 July 31st 07 02:02 PM
Enter current date automatically in filename Megadrone Excel Worksheet Functions 1 January 25th 07 03:48 PM
set filename to <filename-date on open bob engler Excel Worksheet Functions 2 July 13th 06 05:11 AM
How to automate footers (filename/date) for all new spreadsheets? amgbd26 Excel Worksheet Functions 4 August 10th 05 10:01 PM


All times are GMT +1. The time now is 07:31 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"