Forum

Transfer data from ...
 
Notifications
Clear all

Transfer data from text files as list in a worksheet

10 Posts
4 Users
0 Reactions
65 Views
(@sdgio)
Posts: 4
Active Member
Topic starter
 

Hello!

I'm newby to ms excel vba.

I want to read text files where are in a folder and the data of each file to transfer in a workbook/sheet as list.

The text files there are to C:tst.

The data of every text file has the following view:

UserName             

XXXXXXXXXXXXXX 

PC Name                       

YYYYYYYYYYYYYY 

SerialNumber 

ZZZZZZZZZZZZZZZ

 

and i want to view them to excel as:

 

Column 1              Column 2               Column 3

UserName            PC Name               SerialNumber

XXXXXXXXXXX     YYYYYYYYYYY        ZZZZZZZZZZZZZ

XXXXXXXXXXX     YYYYYYYYYYY        ZZZZZZZZZZZZZ

XXXXXXXXXXX     YYYYYYYYYYY       ZZZZZZZZZZZZZ

XXXXXXXXXXX     YYYYYYYYYYY       ZZZZZZZZZZZZZ

 

etc.

The only that I've done is the following, but, i receive to excel sheet, only the data of one text file at a time:

 Sub ImportFromText()

    Dim c As Integer, data As String

         Cells.ClearContents

    Open "C:tstFileName.txt" For Input As #1

        c = 0

    Do Until EOF(1)

        Line Input #1, data

        Worksheets("Sheet1").Range("A1").Offset(0, c) = data

        c = c + 1

    Loop

    Close #1

End Sub

 

and the result:

 

ώUserName                      XXXXXXXXXXXXXX        PC Name                                YYYYYYYYYYYYYY        SerialNumber          ZZZZZZZZZZZZZZZ

 

Could anybody help me please?

Thanks

Sotiris

 
Posted : 08/07/2017 4:01 am
(@sunnykow)
Posts: 1417
Noble Member
 

Hi Sotiris

You can give this a try.

Hope this helps.

Sunny

 
Posted : 08/07/2017 9:23 pm
(@sdgio)
Posts: 4
Active Member
Topic starter
 

Hi Sunny!

Thank you very much for your help.
This is exactly the format that I want.
The next step that I want is the "macro" to read all the files from the folder and not only one as does now. I think that must add a variable that read every file of the folder and write the data as you have make the code. Imagine that the folder has 300 files. That was my aknowlegde problem with the programming.
Is something that you can help me?

Thank you for another time.

 
Posted : 11/07/2017 2:35 am
(@sunnykow)
Posts: 1417
Noble Member
 

Give this a try.

Good luck

Sunny

 
Posted : 11/07/2017 4:01 am
(@sdgio)
Posts: 4
Active Member
Topic starter
 

Hi Sunny.

I tried your new file, was o.k but i added something (as you 'll see) which complete the code and correct more good its viewing.
I hope that you 'll agree.

Thank you very much.

Sotiris.

 
Posted : 11/07/2017 9:14 am
(@sunnykow)
Posts: 1417
Noble Member
 

Hi Sotiris

Glad it is working for you.

To add on to your codes, just move the line  

Application.ScreenUpdating = True

To the bottom of your code (just before the End Sub)

It will improve the speed tremendously.

Sunny

 
Posted : 11/07/2017 7:09 pm
(@sdgio)
Posts: 4
Active Member
Topic starter
 

Many Thanks!

 
Posted : 12/07/2017 8:33 am
(@kingtamo)
Posts: 13
Active Member
 

Hello

In the code provided I see that using Select is unnecessary so instead of these three lines

Rows(delRow).Select
Application.CutCopyMode = False
Selection.Delete

You can use directly

Rows(delRow).Delete
 
Posted : 04/08/2017 4:11 pm
(@morgan1974)
Posts: 1
New Member
 

Excellent! Thanks for the code SunnyKow!

 
Posted : 17/08/2017 3:09 am
(@sunnykow)
Posts: 1417
Noble Member
 

Hi Morgan

You are welcome Smile.

Sunny

 
Posted : 17/08/2017 4:15 am
Share: