Solved

Open File and Save As

Posted on 2010-08-14
16
284 Views
Last Modified: 2012-05-10
Advised to open new case.

I have several text files in a folder that I want to open automatically insert the snippet code attached for ...1.txt, then do the same thing for ...2.txt, and so on, then save to a different folder.  I've attached the snippet which I created from running a macro, I just don't know how to make it do it for each of the files, not just one.
Workbooks.OpenText Filename:= _
        "C:\Documents and Settings\szyynr\My Documents\Attachmate\POM Files\'SZYYNR SOIUF A0000A MASSE.K001'" _
        , Origin:=437, StartRow:=1, DataType:=xlFixedWidth, FieldInfo:=Array( _
        Array(0, 1), Array(4, 1), Array(10, 1), Array(23, 1), Array(30, 1), Array(32, 1), Array(44, _
        1), Array(85, 1), Array(87, 1), Array(109, 1), Array(122, 1), Array(130, 1), Array(170, 1), _
        Array(199, 1)), TrailingMinusNumbers:=True
    Columns("A:A").Select
    Selection.Delete Shift:=xlToLeft
    Columns("B:B").Select
    Selection.Delete Shift:=xlToLeft
    Columns("D:H").Select
    Selection.Delete Shift:=xlToLeft
    Columns("E:H").Select
    Selection.Delete Shift:=xlToLeft
    Rows("1:1").Select
    Selection.Insert Shift:=xlDown, CopyOrigin:=xlFormatFromLeftOrAbove
    Range("A1").Select
    ActiveCell.FormulaR1C1 = "ORDER NO"
    Range("B1").Select
    ActiveCell.FormulaR1C1 = "MODEL"
    Range("C1").Select
    ActiveCell.FormulaR1C1 = "EVENT"
    Range("D1").Select
    ActiveCell.FormulaR1C1 = "RTPWDATE"
    Range("A1").Select
    ActiveWorkbook.SaveAs Filename:= _
        "C:\Documents and Settings\szyynr\My Documents\Attachmate\POM Files\'SZYYNR SOIUF A0000A MASSE.K001'" _
        , FileFormat:=xlExcel8, Password:="", WriteResPassword:="", _
        ReadOnlyRecommended:=False, CreateBackup:=False

Open in new window

-SZYYNR-SOIUF-A0000A-MASSE1.txt
-SZYYNR-SOIUF-A0000A-MASSE2.txt
0
Comment
Question by:mato01
  • 8
  • 7
16 Comments
 
LVL 18

Expert Comment

by:Simon
ID: 33437952
Are you sure you want to insert the code snippet into each one? Wouldn't it be better to just run the code from a master workbook?
 You'd just have to open each file in turn (it automatically becomes the activeworkbook).
0
 
LVL 6

Expert Comment

by:KnutsonBM
ID: 33438010
See if this works for you

Sub OpenSave()

folderspec = "C:\Documents and Settings\szyynr\My Documents\Attachmate\POM Files\

Set fso = CreateObject("Scripting.FileSystemObject")
Set f = fso.GetFolder(folderspec)
Set fc = f.Files

For Each f1 In fc

If Right(f1.Name, 4) = ".txt" Then

Workbooks.OpenText Filename:= _
        f1 _
        , Origin:=437, StartRow:=1, DataType:=xlFixedWidth, FieldInfo:=Array( _
        Array(0, 1), Array(4, 1), Array(10, 1), Array(23, 1), Array(30, 1), Array(32, 1), Array(44, _
        1), Array(85, 1), Array(87, 1), Array(109, 1), Array(122, 1), Array(130, 1), Array(170, 1), _
        Array(199, 1)), TrailingMinusNumbers:=True
        Columns("A:A").Select
    Selection.Delete Shift:=xlToLeft
    Columns("B:B").Select
    Selection.Delete Shift:=xlToLeft
    Columns("D:H").Select
    Selection.Delete Shift:=xlToLeft
    Columns("E:H").Select
    Selection.Delete Shift:=xlToLeft
    Rows("1:1").Select
    Selection.Insert Shift:=xlDown, CopyOrigin:=xlFormatFromLeftOrAbove
    Range("A1").Select
    ActiveCell.FormulaR1C1 = "ORDER NO"
    Range("B1").Select
    ActiveCell.FormulaR1C1 = "MODEL"
    Range("C1").Select
    ActiveCell.FormulaR1C1 = "EVENT"
    Range("D1").Select
    ActiveCell.FormulaR1C1 = "RTPWDATE"
    Range("A1").Select
    ActiveWorkbook.SaveAs Filename:= _
        f1 _
        , FileFormat:=xlExcel8, Password:="", WriteResPassword:="", _
        ReadOnlyRecommended:=False, CreateBackup:=False
       
        Else
        End If
       
