How can I batch open a txt file in excel 2003 and save back as a .

I have about 3000 .txt files I need to open in excel and just save them.  Is 
there a way I can batch run these?  It would involve opening the txt file and 
saving it as the same name.
0
Silzle (1)
2/9/2005 2:53:02 PM
excel.misc 78881 articles. 5 followers. Follow

3 Replies
320 Views

Similar Articles

[PageSpeed] 58

Try this macro. Change "MyFolder" to the actual path. To 
use, open up a new workbook, press ALT+F11, go to Insert 
> Module, and paste in the code. Press ALT+Q, go to Tools 
> Macro > Macros and run it.

Sub ChangeXLS()
'Constructive criticism from VBA programmers appreciated
Dim MyFolder As String
Dim NewName As String
Dim OldName As String
Dim patharray As Variant
MyFolder = "C:\Program Files\ztest"
Application.ScreenUpdating = False
With Application.FileSearch
    .NewSearch
    .LookIn = MyFolder
    .SearchSubFolders = False
    .Filename = "*.txt"
    .FileType = msoFileTypeAllFiles
    Application.DisplayAlerts = False
    If .Execute() > 0 Then
        For i = 1 To .FoundFiles.Count
            patharray = Split(.FoundFiles(i), "\")
            OldName = patharray(UBound(patharray))
            NewName = Application.WorksheetFunction. _
            Substitute(OldName, ".txt", ".xls")
            Workbooks.Open Filename:=MyFolder _
            & "\" & OldName
            With ActiveWorkbook
                .SaveAs Filename:=MyFolder & "\" & NewName
                .Close
            End With
        Next
    Else
        MsgBox "There were no files found."
        Exit Sub
    End If
    Application.DisplayAlerts = True
End With
Application.ScreenUpdating = True
End Sub

---
HTH
Jason
Atlanta, GA

>-----Original Message-----
>I have about 3000 .txt files I need to open in excel and 
just save them.  Is 
>there a way I can batch run these?  It would involve 
opening the txt file and 
>saving it as the same name.
>.
>
0
jasonjmorin (551)
2/9/2005 4:28:13 PM
And, in case FileSearch doesn't work correctly (it doesn't for me in XL-XP --
returns no files when there ARE matching files), in the past I've posted code
to retrieve a list of files in a directory. So has Bill Manville. If you can't
find it on Google, I can re-post.

On Wed, 9 Feb 2005 08:28:13 -0800, "Jason Morin"
<jasonjmorin@OPPOSITEOFCOLDmail.com> wrote:

