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.
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 assistedor failed to assist you
I welcome your Feedback.
There are currently 1 users browsing this thread. (0 members and 1 guests)
Bookmarks