MrExcel Message Board

Go Back   MrExcel Message Board > Question Forums > Excel Questions

Excel Questions All Excel/VBA questions - formulas, macros, pivot tables, general help, etc. Please post to this forum in English only.

Reply
 
Thread Tools Display Modes
Old May 21st, 2002, 09:59 AM   #1
joyjam
New Member
 
Join Date: Apr 2002
Posts: 6
Default

I want to import a batch of text files.
I have no idea how to do this, and I don't know any of this VBA stuff.
Please Help!
joyjam is offline   Reply With Quote
Old May 21st, 2002, 10:11 AM   #2
dk
MrExcel MVP
 
Join Date: Feb 2002
Location: Sydney, Australia
Posts: 2,908
Default

Hi,

Do you want each file on an individual worksheet, or all on the same worksheet? Are the text files all in the same place and what format are they in e.g. tab delimited, CSV, fixed width?

If you can provide this info then someone will be able to help - maybe me

Regards,
Dan
dk is offline   Reply With Quote
Old May 21st, 2002, 10:13 AM   #3
joyjam
New Member
 
Join Date: Apr 2002
Posts: 6
Default

Tab Delimited, to be imported into one worksheet, all files located in one folder, .txt files.


[ This Message was edited by: joyjam on 2002-05-21 09:15 ]
joyjam is offline   Reply With Quote
Old May 21st, 2002, 10:33 AM   #4
dk
MrExcel MVP
 
Join Date: Feb 2002
Location: Sydney, Australia
Posts: 2,908
Default

Hi,

Here is a piece of VBA code which should do what you want - it's not perfect because I've done it in a hurry but it should work OK.

Open the VB Editor (Alt+F11) and click Insert, Module. Then paste the code shown below. Change the .LookIn bit so that it matches the folder where your text files reside and then save the workbook.

If you then click Alt+F8 and choose the GetTextFiles macro it will import the text files into the workbook containing the macro with each file being on a new sheet.

Any problems please post back,

Dan

Code:
Sub GetTextFiles()
Dim lngCounter As Long, wbText As Workbook

On Error GoTo ErrHandler

Application.DisplayAlerts = False
Application.ScreenUpdating = False

With Application.FileSearch
    .NewSearch
    .FileType = msoFileTypeAllFiles
    .LookIn = "C:temptext files"  'Change this to your folder name
    .Execute
    For lngCounter = 1 To .FoundFiles.Count
        If Right(.FoundFiles(lngCounter), 4) = ".txt" Then
            Workbooks.OpenText .FoundFiles(lngCounter)
            ActiveSheet.UsedRange.Copy
            ActiveWorkbook.Close False
            ThisWorkbook.Sheets.Add
            ActiveSheet.Paste
        End If
    Next lngCounter
End With


Application.DisplayAlerts = True
Application.ScreenUpdating = True

Exit Sub

ErrHandler:
Application.ScreenUpdating = True
MsgBox Err.Description, vbExclamation, "Ooops, an error occurred"

End Sub
dk is offline   Reply With Quote
Old May 21st, 2002, 11:46 AM   #5
joyjam
New Member
 
Join Date: Apr 2002
Posts: 6
Default

Ok- This worked, except for the fact that now I have 693 sheets! How do I combine them all onto one sheet?
Also, I have lost all delimitation. Now everything is one rambling string in one cell. How do I fix it?
Thanks!

[ This Message was edited by: joyjam on 2002-05-21 10:49 ]
joyjam is offline   Reply With Quote
Old May 21st, 2002, 03:30 PM   #6
dk
MrExcel MVP
 
Join Date: Feb 2002
Location: Sydney, Australia
Posts: 2,908
Default

Try this instead. The code worked OK on my machine before but now I've changed it to specify a text-tab delimited file (rather than let Excel guess what it is). This brings all the text files onto one sheet.

Let me know how you get on,

Dan

Code:
Sub GetTextFiles()
Dim lngCounter As Long, wbText As Workbook

On Error GoTo ErrHandler

Application.DisplayAlerts = False
Application.ScreenUpdating = False

With Application.FileSearch
    .NewSearch
    .FileType = msoFileTypeAllFiles
    .LookIn = "C:temptext files"  'Change this to your folder name
    .Execute
    For lngCounter = 1 To .FoundFiles.Count
        If Right(.FoundFiles(lngCounter), 4) = ".txt" Then
            Workbooks.OpenText Filename:=.FoundFiles(lngCounter), tab:=True, DataType:=xlDelimited
            ActiveSheet.UsedRange.Copy
            ActiveWorkbook.Close False
            Range("A" & ActiveSheet.UsedRange.Rows.Count + 1).PasteSpecial
        End If
    Next lngCounter
End With


Application.DisplayAlerts = True
Application.ScreenUpdating = True

Exit Sub

ErrHandler:
Application.ScreenUpdating = True
MsgBox Err.Description, vbExclamation, "Ooops, an error occurred"

End Sub
dk is offline   Reply With Quote
Old Jan 20th, 2010, 08:57 PM   #7
dustymunchkin
New Member
 
Join Date: Jan 2010
Posts: 5
Default Re: Importing Multiple Text Files at once

Thankyou Thankyou! Even though I wasn't the asker of original question I've been wanting to find the solution to this for a while.

One query: My asc files have 10 sections but it's only pulling out 9.
Can you throw any light on why it's not picking up the last row?
dustymunchkin is offline   Reply With Quote
Old Jan 21st, 2010, 03:30 PM   #8
dustymunchkin
New Member
 
Join Date: Jan 2010
Posts: 5
Default Re: Importing Multiple Text Files at once

