+ Reply to Thread
Results 1 to 2 of 2
  1. #1
    Registered User
    Join Date
    06-25-2009
    Location
    Edmonton
    MS-Off Ver
    Excel 2003
    Posts
    1

    VBA Importing .tsv file to worksheet

    Hi, I am very new to computer programming of any kind. I am trying to be able to import .tsv files into an Excel worksheet. I want the imported file to always begin at the first empty column. I had successfully found a code that properly imported and split the .tsv file as well as a code that imported the file to the first empty column (but did not split the .tsv data). I tried to take what I could from both codes to create something functional, but failed. Here is the code that I have right now.

    Private Sub CommandButton1_Click()
    Dim FileName As Variant
    Dim Sep As String
    FileName = Application.GetOpenFilename(FileFilter:="Text File (*.tsv),*.tsv")
    If FileName = False Then
    ''''''''''''''''''''''''''
    ' user cancelled, get out
    ''''''''''''''''''''''''''
    Exit Sub
    End If
    Sep = Chr(9)
    If Sep = vbNullString Then
    ''''''''''''''''''''''''''
    ' user cancelled, get out
    ''''''''''''''''''''''''''
    Exit Sub
    End If
    Debug.Print "FileName: " & FileName, "Separator: " & Sep
    importtextfile FName:=CStr(FileName), Sep:=CStr(Sep)
    End Sub
    Public Sub importtextfile(FName As Variant, Sep As String)

    Dim RowNdx As Long
    Dim ColNdx As Integer
    Dim TempVal As Variant
    Dim WholeLine As String
    Dim Pos As Integer
    Dim NextPos As Integer
    Dim SaveColNdx As Integer
    Dim FirstEmpty&

    Application.ScreenUpdating = False
    On Error GoTo EndMacro:

    On Error GoTo IsBlankSheet
    FirstEmpty = Cells.Find("*", SearchOrder:=xlByColumns, LookIn:=xlValues, SearchDirection:=xlPrevious).Column + 1

    If FirstEmpty = 257 Then
    MsgBox "Sorry, no more columns on this sheet"
    Exit Sub
    End If

    SaveColNdx = FirstEmpty
    RowNdx = ActiveCell.Row


    While Not EOF(1)
    Line Input #1, WholeLine
    If Right(WholeLine, 1) <> Sep Then
    WholeLine = WholeLine & Sep
    End If
    ColNdx = SaveColNdx
    Pos = 1
    NextPos = InStr(Pos, WholeLine, Sep)
    While NextPos >= 1
    TempVal = Mid(WholeLine, Pos, NextPos - Pos)
    Cells(RowNdx, ColNdx).Value = TempVal
    Pos = NextPos + 1
    ColNdx = ColNdx + 1
    NextPos = InStr(Pos, WholeLine, Sep)
    Wend
    RowNdx = RowNdx + 1
    Wend

    EndMacro:
    On Error GoTo 0
    Application.ScreenUpdating = True
    Close #1
    ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
    ' END ImportTextFile
    ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
    IsBlankSheet:
    '//start in column 1
    FirstEmpty = 1
    '//clear the error & continue import


    End Sub

    Any help would be greatly appreciated. Thankyou
    Tim

    I tried to attatch a sample of a file that I am trying to work with, but it is not supported by this site.

  2. #2
    Forum Guru mudraker's Avatar
    Join Date
    11-10-2003
    Location
    Melbourne, Australia
    Posts
    3,984

    Re: VBA Importing .tsv file to worksheet

    Welcome to Exceltip forum

    Please take a couple of minutes and read ALL theForum Rules then wrap your VBA code (Rule 3)
    Please Read Forum Rules Before Posting
    Wrap VBA code by selecting the code and clicking the # icon or Read This
    How To Cross Post politely

    Top Excel links for beginners to Experts

    If you are pleased with a member's answer then use the Scales icon to rate it
    If my reply has assisted or failed to assist you I welcome your Feedback.

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