Results 1 to 4 of 4

Thread: Import a text file into an excel spreadsheet using VBA

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Nov 2001
    Posts
    1,384

    Import a text file into an excel spreadsheet using VBA

    Hi all,

    I want to import the contents of a text file into an Excel Sheet. I am using the following VBA code:
    VB Code:
    1. Path = Workbooks("NITBSum_Macro.xls").Worksheets("Network").Cells(4, 2).Value
    2.     Workbooks.OpenText FileName:=Path & "JT_Insurance_Report.txt", Origin:= _
    3.         xlWindows, StartRow:=1, DataType:=xlDelimited, TextQualifier:= _
    4.         xlDoubleQuote, ConsecutiveDelimiter:=False, Tab:=True, Semicolon:=False, _
    5.         Comma:=False, Space:=False, Other:=True, OtherChar:="|", FieldInfo:= _
    6.         Array(Array(1, 1), Array(2, 1), Array(3, 1), Array(4, 1), Array(5, 1), Array(6, 1), Array(7 _
    7.         , 1), Array(8, 1), Array(9, 1), Array(10, 1), Array(11, 1), Array(12, 1), Array(13, 1), Array _
    8.         (14, 1), Array(15, 1))
    9.     Cells(1, 1).Select
    10.     Selection.EntireRow.Insert
    This is working fine for most text files, however I have one file that has more than 100,000 lines, which exceeds the maximum allowed 65,000 lines in an excel document.

    So my question is:
    1. How can I get it to automatically create a second excel file with the remaining contents of the text file? At the moment it just returns an error when it reaches 65,000 lines.

    Thanks,
    Mel

  2. #2
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: Import a text file into an excel spreadsheet using VBA

    Moved to Office Development

  3. #3
    Fanatic Member Comintern's Avatar
    Join Date
    Nov 2004
    Location
    Lincoln, NE
    Posts
    826

    Re: Import a text file into an excel spreadsheet using VBA

    You'll probably have to read and parse the file manually, so you can keep track of what line you are writing to. I dug up one of my import modules for a long csv report I use at work to give you a starting point:
    VB Code:
    1. Private Function TXReader() As Boolean
    2.  
    3.     Dim sFileName As String, sTrans As String, sSheetName As String, iPage As Integer, iFile As Integer
    4.     Dim lLineNum As Long, oBook As Workbook, oSheet As Worksheet, sValues() As String, iCount As Integer
    5.    
    6.     On Error GoTo ErrHand
    7.    
    8.     sFileName = Application.GetOpenFilename("Report Files (*.prt),*.prt,All Files (*.*),*.*", 1, _
    9.                                             "Select TX Report to Open")
    10.     If sFileName <> "False" Then
    11.         iFile = FreeFile
    12.         lLineNum = 2
    13.         iPage = 1
    14.        
    15.         Application.ScreenUpdating = False
    16.         Set oBook = Application.ActiveWorkbook
    17.         sSheetName = ParseName(sFileName) 'Custom function, strips the path.
    18.         Open sFileName For Input As #iFile
    19.         Set oSheet = Application.ActiveSheet
    20.         oSheet.Name = sSheetName
    21.        
    22.         Do Until EOF(iFile)
    23.             Line Input #iFile, sTrans
    24.             'Replace with whatever you need to do to parse and write the line.
    25.             sValues = Split(sTrans, ",")
    26.             For iCount = 0 To UBound(sValues)
    27.                 oSheet.Cells(lLineNum, iCount + 1) = sValues(iCount)
    28.             Next iCount
    29.             'End replace.
    30.             lLineNum = lLineNum + 1
    31.             If lLineNum > 65000 Then
    32.                 Set oSheet = oBook.Worksheets.Add
    33.                 iPage = iPage + 1
    34.                 oSheet.Name = sSheetName & " Page " & CStr(iPage)
    35.                 lLineNum = 2
    36.             End If
    37.         Loop
    38.        
    39.         If iPage > 1 Then
    40.             Call MsgBox("Input file was too long -- Worksheets added.")
    41.         End If
    42.        
    43.         TXReader = True
    44.     End If
    45.    
    46. ErrHand:
    47.     Application.ScreenUpdating = True
    48.     If Err.Number <> 0 Then
    49.         MsgBox "Error number " & Err.Number & ":" & vbCrLf & Err.Description & vbCrLf & "In module TXReader."
    50.         Err.Clear
    51.     End If
    52.  
    53. End Function

  4. #4

    Thread Starter
    Frenzied Member
    Join Date
    Nov 2001
    Posts
    1,384

    Re: Import a text file into an excel spreadsheet using VBA

    Thanks, will give me a start anyway.
    Mel

Posting Permissions

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



Click Here to Expand Forum to Full Width