Next f1
 
End Sub
0
 
LVL 6

Expert Comment

by:KnutsonBM
ID: 33438013
Screwed up a line

folderspec = "C:\Documents and Settings\szyynr\My Documents\Attachmate\POM Files\

should be

folderspec = "C:\Documents and Settings\szyynr\My Documents\Attachmate\POM Files\"
0
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 

Author Comment

by:mato01
ID: 33438256
When I ran the script nothing happened????  Can you send me example?
0
 

Author Comment

by:mato01
ID: 33438499
Okay. The script works; however, it does not save it to a different folder or as an xls file.  It saves as a txt file.

After the script has run, I need to have the file save as .XLS files to the below folder

"C:\Documents and Settings\szyynr\My Documents\Attachmate\POM Files\FINISHED\
0
 

Author Comment

by:mato01
ID: 33438568
Okay. The script works; however, it does not save it to a different folder or as an xls file.  It saves as a txt file.

After the script has run, I need to have the file save as .XLS files to the below folder

"C:\Documents and Settings\szyynr\My Documents\Attachmate\POM Files\FINISHED\File1.xls
Okay. The script works; however, it does not save it to a different folder or as an xls file.  It saves as a txt file.

After the script has run, I need to have the files save as .XLS files to the below folder


So  That
"C:\Documents and Settings\szyynr\My Documents\Attachmate\POM Files\File1.txt becomes
"C:\Documents and Settings\szyynr\My Documents\Attachmate\POM Files\FINISHED\File1.xls

"C:\Documents and Settings\szyynr\My Documents\Attachmate\POM Files\File2.txt becomes
"C:\Documents and Settings\szyynr\My Documents\Attachmate\POM Files\FINISHED\File2xls

and so forth
0
 
LVL 6

Expert Comment

by:KnutsonBM
ID: 33438588
Sub OpenSave()

folderspec = "C:\Documents and Settings\szyynr\My Documents\Attachmate\POM Files\"
FinishedFolder = "C:\Documents and Settings\szyynr\My Documents\Attachmate\POM Files\FINISHED\"

Set fso = CreateObject("Scripting.FileSystemObject")
Set f = fso.GetFolder(folderspec)
Set fc = f.Files

For Each f1 In fc

If Right(f1.Name, 4) = ".txt" Then

Workbooks.OpenText Filename:= _
        f1 _
        , Origin:=437, StartRow:=1, DataType:=xlFixedWidth, FieldInfo:=Array( _
        Array(0, 1), Array(4, 1), Array(10, 1), Array(23, 1), Array(30, 1), Array(32, 1), Array(44, _
        1), Array(85, 1), Array(87, 1), Array(109, 1), Array(122, 1), Array(130, 1), Array(170, 1), _
        Array(199, 1)), TrailingMinusNumbers:=True
        Columns("A:A").Select
    Selection.Delete Shift:=xlToLeft
    Columns("B:B").Select
    Selection.Delete Shift:=xlToLeft
    Columns("D:H").Select
    Selection.Delete Shift:=xlToLeft
    Columns("E:H").Select
    Selection.Delete Shift:=xlToLeft
    Rows("1:1").Select
    Selection.Insert Shift:=xlDown, CopyOrigin:=xlFormatFromLeftOrAbove
    Range("A1").Select
    ActiveCell.FormulaR1C1 = "ORDER NO"
    Range("B1").Select
    ActiveCell.FormulaR1C1 = "MODEL"
    Range("C1").Select
    ActiveCell.FormulaR1C1 = "EVENT"
    Range("D1").Select
    ActiveCell.FormulaR1C1 = "RTPWDATE"
    Range("A1").Select
     
    ActiveWorkbook.SaveCopyAs (FinishedFolder & ActiveWorkbook.Name)
       
        Else
        End If
       
Next f1
 
End Sub
0
 

Author Comment

by:mato01
ID: 33438652
This saves the files to a new folder; however, they are still txt files.  I would like to be able to save as an excel formatted file.
0
 
LVL 6

Expert Comment

by:KnutsonBM
ID: 33438689
Sub OpenSave()

folderspec = "C:\Documents and Settings\hk468c\Desktop\New Folder\"
FinishedFolder = "C:\Documents and Settings\hk468c\Desktop\New Folder\FINISHED\"

