+ Reply to Thread
Results 1 to 13 of 13
  1. #1
    Registered User
    Join Date
    04-24-2008
    Posts
    63

    Excel 2003 - Importing text file > 65536 rows

    Hello,
    Recently one of our systems underwent a large upgrade and one of the files I receive everyday is about 10x bigger than it was. I had this automated process, where excel would open this file and go to the very bottom, then to the very right, and copy this value into a master sheet for me. This is simply the count of the # of records in the file.

    The problem is now, this file is 200 000 rows, too big for our Excel 2003 to handle at work... so I looked around and found some code that can import this into multiple spreadsheets, but I am trying to avoid this as I have to do this with around 40 files, is there a way to just import the last row? or the last section?, -- as the only value in this entire file that I require is the the rightmost value in the very bottom row.

    Thanks for any suggestions in advance,
    Adam
    Last edited by AdamParker; 10-22-2009 at 07:01 PM.

  2. #2
    Valued Forum Contributor Rick_Stanich's Avatar
    Join Date
    11-21-2005
    Location
    Eastlake Ohio
    MS-Off Ver
    2010
    Posts
    1,000

    Re: Excel 2003 - Importing text file > 65536 rows

    A software upgrade may be the simplest answer, Excel 2007 can handle 1,048,576 rows x 16,384 columns.
    Regards

    Rick
    Win7, Office 2010

  3. #3
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & read 2007
    Posts
    15,498

    Re: Excel 2003 - Importing text file > 65536 rows

    Hello Adam,

    What type of file are you referring to? Is it an Excel workbook, a CSV, a text file, or a database?
    Sincerely,
    Leith Ross

    Remember To Do the Following....

    1. Use code tags. Place [CODE] before the first line of code and [/CODE] after the last line of code.
    2. Thank those who have helped you by clicking the Star below the post.
    3. Please mark your post [SOLVED] if it has been answered satisfactorily.


    Old Scottish Proverb...
    Luathaid gu deanamh maille! (Rushing causes delays!)

  4. #4
    Registered User
    Join Date
    04-24-2008
    Posts
    63

    Re: Excel 2003 - Importing text file > 65536 rows

    Hello,
    Unfortunately a software upgrade is out of the question right now... I work for a place that is very slow to update... we are expecting Excel 2007 to be rolled out next year.

    I am importing a text file.

  5. #5
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & read 2007
    Posts
    15,498

    Re: Excel 2003 - Importing text file > 65536 rows

    Hello AdamParker,

    I haven't test this macro as I don't have a file large enough to import. The macro will prompt you for the text file to open and start importing the data at cell "A1" of the last worksheet in the workbook (usually Sheet3). Once the sheet is filled, a new sheet will be added to the workbook. This process will be repeated until the file is downloaded. The default delimiter is a comma. You change this and the starting worksheet in the code. They are marked in red. The file status is displayed in Excel's Status Bar .
    Code:
    'Written: October 21, 2009
    'Author:  Leith Ross
    'Summary: Imports a text file greater than 65535 lines into Excel.
    
    Sub ImportTextFile()
    
      Dim Arr As Variant
      Dim Data As Variant
      Dim Delimiter As String
      Dim FileFilter As String
      Dim FileName As String
      Dim N As Integer
      Dim R As Long
      Dim Wks As Worksheet
      
        Delimiter = ","
        Set Wks = Worksheets("Sheet3")
        
        FileFilter = "Text Files (*.csv; *.txt), *.csv;*.txt, All Files (*.*), *.*"
        FileName = Application.GetOpenFilename(FileFilter, 0, "Open")
        If FileName = "False" Then Exit Sub
        
          N = FreeFile
          Application.DisplayStatusBar = True
          
          Open FileName For Input As #N
            Do While Seek(N) <= LOF(N)
              R = R + 1
              If R > Rows.Count Then
                 R = 1
                 Set Wks = Worksheets.Add(After:=Worksheets.Count)
              End If
            
              Line Input #N, Data
              Arr = Split(Data, Delimiter)
              Wks.Cells(R, "A").Resize(ColumnSize:=UBound(Arr) + 1).Value = Arr
              
              Application.StatusBar = "Writing Row " & R & " on " & Wks.Name
            Loop
          Close #N
          
          Application.StatusBar = "Download complete."
          
    End Sub
    Sincerely,
    Leith Ross

    Remember To Do the Following....

    1. Use code tags. Place [CODE] before the first line of code and [/CODE] after the last line of code.
    2. Thank those who have helped you by clicking the Star below the post.
    3. Please mark your post [SOLVED] if it has been answered satisfactorily.


    Old Scottish Proverb...
    Luathaid gu deanamh maille! (Rushing causes delays!)

  6. #6
    Registered User
    Join Date
    04-24-2008
    Posts
    63

    Re: Excel 2003 - Importing text file > 65536 rows

    Hi Leith,
    There is an error in the code unfortunately...
    When it gets to "Writing Row 65536 on Sheet3" - an alert comes up stating, "Run-time error '1004': Method 'Add' of object 'Sheets' failed".. when I press Debug, .... the 3rd row here is highlighted, "Set Wks"

    Code:
              If R > Rows.Count Then
                 R = 1
                 Set Wks = Worksheets.Add(After:=Worksheets.Count) 'HIGHLIGHTED
              End If
    What is the problem here?

    Also, would it be possible rather than to place this in different sheets, to have this placed into different columns? - So fill column A with 65536 rows, then goto col B, etc... (with no delimiter, place entire row of text into one cell)... Thanks very much Leith, you are helping me out tremendously.

    Adam

  7. #7
    Forum Moderator shg's Avatar
    Join Date
    06-21-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2003, 2007
    Posts
    25,135

    Re: Excel 2003 - Importing text file > 65536 rows

    If all you need is the last value, why copy the file to the worksheet at all?
    Microsoft MVP - Excel
    Entia non sunt multiplicanda sine necessitate

  8. #8
    Registered User
    Join Date
    04-24-2008
    Posts
    63

    Re: Excel 2003 - Importing text file > 65536 rows

    Because we do this for a few hundred files, only about 40 of these files are now exceeding the excel row limit... could open them manually, but automating this would save time.

  9. #9
    Forum Moderator shg's Avatar
    Join Date
    06-21-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2003, 2007
    Posts
    25,135

    Re: Excel 2003 - Importing text file > 65536 rows

    Quote Originally Posted by AP
    ... as the only value in this entire file that I require is the the rightmost value in the very bottom row.
    Quote Originally Posted by shg
    If all you need is the last value, why copy the file to the worksheet at all?
    Quote Originally Posted by AP
    Because we do this for a few hundred files, only about 40 of these files are now exceeding the excel row limit... could open them manually, but automating this would save time.


    My question is, why read any file in at all, instead of just having code that reads to the bottom of the file and gets the one value you need?
    Microsoft MVP - Excel
    Entia non sunt multiplicanda sine necessitate

  10. #10
    Registered User
    Join Date
    04-24-2008
    Posts
    63

    Re: Excel 2003 - Importing text file > 65536 rows

    Hi shg,
    My inexperience with vba is what is causing this confusion...
    When I designed this process initially, I used the "record macro" feature, opened the file manually.. pressed ctrl+down, ctrl+right, copied value, closed the file...

    I now understand that you're saying that there is no need to open the file... this is correct, but I do not know how to do this... the value can be anywhere from 0 to several million and I just need this recorded in my master sheet... This is just a crude reconciliation process... can you let me know how this is done?

  11. #11
    Forum Moderator shg's Avatar
    Join Date
    06-21-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2003, 2007
    Posts
    25,135

    Re: Excel 2003 - Importing text file > 65536 rows

    So ... how about this:
    Code:
    Sub TheLastWord()
        Dim sFile       As String
        Dim iFF         As Integer
        Dim sLine       As String
        Dim asWd()      As String
    
        sFile = Application.GetOpenFilename(FileFilter:="Text Files, *.csv;*.txt", _
                                            Title:="Pick a file")
        If sFile = "False" Then Exit Sub
    
        iFF = FreeFile
    
        Open sFile For Input As #iFF
        Do Until EOF(iFF)
            Line Input #iFF, sLine
        Loop
        Close #iFF
        
        asWd = Split(sLine, ",")
        MsgBox Prompt:=asWd(UBound(asWd)), Title:="... and the last comma-delimited phrase is:"
    End Sub
    Microsoft MVP - Excel
    Entia non sunt multiplicanda sine necessitate

  12. #12
    Registered User
    Join Date
    04-24-2008
    Posts
    63

    Re: Excel 2003 - Importing text file > 65536 rows

    Thank you very much... I made some adjustments, but it works great! Better than before!

  13. #13
    Forum Moderator shg's Avatar
    Join Date
    06-21-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2003, 2007
    Posts
    25,135

    Re: Excel 2003 - Importing text file > 65536 rows

    You're welcome. A little simplication, and coded as a function:
    Code:
    Function TheLastWord() As String
        ' Returns the last comma-delimited item from a text file
        ' VBA function only
        Dim sFile       As String
        Dim iFF         As Integer
    
        sFile = Application.GetOpenFilename(FileFilter:="Text Files, *.csv;*.txt", _
                                            Title:="Pick a file")
        If sFile = "False" Then Exit Function
    
        iFF = FreeFile
    
        Open sFile For Input As #iFF
        Do Until EOF(iFF)
            Line Input #iFF, TheLastWord
        Loop
        Close #iFF
        
        TheLastWord = Mid(TheLastWord, InStrRev(TheLastWord, ",") + 1)
    End Function
    Last edited by shg; 10-23-2009 at 12:54 PM. Reason: typos
    Microsoft MVP - Excel
    Entia non sunt multiplicanda sine necessitate

Thread Information

Users Browsing this Thread

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

Tags for this Thread

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.2.0