>Try this macro. Change "MyFolder" to the actual path. To 
>use, open up a new workbook, press ALT+F11, go to Insert 
>> Module, and paste in the code. Press ALT+Q, go to Tools 
>> Macro > Macros and run it.
>
>Sub ChangeXLS()
>'Constructive criticism from VBA programmers appreciated
>Dim MyFolder As String
>Dim NewName As String
>Dim OldName As String
>Dim patharray As Variant
>MyFolder = "C:\Program Files\ztest"
>Application.ScreenUpdating = False
>With Application.FileSearch
>    .NewSearch
>    .LookIn = MyFolder
>    .SearchSubFolders = False
>    .Filename = "*.txt"
>    .FileType = msoFileTypeAllFiles
>    Application.DisplayAlerts = False
>    If .Execute() > 0 Then
>        For i = 1 To .FoundFiles.Count
>            patharray = Split(.FoundFiles(i), "\")
>            OldName = patharray(UBound(patharray))
>            NewName = Application.WorksheetFunction. _
>            Substitute(OldName, ".txt", ".xls")
>            Workbooks.Open Filename:=MyFolder _
>            & "\" & OldName
>            With ActiveWorkbook
>                .SaveAs Filename:=MyFolder & "\" & NewName
>                .Close
>            End With
>        Next
>    Else
>        MsgBox "There were no files found."
>        Exit Sub
>    End If
>    Application.DisplayAlerts = True
>End With
>Application.ScreenUpdating = True
>End Sub
>
>---
>HTH
>Jason
>Atlanta, GA
>
>>-----Original Message-----
>>I have about 3000 .txt files I need to open in excel and 
>just save them.  Is 
>>there a way I can batch run these?  It would involve 
>opening the txt file and 
>>saving it as the same name.
>>.
>>

0
anonymous (74719)
2/9/2005 6:25:14 PM
Just some thoughts....(and avoiding the problem that .filesearch poses in some
versions of windows).

Option Explicit
Sub ChangeXLS()
    'Constructive criticism from VBA programmers appreciated
    Dim MyFolder As String
    Dim NewName As String
    Dim i As Long  '<-- you missed this one!
    Dim Wkbk As Workbook
    
    'MyFolder = "C:\my documents\excel\test"
    MyFolder = "C:\Program Files\ztest"
    Application.ScreenUpdating = False
    With Application.FileSearch
        .NewSearch
        .LookIn = MyFolder
        .SearchSubFolders = False
        
        'from what I've read, this is probably more robust
        'across all versions of windows
        '(instead of *.txt)
        .Filename = ".txt"
        
        .FileType = msoFileTypeAllFiles
        If .Execute() > 0 Then
            For i = 1 To .FoundFiles.Count
                'since you're looking at .txt files, just chop it
                NewName = Left(.FoundFiles(i), _
                             Len(.FoundFiles(i)) - 4) & ".xls"
                Set Wkbk = Workbooks.Open(Filename:=.FoundFiles(i))
                With Wkbk
                    Application.DisplayAlerts = False
                    .SaveAs Filename:=NewName, FileFormat:=xlWorkbookNormal
                    Application.DisplayAlerts = True
                    .Close savechanges:=False
                End With
            Next
        Else
            MsgBox "There were no files found."
            'might as well let the code finish
            'and reset all your stuff (.screenupdating in this case)
            'Exit Sub
        End If
    End With
    Application.ScreenUpdating = True
End Sub

Application.worksheetfunction.substitute() is case sensitive.  If your filename
were asdf.TxT, then you wouldn't get your replaced .xls.

..foundfiles(i) will return the fully qualified filename.  And since you're
saving to the same location, you don't need to extract the filename and later
rebuild it.

I like to turn off error checking/.displayalerts for as little time as
possible.  And right near the lines that I want.

And I think I wouldn't leave it to excel to guess what fileformat I wanted.  It
doesn't hurt to specify it and it makes me feel better.  (Same thinking with
savechanges:=false.)

And I like using a workbook variable to hold the newly opened workbook (.txt
file).

And I think I would probably use Workbooks.OpenText to be able to specify how to
import each field.

Then this:
                Set Wkbk = Workbooks.Open(Filename:=.FoundFiles(i))
                With Wkbk

Would look more like:

                Workbooks.OpenText Filename:=.foundfiles(i), Origin:=437, _
                    StartRow:=1, DataType:=xlDelimited, _
                    TextQualifier:=xlDoubleQuote, _
	            ConsecutiveDelimiter:=False, Tab:=True, _
                    Semicolon:=False, Comma:=False, Space:=False, _
                    Other:=False, FieldInfo:=Array(1, 1)
                Set wkbk = activeworkbook
                with wkbk

When I post routines like this, I'll suggest that they open one .txt file
manually with the recorder on.  Then they can plop that portion of their
recorded macro into this section.



