Solved

Open File and Save As

Posted on 2010-08-14
16
289 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
[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
  • 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
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!

 

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

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

Introduction This Article briefly covers methods of calculating the NPV and IRR variants in Excel as well as the limitations in calculating and interpreting IRR results. Paraphrasing Richard Shockley, author of my favourite finance reference tex…
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 Micro Tutorial demonstrates in Microsoft Excel how to consolidate your marketing data by creating an interactive charts using form controls. This creates cool drop-downs for viewers of your chart to choose from.
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…

717 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