Solved

Save as txt vba

Posted on 2014-03-23
5
1,729 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:Wass_QA
  • 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:Wass_QA
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:Wass_QA
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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

How to quickly and accurately populate Word documents with Excel data, charts and images (including Automated Bookmark generation) David Miller (dlmille) Synopsis In this article you’ll learn how to use ExcelToWord! to copy data,charts, shapes …
This article will guide you to convert a grid from a picture into Excel format using Microsoft OneNote and no other 3rd party application.
This Micro Tutorial demonstrate the bugs in Microsoft Excel for Mac with Pivot Charts.
This Micro Tutorial will demonstrate in Google Sheets how to use the HYPERLINK function to create live links inside your spreadsheet.

706 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now