VBA Excel : Upload data from the txt to spreadsheet

Let say there is a txt file with next content  :

txt #1 :

54.46      54.46      54.46      54.46      1137      5/29/2015 9:30
54.42      54.49      54.38      54.48      196268      5/29/2015 9:31
54.44      54.45      54.39      54.43      29330      5/29/2015 9:32
54.4      54.47      54.38      54.44      30679      5/29/2015 9:33
54.415      54.42      54.36      54.38      25518      5/29/2015 9:34
54.38      54.415      54.34      54.41      39001      5/29/2015 9:35
54.32      54.38      54.3      54.37      54390      5/29/2015 9:36
54.33      54.37      54.32      54.32      29377      5/29/2015 9:37
54.355      54.369      54.32      54.3399      45962      5/29/2015 9:38
54.35      54.37      54.34      54.36      47212      5/29/2015 9:39
54.35      54.37      54.33      54.35      42659      5/29/2015 9:40
54.35      54.365      54.3348      54.35      28713      5/29/2015 9:41

I need to upload this data to excel spreadsheet. in order that  every line would be divided on 6 row (from A to F)
Txt file that excel VBA sheet lay in a same folder.
SunnyXAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Qlemo"Batchelor", Developer and EE Topic AdvisorCommented:
To be clear: You want a programmatic solution? Nothing you can do manually only? For the latter, using the Import Wizard would be sufficient.
ProfessorJimJamMicrosoft Excel ExpertCommented:
Import wizard is much user friendly
SunnyXAuthor Commented:
Nothing you can do manually only?

This is the part of the application that eventually will manage big db. There are no way to make it manually because after uploading I expect to do some automatic manipulation with data.
Exploring SQL Server 2016: Fundamentals

Learn the fundamentals of Microsoft SQL Server, a relational database management system that stores and retrieves data when requested by other software applications.

Qlemo"Batchelor", Developer and EE Topic AdvisorCommented:
How about running this after loading the text file into the sheet:
    Selection.TextToColumns Destination:=Range("A1"), DataType:=xlDelimited, _
        TextQualifier:=xlNone, ConsecutiveDelimiter:=True, Tab:=False, Semicolon _
        :=False, Comma:=False, Space:=True, Other:=False, FieldInfo:=Array( _
        Array(1, 1), Array(2, 9), Array(3, 9), Array(4, 1), Array(5, 9), Array(6, 9), Array(7, 1), _
        Array(8, 9), Array(9, 9), Array(10, 1), Array(11, 9), Array(12, 9), Array(13, 1), Array(14, _
        9), Array(15, 9), Array(16, 1), Array(17, 1)), TrailingMinusNumbers:=True

Open in new window

Martin LissOlder than dirtCommented:
Try this macro. It assumes that the file name and/or path can change and it asks you to find the file. If they are always the same then the file name and path can be hard-coded.

Sub ImportFile()
Dim fd As FileDialog
Dim strFile As String
Dim FF As Integer
Dim strLine As String
Dim strParts() As String
Dim lngNewRow As Long
Dim intLength As Integer
Dim intParts As Integer

Set fd = Application.FileDialog(msoFileDialogFilePicker)

With fd
    .Filters.Clear
    .Filters.Add "Text files", "*.txt"
    .Filters.Add "All files", "*.*"
    If .Show = -1 Then
        strFile = .SelectedItems(1)
    Else
        MsgBox "Cancel selected"
        Exit Sub
    End If
End With
Set fd = Nothing

FF = FreeFile
Open strFile For Input As #FF

Do While Not EOF(FF)
    Line Input #FF, strLine
    ' Replace all multiple spaces with single spaces
    intLength = 0
    Do Until intLength = Len(strLine)
        intLength = Len(strLine)
        strLine = Replace(strLine, "  ", " ")
    Loop
    strParts = Split(strLine, " ")
    lngNewRow = lngNewRow + 1
    With ActiveSheet
        For intParts = 0 To UBound(strParts) - 1
            If intParts = UBound(strParts) - 1 Then
                ' Combine Date and Time
                .Cells(lngNewRow, intParts + 1) = strParts(intParts) & " " & strParts(intParts + 1)
            Else
                .Cells(lngNewRow, intParts + 1) = strParts(intParts)
            End If
        Next
    End With
Loop
Close
End Sub

Open in new window

SunnyXAuthor Commented:
@Qlemo
Compile error :
Invalid outside procedure

@Martin Liss
Yes, indeed this code upload my data from the file but in upload it in one single row (instead of 6 ) and upload data without any spaces between data elements. So, in the sheet,  it look likes.

"96.4496.4596.32596.36126665/29/2015 10:12"

Looking forward for reply from you dear experts.
Martin LissOlder than dirtCommented:
In your question you showed data with several spaces between each value.  Can you show what one of the rows really looks like?
SunnyXAuthor Commented:
Yes sorry this is my fault. In original files data rows separated my tabulation (tab on keyboard )

Can you show what one of the rows really looks like?
Sure, please take a look in attachment.
ABT.txt
UploadTxtDataApp.xlsx
Martin LissOlder than dirtCommented:
Sub ImportFile()
Dim fd As FileDialog
Dim strFile As String
Dim FF As Integer
Dim strLine As String
Dim strParts() As String
Dim lngNewRow As Long
Dim intParts As Integer

Set fd = Application.FileDialog(msoFileDialogFilePicker)

With fd
    .Filters.Clear
    .Filters.Add "Text files", "*.txt"
    .Filters.Add "All files", "*.*"
    If .Show = -1 Then
        strFile = .SelectedItems(1)
    Else
        MsgBox "Cancel selected"
        Exit Sub
    End If
End With
Set fd = Nothing

FF = FreeFile
Open strFile For Input As #FF

Do While Not EOF(FF)
    Line Input #FF, strLine
    strParts = Split(strLine, vbTab)
    lngNewRow = lngNewRow + 1
    With ActiveSheet
        For intParts = 0 To UBound(strParts)
            .Cells(lngNewRow, intParts + 1) = strParts(intParts)
        Next
    End With
Loop
Close
End Sub

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
SunnyXAuthor Commented:
thx you so much !
Martin LissOlder than dirtCommented:
You're welcome and I'm glad I was able to help.

In my profile you'll find links to some articles I've written that may interest you.
Marty - MVP 2009 to 2015
SunnyXAuthor Commented:
Please Martin take a look on my another question. About removing duplication in spreadsheet.

http://www.experts-exchange.com/questions/28691223/VBA-Excel-removing-duplication-in-database.html

Many thanks in advance !
Qlemo"Batchelor", Developer and EE Topic AdvisorCommented:
Sorry, should have told you that you need to put my code into a "sub".
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
VB Script

From novice to tech pro — start learning today.