Excel File To a Text File

pbt

Well-known Member
Joined
Oct 18, 2005
Messages
1,613
I'm having problems converting to a text file from Excel.

The below code worked in 2003, but now I have put it in 2007 with just a change in the Range to work with.

What I'm getting is a Text file with, for lack of a better term, hyroglifics (sp).
Not the numbers that I should get like I got in 2003.

Code:
Private Sub CommandButtonExportPAG_Click()
    Application.ScreenUpdating = False
    On Error GoTo errTrap
    '//Hide the rows with 0 values
    LastRow = Range("S65536").End(xlUp).Row
     For r = LastRow To 263 Step -1
        If (Range("S" & r).Value = 0) Then
            Rows(r).Delete
        End If
    Next r
Application.Wait (Now + TimeValue("0:00:01"))
    Range("K264", Range("S264").End(xlDown).Offset(0, -1)).Copy
        Sheets.Add
        ActiveSheet.Name = "TEMP"
    Selection.PasteSpecial Paste:=xlValues
    Application.CutCopyMode = False
    Application.Range("A1").Select

    Sheets("TEMP").Copy
    Application.DisplayAlerts = False

    ActiveWorkbook.SaveAs Filename:= _
        "E:\Documents and Settings\michele_p.NSP\Desktop\Job Setup.txt", FileFormat _
        :=xlText, CreateBackup:=False
    
    ActiveWindow.Close
    ActiveWindow.SelectedSheets.Delete
        Sheets("PAG").Range("K264", Range("S264").End(xlDown)).ClearContents
        Range("A1").Select
    Application.DisplayAlerts = True
    Application.ScreenUpdating = True
    SplashForm.Show
    Exit Sub
errTrap:
    Application.Dialogs(xlDialogSaveAs).Show ("Job Setup.txt")
    ActiveWindow.Close
    ActiveWindow.SelectedSheets.Delete
        Sheets("PAG").Range("K264", Range("S264").End(xlDown)).ClearContents
         Range("A1").Select
    
    Application.DisplayAlerts = True
    Application.ScreenUpdating = True
    SplashForm.Show
         
End Sub

Anybody have an idea?

Thanks
Harry
 
Thank you that makes since.

What I'm still lost on is the (my) code that works in 2003 doese not work with 2007.

You had to go to "Creat Object" etc. etc. to make it work for 2007.

Why would a my code written for 2003 not work with 2007 ??

Harry
 
Upvote 0
CreateObject("WScript.Shell").SpecialFolders("Desktop")

It is not because of the Version, it is just calling SpecialFolders function from WSH (Windows Scripting Host).
It returns DeskTop path for any PC (when multi user, logged in desktop)
You can also use FileSystemObject.
 
Upvote 0
One little bit of help:

This is what I currently have, (with my attempt to solve, which does not work)
Code:
Private Sub CommandButtonExportPAG_Click()
    On Error GoTo errTrap
    Dim fn As String, txt As String
   ' Dim MyCell As Variant  '\\added
   ' Set MyCell = Range("J1").Value  '\\added
    Application.ScreenUpdating = False
fn = CreateObject("WScript.Shell").SpecialFolders("Desktop") & "\Job Setup & MyCell.txt" '\\added
With Sheets("PAG")
    For Each r In .Range("s93", .Range("s" & Rows.Count).End(xlUp))
        If r.Value <> 0 Then txt = txt & vbCrLf & Join(Evaluate("transpose(transpose(" & _
         r.Offset(, -8).Resize(, 8).Address & "))"), vbTab)
    Next
End With
Open fn For Output As #1
    Print #1, Mid$(txt, Len(vbCrLf) + 1)
Close #1
    Sheets("PAG").Range("K93", Range("S93").End(xlDown)).ClearContents
    Range("A1").Select
    Application.ScreenUpdating = True
    SplashForm.Show
    Exit Sub
errTrap:
    Sheets("PAG").Range("K93", Range("S93").End(xlDown)).ClearContents
    Range("A1").Select
    Application.ScreenUpdating = True
    SplashForm.Show
End Sub

I need to run this more then once before exporting the Text files into my other program.

Each time it runs, it overwrites the file on the Desktop.

Is there way to make it rename the Text file each time it is run?
Job Setup(2), Job Setup(3), etc. is okay.

Or if I can grab the Value from say J1 to be the name of the Text file. J1 will change each time it runs.

Thanks
Harry
 
Upvote 0
How about
Code:
fn = CreateObject("WScript.Shell").SpecialFolders("Desktop") & _
       "\Job Setup & MyCell.txt"
to
Code:
fn = CreateObject("WScript.Shell").SpecialFolders("Desktop") & _
       "\Job Setup(" & Format$(Now,"mmddyy-hh:mm:ss") & ").txt"
 
Upvote 0
Thanks jindon

I reads all the way to Open fn For Output As #1 the goes to errTrap:

Harry
 
Upvote 0
Thanks jindon, again

Code:
"mmddyy-hhmmss"
worked, grouped the time as one number



Code:
"mmddyy-hh-mm-ss"
Also worked with the hyphen between the hour, min, sec.

Harry
 
Upvote 0

Forum statistics

Threads
1,226,832
Messages
6,193,211
Members
453,780
Latest member
Nguyentam2007

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top