Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 162
Default Macro to save contents of a named range to a file

In Microsoft Excel 2007, is it possible to create a macro that saves
the contents of a named range to a tab delimited text file ?

For example, the DATA name refers to A1:C2 and contains the following:

A B C
1 Item Qty Cost
2 Apple 1 1

The text file DATA.TXT would contain:

Item Qty Cost
Apple 1 1


Any ideas ?



- Ronald K.
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 621
Default Macro to save contents of a named range to a file

Yes, it is possible.

Turn on the macro recorder and copy the range(by name) to a new workbook then
save as tab delimited *.txt file.


Gord Dibben MS Excel MVP

On Sat, 30 Apr 2011 23:15:58 -0700 (PDT), kittronald
wrote:

In Microsoft Excel 2007, is it possible to create a macro that saves
the contents of a named range to a tab delimited text file ?

For example, the DATA name refers to A1:C2 and contains the following:

A B C
1 Item Qty Cost
2 Apple 1 1

The text file DATA.TXT would contain:

Item Qty Cost
Apple 1 1


Any ideas ?



- Ronald K.

  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 162
Default Macro to save contents of a named range to a file

Gord,

Thanks for the quick reply.

How do you copy a name ?

When I copy a named range, the macro records the cell addresses and
not the name.



- Ronald K.
  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 162
Default Macro to save contents of a named range to a file

Gord,

Figured it out - partly.

Had to use the Goto command to specify the name.

However, when the filename to save to already exists, how do you
specify "Yes" to the overwrite prompt in the macro ?



- Ronald K.
  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 3,514
Default Macro to save contents of a named range to a file

kittronald formulated the question :
Gord,

Figured it out - partly.

Had to use the Goto command to specify the name.

However, when the filename to save to already exists, how do you
specify "Yes" to the overwrite prompt in the macro ?



- Ronald K.


Post the code so we can see what's going on.

--
Garry

Free usenet access at http://www.eternal-september.org
ClassicVB Users Regroup! comp.lang.basic.visual.misc




  #6   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 621
Default Macro to save contents of a named range to a file

Application.DisplayAlerts = False
ActiveWorkbook.SaveAs FileName:="C:\myfolder\" & _
ActiveWorkbook.Name
Application.DisplayAlerts = True


Gord

On Sun, 1 May 2011 19:02:56 -0700 (PDT), kittronald
wrote:

Gord,

Figured it out - partly.

Had to use the Goto command to specify the name.

However, when the filename to save to already exists, how do you
specify "Yes" to the overwrite prompt in the macro ?



- Ronald K.

  #7   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 3,514
Default Macro to save contents of a named range to a file

Gord,
It's interesting how the way a Q is asked makes/breaks how we answer
it. Your reply hits the bullseye (now that I see it) but didn't even
think that because the post implied a prompt being responded to, and so
I thought maybe the OP was looking for a way to determine if the answer
to the prompt was vbYes. IOW, there was nothing to suggest looking for
a way to supress the overwrite alert.

So who's having a 'bad day' now?<g

--
Garry

Free usenet access at http://www.eternal-september.org
ClassicVB Users Regroup! comp.lang.basic.visual.misc


  #8   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 162
Default Macro to save contents of a named range to a file

Gord and Garry,

Initially, if the file I was writing to already existed, the macro
would cause the following dialog message prompt to appear:

"A filename 'C:\Temp\TEST.txt already exists in this location.
Do you want to replace it ?"

After inserting the "Application.DisplayAlerts = False" line, I've
verified the macro successfully overwrites the existing file -
effectively dismissing the overwrite prompt.

Here is the code:

Sub Macro_SaveAs_File()
'
' Macro_SaveAs_File Macro
'

'
Application.Goto Reference:="DATA"
Selection.Copy
Workbooks.Add
ActiveSheet.Paste
Application.CutCopyMode = False
Application.DisplayAlerts = False
ActiveWorkbook.SaveAs Filename:="C:\Temp\TEST.txt",
FileFormat:=xlText, _
CreateBackup:=False
Application.DisplayAlerts = True
End Sub

Thanks for the help Gord.



- Ronald K.
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
2007 Macro to Open File, Delete Contents, Save New File Flintstone[_2_] Excel Discussion (Misc queries) 2 February 1st 10 11:25 PM
Copy & Paste Named Range Contents BJ Excel Programming 8 August 1st 08 07:18 PM
write a macro like the /file copy combine named range in lotus 12 surelystumped Excel Programming 5 September 25th 06 02:28 PM
macro to save named range as text GFN Excel Programming 3 September 13th 06 06:09 AM
How to save file in a macro taking new name from cell contents? LowIQ Excel Discussion (Misc queries) 3 May 31st 06 10:52 PM


All times are GMT +1. The time now is 05:49 PM.

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

About Us

"It's about Microsoft Excel"