Results 1 to 20 of 22

Thread: Create Import Wizard for Text Files

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #11
    Knowledge Base Approver
    The King of Overkill!
    VBAX Master
    Joined
    Jul 2004
    Location
    Rochester, NY
    Posts
    1,727
    Location
    OK, I changed your ImportTextLines sub to ImportTextFile. Also, I added (and commented out) a line which will restrict the Sep variable to be only 1 character long. I also changed the file number to be a variable, in the rare instance that file number 1 is already being used.
    Give these a try:

    [vba]Public Sub DoTheImport()
    Dim FName As Variant
    Dim Sep As String
    FName = Application.GetOpenFilename _
    (filefilter:="Text Files(*.txt),*.txt,All Files (*.*),*.*")
    If FName = False Then
    Msgbox "You didn't select a file"
    Exit Sub
    End If
    Redo:
    Sep = InputBox("Enter a single delimiter character.", _
    "Import Text File")
    ' If Len(Sep) <> 1 Then msgbox "Invalid delimiter length.": GoTo Redo
    If Sep = "" Then Exit Sub

    ImportTextfile CStr(FName), Sep
    End Sub
    Sub ImportTextfile(FName As String, Sep As String)
    Dim RowNdx As Integer
    Dim ColNdx As Integer
    Dim WholeLine As String
    Dim FileNum As Integer
    Application.ScreenUpdating = False
    ColNdx = ActiveCell.Column
    RowNdx = ActiveCell.Row
    FileNum = FreeFile()
    Open FName For Input Access Read As #FileNum
    While Not EOF(FileNum)
    Line Input #FileNum, WholeLine
    While WholeLine Like "*" & Sep & "*"
    Cells(RowNdx, ColNdx) = Left(WholeLine, InStr(1, WholeLine, Sep, 1) - 1)
    WholeLine = Mid(WholeLine, InStr(1, WholeLine, Sep, 1) + Len(Sep))
    ColNdx = ColNdx + 1
    Wend
    Cells(RowNdx, ColNdx) = WholeLine
    RowNdx = RowNdx + 1
    ColNdx = 1
    Wend
    Close #FileNum
    End Sub[/vba]

    Matt
    Last edited by mvidas; 09-17-2004 at 10:14 AM. Reason: Changed commented line, added error checking for blank/cancel delimiter

Posting Permissions

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