Solved

Save as txt vba

Posted on 2014-03-23
5
1,833 Views
Last Modified: 2014-03-23
Hello,
can you please help,
Im using below code to save range on sheet as .txt
I need to name the saved text as cell value which is not included in the range.
range to save is "A1:A1000"
Save range as text with name from Cell "B1"

Sub Solo_SAVE_Range_txt()
    Sheets("Solo").Select
    Range("A1:A1000").Copy
    Sheets.Add.Range("A1").PasteSpecial xlPasteAll
    Application.CutCopyMode = False
    ActiveSheet.UsedRange.EntireColumn.AutoFit
    ActiveSheet.Move
    ActiveWorkbook.SaveAs Filename:="C:\Users\Wassim\Desktop\Reports\" & Range("B1").Value & ".txt", FileFormat:=xlTextPrinter, CreateBackup:=False
    ActiveWorkbook.Close False
End Sub

Open in new window


Thank you fro any help.
0
Comment
Question by:W.E.B
  • 2
  • 2
5 Comments
 
LVL 22

Accepted Solution

by:
rspahitz earned 300 total points
ID: 39948979
I'm not quite sure of the question, but if all you want to do is save some cells as text, you could do that directly as follows (rather than using the saveAs, which saves the entire sheet):
Sub Solo_SAVE_Range_txt()
    Sheets("Solo").Select
    Dim FileName as String
    Dim FileNumber as Integer

   Filename="C:\Users\Wassim\Desktop\Reports\" & Range("B1").Value & ".txt"
   FileNumber = FreeFile()
   Open FileName for Output as #FileNumber 
   For row=1 to 1000
      Print #FileNumber, cells(row,1).Value
   Next
   Close #FileNumber 
End Sub

Open in new window

0
 

Author Comment

by:W.E.B
ID: 39948991
Hello,
Correct, I don't want to save the entire sheet,
only range A1:A1000

save as cell "B1" value    (example: cell B1 Value = Solo (03-21-2014 _ 03-21-2014))

I get error when I run your code

Compile error
variable not defined.

For row = 1 To 1000
Thanks,
0
 

Author Closing Comment

by:W.E.B
ID: 39949040
thank you
0
 
LVL 33

Expert Comment

by:Norie
ID: 39949054
Perhaps if you get the filename before you copy the data to a new workbook.
Sub Solo_SAVE_Range_txt()
Dim strFileName As String

    With Sheets("Solo").Select
        strFileName = .Range("B1").Value & ".txt"
        .Range("A1:A1000").Copy
    End With

    Sheets.Add.Range("A1").PasteSpecial xlPasteAll

    Application.CutCopyMode = False

    ActiveSheet.UsedRange.EntireColumn.AutoFit

    ActiveSheet.Move

    ActiveWorkbook.SaveAs Filename:="C:\Users\Wassim\Desktop\Reports\" & strFileName, FileFormat:=xlTextPrinter, CreateBackup:=False

    ActiveWorkbook.Close False

End Sub 

Open in new window

0
 
LVL 22

Expert Comment

by:rspahitz
ID: 39949153
For the error, try adding this near the top:

Dim row as Integer
Sub Solo_SAVE_Range_txt()
    Sheets("Solo").Select
    Dim FileName As String
    Dim FileNumber As Integer
    Dim row As Integer

   FileName = "C:\Users\Wassim\Desktop\Reports\" & Range("B1").Value & ".txt"
   FileNumber = FreeFile()
   Open FileName For Output As #FileNumber
   For row = 1 To 1000
      Print #FileNumber, Cells(row, 1).Value
   Next
   Close #FileNumber
End Sub

Open in new window

0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Not long ago I saw a question in the VB Script forum that I thought would not take much time. You can read that question (Question ID  (http://www.experts-exchange.com/Programming/Languages/Visual_Basic/VB_Script/Q_28455246.html)28455246) Here (http…
This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
The viewer will learn how to use a discrete random variable to simulate the return on an investment over a period of years, create a Monte Carlo simulation using the discrete random variable, and create a graph to represent the possible returns over…
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…

821 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