+ Reply to Thread
Results 1 to 12 of 12

Excel 2003 importing data from multiple .txt files

Hybrid View

theDork Excel 2003 importing data... 06-15-2011, 01:32 AM
JBeaucaire Re: Excel 2003 importing data... 06-15-2011, 02:33 AM
snb Re: Excel 2003 importing data... 06-15-2011, 03:17 AM
theDork Re: Excel 2003 importing data... 06-15-2011, 04:56 AM
JBeaucaire Re: Excel 2003 importing data... 06-15-2011, 11:25 AM
theDork Re: Excel 2003 importing data... 06-15-2011, 03:53 PM
JBeaucaire Re: Excel 2003 importing data... 06-15-2011, 05:00 PM
theDork Re: Excel 2003 importing data... 06-15-2011, 08:44 PM
JBeaucaire Re: Excel 2003 importing data... 06-16-2011, 02:56 AM
theDork Re: Excel 2003 importing data... 06-16-2011, 08:17 AM
JBeaucaire Re: Excel 2003 importing data... 06-16-2011, 05:18 PM
theDork Re: Excel 2003 importing data... 06-16-2011, 08:38 PM
  1. #1
    Registered User
    Join Date
    06-15-2011
    Location
    Melbourne
    MS-Off Ver
    Excel 2003
    Posts
    6

    Excel 2003 importing data from multiple .txt files

    hi
    i have one folder containing my base.xls file and a variable number (between 1 and 250) of *.txt files
    each txt file has a variable number 1-n records (between 1 and 500)
    each txt file has the first 3 rows taken up with rubbish; the fourth row contains the column headings; rows 5-n have the data that i need to capture
    each row of data has 7 (columns) elements

    base.xls file has the appropriate column headings (7) in row 1

    i need a macro to import all data from all text files consecutively into Sheet1 of base.xls.
    When the data is imported base.xls_sheet1 it needs to occupy columns C:I ; column B in base.xls needs to be the file name of the originating .txt file (excluding the '.txt' suffix)

    is anyone able to help please?
    dave
    Last edited by theDork; 06-16-2011 at 08:35 PM. Reason: Solved

  2. #2
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: Excel 2003 importing data from multiple .txt files

    Can you upload 2 text files and then the base.xls that is created by importing those two files? In other words show us the BEFORE (2 text files of different lengths) and the AFTER (the filled in sheet1).

    Click GO ADVANCED and use the paperclip icon to post up your files.
    _________________
    Microsoft MVP 2010 - Excel
    Visit: Jerry Beaucaire's Excel Files & Macros

    If you've been given good help, use the icon below to give reputation feedback, it is appreciated.
    Always put your code between code tags. [CODE] your code here [/CODE]

    ?None of us is as good as all of us? - Ray Kroc
    ?Actually, I *am* a rocket scientist.? - JB (little ones count!)

  3. #3
    Forum Expert snb's Avatar
    Join Date
    05-09-2010
    Location
    VBA
    MS-Off Ver
    Redhat
    Posts
    5,649

    Re: Excel 2003 importing data from multiple .txt files

    sub snb()
      c00="E:\OF\"
      c01=dir(c00 & "*.txt")
    
      do until c01=""
        open c00 & c01 for input as 1
          c02= replace(input(LOF(1),1),vbcrlf,vbcrlf & replace(c01,".txt",Application.International(xlListSeparator))) 
          c03=c03 & vbcrlf & split(c02,split(c02,vbcrlf)(4))(1)
        close
        c01=dir
      loop
    
      open c00 & "all.csv" for output as 1
        print 1,c03
      close
    
      workbooks.open c00 & "all.csv"
    End sub
    Last edited by snb; 06-15-2011 at 11:29 AM.



  4. #4
    Registered User
    Join Date
    06-15-2011
    Location
    Melbourne
    MS-Off Ver
    Excel 2003
    Posts
    6

    Re: Excel 2003 importing data from multiple .txt files

    @ JBeaucaire ... i do not know where the 'Go' button is ... i have used the 'Additional Options / Manage Attachments' in the hope that this is what you want?
    Two txt files have been imported into base.xls using Semicolon delimiter ...
    Attached Files Attached Files

  5. #5
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: Excel 2003 importing data from multiple .txt files

    Before I spend any time on this, did you try to adapt the code offered above by SNB? I'm sure he'll be happy to explain/expand on it if you try it.

  6. #6
    Registered User
    Join Date
    06-15-2011
    Location
    Melbourne
    MS-Off Ver
    Excel 2003
    Posts
    6

    Re: Excel 2003 importing data from multiple .txt files

    Quote Originally Posted by JBeaucaire View Post
    Before I spend any time on this, did you try to adapt the code offered above by SNB? I'm sure he'll be happy to explain/expand on it if you try it.
    yes i have looked at that code ... it returns a syntax error - after commenting that line out it returns a path unknown error
    it doesn't seem to be tailored to the question in any way ...
    it would be good to get a solution with some comments etc so it can be modified to suit the actual need
    so no, it this time that code doesn't do a great deal

  7. #7
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: Excel 2003 importing data from multiple .txt files

    Give this a try:
    Option Explicit
    
    Sub ImportTextFiles()
    Dim txtPath As String
    Dim txtFile As String
    Dim txtName As String
    Dim wsBase As Worksheet
    Dim FR As Long, NR As Long
      
    Application.DisplayAlerts = False
    Application.ScreenUpdating = False
    
    'Imported to this worksheet
        Set wsBase = ThisWorkbook.Sheets("Sheet1")
    
    'remember the final \ in this string
        txtPath = "C:\2011\Text\"
    
    'clear existing report
        If wsBase.UsedRange.Rows.Count > 1 Then wsBase.UsedRange.Offset(1).ClearContents
        NR = 2
    
    'Start looping through text files in noted folder
        txtFile = Dir(txtPath & "*.txt")
    
        Do While Len(txtFile) > 0
            FR = NR
            txtName = Replace(txtFile, ".txt", "")
            ActiveWorkbook.Worksheets.Add
            With ActiveSheet.QueryTables.Add(Connection:="TEXT;" & txtPath & txtFile, Destination:=ActiveSheet.Range("A1"))
                .Name = txtName
                .FieldNames = True
                .RowNumbers = False
                .FillAdjacentFormulas = False
                .PreserveFormatting = True
                .RefreshOnFileOpen = False
                .RefreshStyle = xlInsertDeleteCells
                .SavePassword = False
                .SaveData = True
                .AdjustColumnWidth = True
                .RefreshPeriod = 0
                .TextFilePromptOnRefresh = False
                .TextFilePlatform = 437
                .TextFileStartRow = 1
                .TextFileParseType = xlDelimited
                .TextFileTextQualifier = xlTextQualifierDoubleQuote
                .TextFileConsecutiveDelimiter = False
                .TextFileTabDelimiter = False
                .TextFileSemicolonDelimiter = True
                .TextFileCommaDelimiter = False
                .TextFileSpaceDelimiter = False
                .TextFileColumnDataTypes = Array(1, 1, 1, 1, 1, 1, 1, 1)
                .TextFileTrailingMinusNumbers = True
                .Refresh BackgroundQuery:=False
            End With
            
            ActiveSheet.Range("A3").CurrentRegion.Offset(1).Copy wsBase.Range("C" & FR)
            NR = wsBase.Range("C" & wsBase.Rows.Count).End(xlUp).Row + 1
            wsBase.Range("B" & FR & ":B" & NR - 1) = txtName
            ActiveSheet.Delete              'delete temp sheet
            
            txtFile = Dir                   'get next text filename
        Loop
    
    wsBase.Columns.AutoFit                  'cleanup
    Application.ScreenUpdating = True
    End Sub

  8. #8
    Registered User
    Join Date
    06-15-2011
    Location
    Melbourne
    MS-Off Ver
    Excel 2003
    Posts
    6

    Re: Excel 2003 importing data from multiple .txt files

    Dim FR As Long, NR As Long

    Works a treat thank you ... I just want to comment out a little more and it would be good if you could give more info about FR and NR ... what exactly do they mean?

    also, you have txtFile = Dir but the only other pointer to Dir is outside the loop so I am not sure where it is getting changed values from - can you give a little info on that too please?

    otherwise this seems to meet the need very well thank you !!
    normally this would take me 6-8 hours to scramble together as i am more a formulae person so this is very helpful ... where do i send the bottle of wine?

  9. #9
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: Excel 2003 importing data from multiple .txt files

    FR = First Row (to enter sheet names)
    NR = Next Empty Row (at bottom of current data set)

    The DIR() function was "started" at the top of the macro. The next time we use it it simply reapplies the earlier function and moves on to the next file that matches the filter.

  10. #10
    Registered User
    Join Date
    06-15-2011
    Location
    Melbourne
    MS-Off Ver
    Excel 2003
    Posts
    6

    Re: Excel 2003 importing data from multiple .txt files

    Quote Originally Posted by JBeaucaire View Post
    FR = First Row (to enter sheet names)
    NR = Next Empty Row (at bottom of current data set)

    The DIR() function was "started" at the top of the macro. The next time we use it it simply reapplies the earlier function and moves on to the next file that matches the filter.
    Great ... that helps a lot. Now of course i need to tidy up the things not included in my specs (like shifting all a column to the right, and populating A:B as it runs through).

    All good ... got your text too thanks

  11. #11
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: Excel 2003 importing data from multiple .txt files

    The macro given will populate the data into columns C:I, and will fill in the sheet name in column B. The only thing left is the column A which you provided no specifications for. I saw 2000 was in that column in the sample, but no absolute indication where that comes from.


    If that takes care of your need, please click EDIT in your original post, click GO ADVANCED and set the PREFIX box to SOLVED.

  12. #12
    Registered User
    Join Date
    06-15-2011
    Location
    Melbourne
    MS-Off Ver
    Excel 2003
    Posts
    6

    Re: Excel 2003 importing data from multiple .txt files

    Quote Originally Posted by JBeaucaire View Post
    The macro given will populate the data into columns C:I, and will fill in the sheet name in column B. The only thing left is the column A which you provided no specifications for. I saw 2000 was in that column in the sample, but no absolute indication where that comes from.


    If that takes care of your need, please click EDIT in your original post, click GO ADVANCED and set the PREFIX box to SOLVED.
    I have 'Solved' this question as it does finish the request. And yes, I do know how it works - I left some specs out because I wanted the framework with which I could tailor the rest of the process since some of the data is confidential etc ... with your explanations of terms etc I am perfectly happy with the outcome, and very grateful for your time.
    Thanks again!

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1