Set fso = CreateObject("Scripting.FileSystemObject")
Set f = fso.GetFolder(folderspec)
Set fc = f.Files

For Each f1 In fc

If Right(f1.Name, 4) = ".txt" Then

Workbooks.OpenText Filename:= _
        f1 _
        , Origin:=437, StartRow:=1, DataType:=xlFixedWidth, FieldInfo:=Array( _
        Array(0, 1), Array(4, 1), Array(10, 1), Array(23, 1), Array(30, 1), Array(32, 1), Array(44, _
        1), Array(85, 1), Array(87, 1), Array(109, 1), Array(122, 1), Array(130, 1), Array(170, 1), _
        Array(199, 1)), TrailingMinusNumbers:=True
        Columns("A:A").Select
    Selection.Delete Shift:=xlToLeft
    Columns("B:B").Select
    Selection.Delete Shift:=xlToLeft
    Columns("D:H").Select
    Selection.Delete Shift:=xlToLeft
    Columns("E:H").Select
    Selection.Delete Shift:=xlToLeft
    Rows("1:1").Select
    Selection.Insert Shift:=xlDown, CopyOrigin:=xlFormatFromLeftOrAbove
    Range("A1").Select
    ActiveCell.FormulaR1C1 = "ORDER NO"
    Range("B1").Select
    ActiveCell.FormulaR1C1 = "MODEL"
    Range("C1").Select
    ActiveCell.FormulaR1C1 = "EVENT"
    Range("D1").Select
    ActiveCell.FormulaR1C1 = "RTPWDATE"
    Range("A1").Select
     
    ActiveWorkbook.SaveCopyAs (Left((FinishedFolder & ActiveWorkbook.Name), Len(FinishedFolder & ActiveWorkbook.Name) - 3) & "xls")
       
        Else
        End If
       
Next f1
 
End Sub



Sorry taht i didnt see that even though you posted it like 4 times, this should do the trick though!

-Brandon
0
 
LVL 6

Expert Comment

by:KnutsonBM
ID: 33438695
doh i posted my test code up there, you will have to replace the folderspec and FinishedFolder with your file paths, sorry
0
 

Author Comment

by:mato01
ID: 33438867
Almost works.  However, it still saves as a text file with a .xls extension, not as a .xls file.  See files attached.

Attachment 1 is what the results of the macro.

Attachment 2 is what I'm actually  lookoing for.
Book-0G.xls
Book-0G---FINISHED.xls
0
 
LVL 6

Accepted Solution

by:
KnutsonBM earned 500 total points
ID: 33438874
ActiveWorkbook.SaveAs Filename:= _
        (Left((FinishedFolder & ActiveWorkbook.Name), Len(FinishedFolder & ActiveWorkbook.Name) - 3) & "xls")
, _
        FileFormat:=xlExcel8, Password:="", WriteResPassword:="", _
        ReadOnlyRecommended:=False, CreateBackup:=False

try this
0
 

Author Comment

by:mato01
ID: 33438884
THis part turned RED (Compile error: Syntax error).  Am I missing something?

, _
        FileFormat:=xlExcel8, Password:="", WriteResPassword:="", _
        ReadOnlyRecommended:=False, CreateBackup:=False
0
 

Author Closing Comment

by:mato01
ID: 33438894
Ignore the comment after this.  I just needed to lose some space.  It works exactly like I wanted it to.

Thanks so much for your help, and definitely for your patience.

0
 
LVL 6

Expert Comment

by:KnutsonBM
ID: 33438895
no, copy and paste problem on my part, sorry
[code]
ActiveWorkbook.SaveAs FileName:= _
        (Left((FinishedFolder & ActiveWorkbook.Name), Len(FinishedFolder & ActiveWorkbook.Name) - 3) & "xls"), _
        FileFormat:=xlExcel8, Password:="", WriteResPassword:="", _
        ReadOnlyRecommended:=False, CreateBackup:=False
[\code]
0
 
LVL 6

Expert Comment

by:KnutsonBM
ID: 33438902
thanks for the points and grade!
0

Featured Post

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone 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

Workbook link problems after copying tabs to a new workbook? David Miller (dlmille) Intro Have you either copied sheets to a new workbook, and after having saved and opened that workbook, you find that there are links back to the original sou…
Freeze panes is an option within all variants of Excel to enable parts of a sheet to remain stationary when the cursor is in another part of the sheet. This is a very useful feature which is overlooked or under used.
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…
This Micro Tutorial demonstrates how to create Excel charts: column, area, line, bar, and scatter charts. Formatting tips are provided as well.

789 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