Solved

Open File and Save As

Posted on 2010-08-14
16
282 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
Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

 

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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Hiding information in Excel 16 70
Real Time 2 21
Excel User Form VBA Help 18 30
Excel - DATEDIF error #NUM 6 16
INDEX and MATCH can be used to great effect to replace HLOOKUP and VLOOKUP as it does not have the limitation of needing the data to be sorted so that the reference value is in the first column or row. It also has the ability to perform a bi-directi…
When you see single cell contains number and text, and you have to get any date out of it seems like cracking our heads.
This Micro Tutorial demonstrates how to create Excel charts: column, area, line, bar, and scatter charts. Formatting tips are provided as well.
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

785 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