OK ignore last query think it's the files that are corrupted.
What I now need to do is get the headers with the files.

Found something on the site (see below) but get object invoked has disconnected from its clients on the line beginning x = application. I think because files are .asc.

How do I correct this?

Sub test()
Dim myDir As String, fn As String, txt As String, x
myDir = "c:\testt" '<- change to actual folder path
fn = Dir(myDir & "\*.asc")
Do While fn <> ""
txt = CreateObject("Scripting.FileSystemObject").OpenTextFile(myDir & "\" & fn).ReadAll
x = Application.Transpose(Split(txt, vbCrLf))
Sheets(1).Range("a" & Rows.Count).End(xlUp)(2).Resize(UBound(x, 1)).Value = x
Sheets(1).Range("b" & Rows.Count).End(xlUp)(2).Resize(UBound(x, 1)).Value = fn
fn = Dir()
Loop
End Sub
dustymunchkin is offline   Reply With Quote
Old Feb 16th, 2010, 01:00 AM   #9
wzlove26
New Member
 
Join Date: Feb 2010
Location: Ames, IA
Posts: 1
Default Re: Importing Multiple Text Files at once

I am trying to import thousands of .asc files into excel. I tried this script, but I do not know why this does not work for me. Could you help me out?
wzlove26 is offline   Reply With Quote
Old Jan 14th, 2012, 01:02 AM   #10
Samuel_T
New Member
 
Join Date: Jan 2012
Posts: 1
Default Re: Importing Multiple Text Files at once

Hello!

Sorry to bump an older post but I found it via google while looking for help on the subject.

The module posted here helps a lot! I'm just wondering how I could go about updating the sheets. I have the same number of text files I need to import every couple of days and I'd like them to keep the name I give them.

For now I've been deleting the sheets and running the module again, then renaming & re-coloring the tabs.

Is there a way to just easily replace the data in the sheets, keeping the tab names and colors?

Sorry for the terminology, I'm a total newbie with Excel.

Thanks for any help!
Samuel_T is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is On

Forum Jump


All times are GMT -4. The time now is 04:15 PM.


Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2012, vBulletin Solutions, Inc.
All contents Copyright 1998-2012 by MrExcel Consulting.
diabetic desserts recipes recipes Diabetic Soups Holiday Pizza Recipes Popcorn Recipes Recipes For Microwave Pasta Recipes Casserole Recipes Chili Recipes Curry Recipes Crockpot Recipes Apples Recipes Bread Recipes Vegetarian Recipes Vegetable recipes Desserts Recipes Appetizers Ethnic Recipes Meat Dishes Barbecue Recipes Sauces Recipes Marinade Recipes Low Fat Recipes Frugal Gourmet Kitchen Classics Recipes On The Grill Cook Books Seafood Recipes Cajun Recipes Breads Low Fat Low Fat Breads Bread Machine Recipes Yeast Breads Quick Breads Fat Free Vegetarian Salad Recipes Eggplant Recipes Radish Recipes Tomato Recipes Jalapeno Recipes Potato Recipes Lettuce Recipes Cabbage Recipes Beans Ambrosia Recipes Biscotti Recipes Desserts Low Fat Cookie Recipes Cheesecake Recipes Cake Recipes Pie Recipes Muffin Recipes Custard Recipes Best Appetizers Appetizers Low Fat Salsa Recipes Dip Recipes International Recipes Afghan Recipes Alaska Recipes French Recipes German Recipes Greek Recipes Italian Recipes Spanish Recipes Thai Recipes Korean Recipes Chinese Recipes Mexican Recipes Indian Recipes Beef Recipes Pork Pork & Ham Pork Butts Pork Chop Recipes Pork Ribs Rulled Pork Poultry Recipes Stews Recipes Ground Beef Barbecue Grill Barbecue Smoker All Purpose Sauce BBQ Sauce Barbecue Sauce Carolina BBQ Sauce Pickle Recipes Marinades Smoking Low Fat Appetizers & Dips Low Fat Breakfast Low Fat Cakes Low Fat Cheesecakes Low Fat Cookies Low Fat Desserts Low Fat Fish & Seafood Low Fat Meats Low Fat Pasta Low Fat Pies Low Fat Salads Low Fat Sandwiches Low Fat Sauces & Condiments Low Fat Sides Low Fat Soups Low Fat Vegetarian Baker's Dozen Taste of Home Recipe Book Bon Appetit Cookbook Blacktie Cookbook Buster Cook Book Cookbook USA Cook Book Cook Book Sara's Cookbook Sara's Cookbook Appetizers and Dips Poultry recipes Diabetic recipes Holiday recipes Miscellaneous recipes 110 recipes 1986 Usenet cookbook 2900 recipes Cyberrealm recipes Great sysops of world Specialty recipes Ceideburg recipes Cheese recipes Chili recipes Fruits recipes Garlic recipes Great chefs of NY Londontowne recipes Raisins recipes Recipes for kids US Food Vegetarian recipes Bread recipes Drinks Meat Dishes Brisket recipes Caribou recipes Chicken recipes Filet mignons recipes Pork recipes Swordfish recipes Turkey recipes Pasta recipes Uncategorized recipes Ethnic recipes Canada recipes English recipes Ethiopia recipes Germany recipes Greece recipes Mexican recipes Philippines recipes Welsh recipes Microwave recipes Soups recipes Vegetable recipes Asparagus recipes Barley recipes Brown rice recipes Lentil recipes Mushrooms recipes Salads recipes Wild rice Desserts recipes Cakes recipes Chocolate recipes Cookies recipes Ice cream recipes