Create Excel formatted text file with generated text as file name in a macro

Posted on 2003-03-18
Medium Priority
Last Modified: 2010-05-01
I have >100 excel files to save as .txt files and this field changes in each one so I'm trying to create a macro to do it and use the field in the file name...

This is an example of the data I have:

A1 = 01234
in A3 is =CONCATENATE(A1,A2) giving 01234ABCDE
I want to be able to save this file as 01234ABCDE.txt (text file TAB delimited)
where 01234 is different every time.

I tried to copy the concatenated field to a text field using paste special:
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False

and then tried copying the value to the save box but it gave me the same value again and again.
ActiveWorkbook.SaveAs Filename:="c:\01234ABCDE.txt", FileFormat:=xlText, CreateBackup:=False
I want it to look at A3.

Stuck and hope you can help please :-/

Question by:Mellissa
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
LVL 43

Accepted Solution

TimCottee earned 300 total points
ID: 8158754
Hi Mellissa,

Use ActiveWorkbook.SaveAs FileName:=ActiveSheet.Range("A3"), FileFormat:=xlText, CreateBackup:=False


Brainbench MVP for Visual Basic

Experts-Exchange Advisory Board Member

Author Comment

ID: 8158866
Thank you,
Tried, tested and works a treat

It always looks easy when someone else shows you how.


Expert Comment

ID: 8158873

Sub SaveFile()
 Path = "c:\"
 strfilename = Path & Range("A3").Value & ".txt"
 While Dir(strfilename) <> ""
  Number = Range("A1").Value + 1
  Number = Format(Number, "00000")
  Range("A1").Value = "'" & CStr(Number)
  strfilename = Path & Range("A3").Value & ".txt"
  ActiveWorkbook.SaveAs Filename:=strfilename, FileFormat:=xlText, _
End Sub

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…
Suggested Courses

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question