Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Microsoft Office > Excel VBA > Excel VBA
Excel VBA Discuss using VBA for Excel programming.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Excel VBA section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
Old August 4th, 2007, 05:54 AM
Registered User
Join Date: Aug 2007
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default Text Import- How to import the first 5 lines.

Hi all,

This is the code which can import all the data from a text file to Excel 2003. I am trying to only import the first 10 lines, it will be very appreciated if someone can help.

Sub Import()
    'ImportTextFile FName:="C:\Nick\1.txt", Sep:=" "
    'ImportRangeFromDelimitedText SourceFile = "C:\Nick\1.txt", SepChar = " ", TargetWB = ThisWorkbook.Name, TargetWS = "ImportSheet", TargetAddress = "A3"
    'ImportRangeFromDelimitedText "C:\Nick\1.txt", _ " ; ", ThisWorkbook.Name, "ImportSheet", "A3"
    ImportRangeFromDelimitedText "C:\Nick\1.txt", _
    " ", ThisWorkbook.Name, "ImportSheet", "A1"
End Sub
Sub ImportRangeFromDelimitedText(SourceFile As String, SepChar As String, _
    TargetWB As String, TargetWS As String, TargetAddress As String)
' Imports the data separated by SepChar in SourceFile to
' Workbooks(TargetWB).Worksheets(TargetWS).Range(Tar getAddress)
' Replaces existing data in Workbooks(TargetWB).Worksheets(TargetWS)
' without prompting for confirmation
' Example:
' ImportRangeFromDelimitedText "C:\FolderName\DelimitedText.txt", _
    ";", ThisWorkbook.Name, "ImportSheet", "A3"

Dim SC As String * 1, TargetCell As Range, TargetValues As Variant
Dim r As Long, fLen As Long
Dim fn As Integer, LineString As String
Dim coll As Variant

    ' validate the input data if necessary
    If Dir(SourceFile) = "" Then Exit Sub ' SourceFile doesn't exist
    If UCase(SepChar) = "TAB" Or UCase(SepChar) = "T" Then
        SC = Chr(9)
        SC = Left(SepChar, 1)
    End If

    ' perform import
    Set TargetCell = Range(TargetAddress).Cells(1, 1)
    On Error GoTo NotAbleToImport
    fn = FreeFile
    Open SourceFile For Input As #fn
    On Error GoTo 0
    fLen = LOF(fn)
     r = 0
    While Not EOF(fn)
        Line Input #fn, LineString

        TargetValues = Split(LineString, SC, -1, vbBinaryCompare) ' Excel 2000 or later
        UpdateCells TargetCell.Offset(r, 0), TargetValues
        r = r + 1
    Close #fn
    Application.Calculation = xlCalculationAutomatic

    ' clean up
    Set TargetCell = Nothing
    Application.StatusBar = False
End Sub

Sub UpdateCells(TargetRange As Range, TargetValues As Variant)
' Writes the content of the variable TargetValues to
' the active worksheet range starting at TargetRange
' Replaces existing data in TargetRange without prompting for confirmation
Dim r As Long, c As Integer
    If TypeName(ActiveSheet) <> "Worksheet" Then Exit Sub
    r = 1
    c = 1
    On Error Resume Next
    c = UBound(TargetValues, 1)
    r = UBound(TargetValues, 2)
    Range(TargetRange.Cells(1, 1), _
        TargetRange.Cells(1, 1).Offset(r - 1, c - 1)).Formula = TargetValues
    On Error GoTo 0
End Sub


Old August 4th, 2007, 05:06 PM
Registered User
Join Date: Aug 2007
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts

And I found some tips, but I cant use it properly, can anyone help? Thank you very much!

" If you want to read a known count of lines from the start or within a text file, use a variable to store the line count you have read:
Set coll = New Collection
i = 0
Do While Not EOF(fn) And i < 10
Line Input #fn, coll.Add(strLine) ' add line to a collection
i = i + 1


Similar Threads
Thread Thread Starter Forum Replies Last Post
Text import - Browse for location djgenesis Excel VBA 2 August 2nd, 2007 05:18 AM
How to import data from text file? starnet SQL Server 2000 1 September 18th, 2006 06:56 AM
Excel IMport Text Help!! money Excel VBA 2 April 30th, 2004 12:43 AM
Excel text Import and VB Franck Excel VBA 0 January 8th, 2004 09:57 AM
Text import - an extra bit Morris Access 6 August 11th, 2003 12:51 PM

Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.