Solved

Save as txt vba

Posted on 2014-03-23
5
1,876 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
[X]
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
  • 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 34

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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

This article will guide you to convert a grid from a picture into Excel format using Microsoft OneNote and no other 3rd party application.
Excel can be a tricky bit of software to get your head around. Whilst you’ll be able to eventually get to grips with the basic understanding of how to get by, there are a few Excel tips that not everybody will even know about let alone know how to d…
The viewer will learn how to create a normally distributed random variable in Excel, use a normal distribution to simulate the return on an investment over a period of years, Create a Monte Carlo simulation using a normal random variable, and calcul…
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…

739 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