Bogdan
08-30-2013, 02:53 PM
Hello,
I saw some post's on this subject but unfortunately my VBA knowledge is rudimentary.
So below I have a macro that imports a tab delimited text file in excel
Public Sub ImportTextFile(FName As String, 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
Application.ScreenUpdating = False
SaveColNdx = ActiveCell.Column
RowNdx = ActiveCell.Row
Open FName For Input Access Read As #1
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 Sub
Sub DoTheImport()
Dim Position As Variant
Dim Separator As String
Dim FileName As Variant
Dim Sep As String
FileName = Application.GetOpenFilename(FileFilter:="Text File (*.txt),*.txt")
If FileName = False Then
Exit Sub
End If
Separator = vbTab
Debug.Print "FileName: " & FileName, "Separator: " & Sep
ImportTextFile FName:=CStr(FileName), Sep:=Separator
End Sub
the problem is that i need only certain rows/columns.
At the the moment i do the formatting manually and would really appreciate some help with adding these features to the macro, or creating a new one.
For Rows I was thinking about 2 solutions:
1. To determine by user input the 1st and last row needed, then get only the data in that range.
2. To read from an Excel sheet the required values, search them and if found copy the entire row. (This would be preferred)
For Columns it's a lot easier as i only need columns B,C,F,G out of columns A to H. If it can be added in the code very well, if not i can do it manually.
Thanks.
I saw some post's on this subject but unfortunately my VBA knowledge is rudimentary.
So below I have a macro that imports a tab delimited text file in excel
Public Sub ImportTextFile(FName As String, 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
Application.ScreenUpdating = False
SaveColNdx = ActiveCell.Column
RowNdx = ActiveCell.Row
Open FName For Input Access Read As #1
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 Sub
Sub DoTheImport()
Dim Position As Variant
Dim Separator As String
Dim FileName As Variant
Dim Sep As String
FileName = Application.GetOpenFilename(FileFilter:="Text File (*.txt),*.txt")
If FileName = False Then
Exit Sub
End If
Separator = vbTab
Debug.Print "FileName: " & FileName, "Separator: " & Sep
ImportTextFile FName:=CStr(FileName), Sep:=Separator
End Sub
the problem is that i need only certain rows/columns.
At the the moment i do the formatting manually and would really appreciate some help with adding these features to the macro, or creating a new one.
For Rows I was thinking about 2 solutions:
1. To determine by user input the 1st and last row needed, then get only the data in that range.
2. To read from an Excel sheet the required values, search them and if found copy the entire row. (This would be preferred)
For Columns it's a lot easier as i only need columns B,C,F,G out of columns A to H. If it can be added in the code very well, if not i can do it manually.
Thanks.