Import data from multiple big .txt (around 1.5 Mb) into multiple .xlsx

TheShredder

New Member
Joined
Feb 1, 2018
Messages
3
Hi guys,
I'm currently a newbie to VBA macro scripting. While working I need to find a way to import data from a text file to an excel worksheet, and a way to automatize it for a list of .txt into a list of .xlsx . Found a sweet code here http://www.cpearson.com/excel/ImportBigFiles.aspx but it cannot detect that into the .txt data are delimited: so I'm not able to separate them with the default setting for import.
<code>
Delimited
Delimiters : TAB
Column Data format : General
Where do you want to put the data ? Existing worksheet: =$A$1
</code>

Here's the example of what I get manually and what with the code above.

Manually.
https://imgur.com/a/4C5AD

With the script.
https://imgur.com/a/Tefzs

So, in order to resume, I'm looking mainly to a way to get the text properly formatted and without random characters added (+ or _ ).
After that, I'll try something to automatize the process to a list of .txt into a proper folder.

Every tip or suggestion are well accepted.
Thanks for the attention.
4C5AD
 

Excel Facts

Remove leading & trailing spaces
Save as CSV to remove all leading and trailing spaces. It is faster than using TRIM().
so what is your file? Delimited, not delimited, fixed?


My files are delimited by tab. I've solved using python and library xlwt and xlrd. I know it doesn't support .xlsx (infact new files are generated with .xls extension, but for me doesn't matter at all). Here's the code.

<code>
!# /usr/bin/env/python

mypath = raw_input ("Enter the directory ..")

from os import listdir
from os.path import isfile, join
textfiles = [ join(mypath,f) for f in listdir(mypath) if isfile(join(mypath,f)) and '.txt' in f]


def is_number(s):
try:
float(s)
return True
except ValueError:
return False


import xlwt
import xlrd


style = xlwt.XFStyle()
style.num_format_str = '#,###0.00'


for textfile in textfiles:
f = open(textfile, 'r+')
row_list = []
for row in f:
row_list.append(row.split('\t'))
column_list = zip(*row_list)
workbook = xlwt.Workbook()
worksheet = workbook.add_sheet('Sheet1')
i = 0
for column in column_list:
for item in range(len(column)):
value = column[item].strip()
if is_number(value):
worksheet.write(item, i, float(value), style=style)
else:
worksheet.write(item, i, value)
i+=1
workbook.save(textfile.replace('.txt', '.xlsx'))

</code>
 
Upvote 0
Have you tried import via text:
menu: Data tab, From Text, select the file, then set TAB DELIMITED
 
Upvote 0

Forum statistics

Threads
1,223,893
Messages
6,175,240
Members
452,621
Latest member
Laura_PinksBTHFT

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top