Jason Morin wrote:
> 
> Try this macro. Change "MyFolder" to the actual path. To
> use, open up a new workbook, press ALT+F11, go to Insert
> > Module, and paste in the code. Press ALT+Q, go to Tools
> > Macro > Macros and run it.
> 
> Sub ChangeXLS()
> 'Constructive criticism from VBA programmers appreciated
> Dim MyFolder As String
> Dim NewName As String
> Dim OldName As String
> Dim patharray As Variant
> MyFolder = "C:\Program Files\ztest"
> Application.ScreenUpdating = False
> With Application.FileSearch
>     .NewSearch
>     .LookIn = MyFolder
>     .SearchSubFolders = False
>     .Filename = "*.txt"
>     .FileType = msoFileTypeAllFiles
>     Application.DisplayAlerts = False
>     If .Execute() > 0 Then
>         For i = 1 To .FoundFiles.Count
>             patharray = Split(.FoundFiles(i), "\")
>             OldName = patharray(UBound(patharray))
>             NewName = Application.WorksheetFunction. _
>             Substitute(OldName, ".txt", ".xls")
>             Workbooks.Open Filename:=MyFolder _
>             & "\" & OldName
>             With ActiveWorkbook
>                 .SaveAs Filename:=MyFolder & "\" & NewName
>                 .Close
>             End With
>         Next
>     Else
>         MsgBox "There were no files found."
>         Exit Sub
>     End If
>     Application.DisplayAlerts = True
> End With
> Application.ScreenUpdating = True
> End Sub
> 
> ---
> HTH
> Jason
> Atlanta, GA
> 
> >-----Original Message-----
> >I have about 3000 .txt files I need to open in excel and
> just save them.  Is
> >there a way I can batch run these?  It would involve
> opening the txt file and
> >saving it as the same name.
> >.
> >

-- 

Dave Peterson
0
ec357201 (5290)
2/9/2005 11:44:10 PM
Reply:

Similar Artilces:

concatenate files...
Back in DOS days I could concatenate text files without using a third party tool. Can I do this with XP, Vista, or W7? TIA, Paul "Paul H" <NoSpamphobergNoSpam@att.net> wrote: >Back in DOS days I could concatenate text files without using a third party >tool. Can I do this with XP, Vista, or W7? TIA, Paul Yes, the same way you did before: copy file1.txt + file2.txt fileboth.txt Type copy /? at a command prompt to find out all about the copy command. -- Tim Slattery Slattery_T@bls.gov http://members.cox.net/slatteryt Paul H wrote: ...

How to tell if a form is open
I want to know how to return a value telling me whether or not a form is currently open. Something like this pseudo-code: If Forms![new appointment].Open = True Then 'send harry a box of chocolates' End If Cheers -- Nick Assuming Access 2000 or later: If CurrentProject.AllForms("new appointment").IsLoaded Then -- Allen Browne - Microsoft MVP. Perth, Western Australia Tips for Access users - http://allenbrowne.com/tips.html Reply to group, rather than allenbrowne at mvps dot org. "Nick Heath" <NickHeath@discussions.microsoft.com> wrote in messa...

error: OBE-15502 Can only have 16000 per document
Hi, I tried to export data from oracle query builder to Excel sheet, got a error message: OBE-15502 Can only have 16000 per document Is there any way i can get more records(over 16000 or unlimited) in one sheet? thanks. -- JJ ...

Can't see 401(k) contribiltion total
I've set up a 401(k)accont. I transfer contribiltions to this 401(k)account from each paycheck. When I go to the 401 (k) account view, it shows all the informations such as transections and the account vallue ect., EXCEPT the total contribiltion. I want to know not only what my 401(k) vallue is, but how much money I have contribilt total or over a period of time as well. I find no place to see it. Anybody can help? I'm using Money2002 and 2004 Trial Verstion. Thanks a lot! ...

How can I print a selection in Outlook 2003?
How can I print just a selection of an email in Outlook 2003? The printer driver window is different from the one that comes up in Outlook Express. Thank you! You can do this for HTML formatted messages. Highlight your text, File-> Print... and select to print only the selection -- Robert Sparnaaij [MVP-Outlook] www.howto-outlook.com Tips of the month: -What do the Outlook Icons Mean? -Create an Office 2003 CD slipstreamed with Service Pack 1 ----- "Murphie" <Murphie@discussions.microsoft.com> wrote in message news:92F39B50-B877-408A-AAA7-DF350B1D72ED@microsoft.com....

Can different editions of Publisher be used together?
If my son uses Publisher 2003 on a school computer, downloads his work to a flash drive, brings it home to edit on Publisher 2007, will he then be able to take the edited work back to the school computer to be used, or will it not be compatible? Stateparent wrote: > If my son uses Publisher 2003 on a school computer, downloads his work to a > flash drive, brings it home to edit on Publisher 2007, will he then be able > to take the edited work back to the school computer to be used, or will it > not be compatible? Publisher 2003 and 2007 share a common file format, so the sc...

Can't send/receive
anyone have a situation where all of sudden they can't send new messages or receive anything? I get a message when I click on "accounts" that says 'fatal error in outlook unable to complete operation" ...

Can not Customize Leads View
Hi I have tried to customize the Leads view, I tried to add a view and add some columns to it. The view is shown however I see only the name column. When I go back to the View customization, I see the columns I added. Published your customizations??? -- ---------------------- regards, Ilya Milshtein MBS Master, MBSCP, MCP Softline International www.softline.ru "alsap" wrote: > Hi I have tried to customize the Leads view, I tried to add a view and add > some columns to it. > The view is shown however I see only the name column. > When I go back to the View customi...

Can I change the "Applied Task Update Requests and Errors" view?
I really like this view in PWA 2007, but I would like to replace one column with another. Is this view customizeable? Randy -- Bad news, my friend. This view is not customizable, nor is the view shown in the Task Updates customizable (which many users wish it was). Hope this help. -- Dale A. Howard [MVP] VP of Educational Services msProjectExperts http://www.msprojectexperts.com http://www.projectserverexperts.com "We write the books on Project Server" "Randy Schmid" <RandySchmid@discussions.microsoft.com> wrote in message news:72FA34B5-...

Should be an easy but I can not figure it out!!
I have a text box where the user enters a date and it is assigned to variable txtDate. How to I convert that to the serial number of the date? Thanks for the help IIRC, you can use DATEVAL(String) to get the serial date. -- HTH, Barb Reinhardt "VinceW" wrote: > I have a text box where the user enters a date and it is assigned to variable > txtDate. How to I convert that to the serial number of the date? > > Thanks for the help Barb, Thanks for the quick reply, however I get the error "Sub or Function not defined" when trying...

Excel's calculations are off a few cents!
I can't figure out why my TOTAL for $ column is off from 1-5 cents. When I check the amounts going across, they are correct, but the amount at the bottom of the column is off!! What am I doing wrong? Any help would be greatly appreciated, as I have to manually check all Excel's figures. View more decimal places Natalie, does it still add up incorrectly?? I'm guessing your summing calculated values, that are viewed at less precision than calculated. You can use ROUND(formula, #decimals) to make the calculations round to the precision you desire, or you can set "Tools&q...

Bulk attachments
I have a very long list of Images in a single folder that I want to attach to seperate fields in a table/form. Each record contains two images, which are named like this: W:\Foldername\Micromap Run 001 A.bmp =(1st record) W:\Foldername\Micromap Run 001 B.bmp =(1st record) W:\Foldername\Micromap Run 002 A.bmp =(2nd record) W:\Foldername\Micromap Run 002 B.bmp =(2nd record) W:\Foldername\Micromap Run 003 A.bmp =(3rd record) W:\Foldername\Micromap Run 003 B.bmp =(3rd record) and so on... Image A goes to field A Image B goes to field B Is there a way of doing this au...

Publisher file won't open #2
I worked for three hours then Publsher crashed. I had saved my file, but when I trie to open it it says problem with file, do you want to report it, and then reboots Publisher. How can I save my work? Using Publisher 2003. You may send the file to me. If I can open it I'll save it for you. If it crashes my system, you may be SOL. (Surely out of luck.) If you want to accept my offer, send it to jl dot paules at gmail at com. -- JoAnn Paules MVP Microsoft [Publisher] "Don Smith" <dhsmith@bellsouth.net> wrote in message news:r4ns615gkmkm12q5or5nd0k70gac5stjbc@4ax....

can't uninstall exchange from cluster nodes
hi our client has several exchange clusters, and one of them is being retired. we have removed the exchange resources and the EVS and the server no longer appears in the organization and everything else there seems to be ok. we can't seem to uninstall exchange from either of the nodes. setup will start saying it's loading components then terminates with no error; happens on both nodes. both nodes are exchange 2003 sp1 on windows 2003. i did find the setup log; here is what it says, hope someone can shed some light on this (company info replaced with < >): [19:47:23] *...

audible click when opening a new window.
I know I should be able to find this somewhere but I can't seem to figure it out. When I open a new folder or window there is a "click" sound that the computer makes. How can I turn this off? Thanks! Amadeus Try: Control Panel-Sounds "Start Navigation" and "Complete Navigation" are the relevant items. ....Alan -- Alan Edwards, MS MVP Windows -Internet Explorer http://dts-l.com/index.htm On Sun, 13 Jun 2010 13:56:27 -0700, in microsoft.public.windowsxp.basics, Amadeus <Amadeus@discussions.microsoft.com> wrote: >I kno...

Excel 2007 Chart with Zero Values
I have chart data for sales that I add to each day. I hide the lines for the days to come(ie if it's the 5th, I hide lines for the 6th - 31st) so that the chart doesn't go all the way down to zero for the 6th thru the 31st. I'd like to be able to leave all lines unhid, but not have the chart line go down to zero. I've tried putting N/As in those days that I don't have data, but the chart is still going to down to zero - I don't know if I have a setting wrong or what, but any help would be much appreciated. Thanks. Hi, Try either #N/A or th...

Deleting a batch...
I have just upgraded from QS2000.... I have redesigned my taxes to base them on the customers Custom text 1 field... (aka County) We tendered a transaction on a customer that was using an older tax schedule, we changed the customer record to use the new tax record... when we voided the tax record... It credited the new tax category instead of the one that was originally charged... I tried to switch the customer back to So I ran a sale and void in the reverse order, hoping the sale would offset the last void, and the new void would offset the first sale... but it didnt' work... W...

Excel formulas imported from tab-delimited files
I am trying to import some tab delimited entries into an Excel spreadsheet. Everything is working fine and dandy except that the formula fields which reference other worksheets in the spreadsheet are not getting automatically resolved unless I double click on them. For example, the "=Employee_Info!Y6" entry is not automatically getting resolved unless I double click on that value. Formulas that do not reference other worksheets are resolving correctly, without any intervention. Can someone kindly suggest how I can get past the problem? Thanks, Nimmi I'm not sure why excel ...

Can't delete email #2
I'm am trying to help a friend who is using Outlook Express on W98, I cannot delete any messages from her in box. She has over 500 in there, which may be part of the problem. Any suggestions? Desperate! Mark Mark Ryan <m.ryan@earthlink.com> wrote: > I'm am trying to help a friend who is using Outlook Express > on W98, I cannot delete any messages from her in box. She > has over 500 in there, which may be part of the problem. > Any suggestions? The first suggestion is to ask in an Outlok Express newsgroup. The second suggestion is to delete Deleted Items.dbx and ...

Can Not Reply To Emails
Running Windows 7 Professional with latest version of WLM. I get email messages I can not reply to. The reply button is grayed out. For now I am forwarding them to my WinXP computer and then replying from that computer with OE6. Anybody have any ideas? -- <Bill> Brought to you from Anchorage, Alaska. The most obvious reason for not having an active Reply button is not having a mail account set up. --=20 Gary VanderMolen, Microsoft MVP (Mail) http://mvp.support.microsoft.com/default.aspx/profile/vandermolen "Bill Bradshaw" <bradshaw@gci.net>...

How can I rename a database table?
Hi I am working on a project that uses the MFC CDatabase and CRecordset classes to handle its interfacing with an Access database. It is necessary for the software to rename a table within existing user databases to ensure that they are valid for use with the new version of the software. As a result of my limited database and MFC knowledge I am having difficulty finding a way of renaming the table. I have tried using the following: try { theDataBase.ExecuteSQL( "RENAME oldTableName TO newTableName"); } catch( CDBException* e) { e->m_strError; } However, it results in th...

Can (or how can) I do this
Hello, My Access application tracks an employee's progress through a process similar to hiring. There are 5 steps in the process. I track 4 of the steps by counting the number of times a value appears in a date field for that step. The count is then displayed and used in a calculation. The 5th step is more complicated. The 5th step needs to determine if the employee has verified that he can access all the applications that he should have access to. My process tracking table has the first 4 dates. The application access info comes from a different table. This process trackin...

how can I restore unsaved changes in excel #2
...

Cannot open attachments
Using Outlook 03 I cannot open html attachments. I can open images (jpg, etc). What is blocking the html attachments? I tried cleaning all the temp files that did not help. Should I just blow out the profile and start again? I wouldn't blow away the profile just yet. Do you get any errors messages? What happens when you try? -- Diane Poremsky [MVP - Outlook] Outlook Tips: http://www.outlook-tips.net/ Outlook & Exchange Solutions Center: http://www.slipstick.com/ Outlook Tips by email: mailto:dailytips-subscribe-request@lists.outlooktips.net EMO - a weekly newsl...

Can Not Connect to Exchange Server after Mailbox Move
I have 2 exchange servers. While logged into the domain, and at the location, I can log into the email system just fine. While remote VPN in, I can not access the server. I can ping the server, I can see all shares, and I can even access the printer and print from the exchange server. But I can not get to it with email. Any Ideas? Oh.. both exchange 5.5, both in the same site. Exchange Admin shows both sites just fine. is there a firewall with rules configured, in between the users vpn connection and the internal network? kmelillo@gmail.com wrote: > Oh.. both exchange 5.5, bot...