PDA

View Full Version : Importing files will format data



Einsener37
05-11-2020, 02:38 PM
Hi!
I found code from https://www.extendoffice.com/documents/excel/3645-excel-import-multiple-text-files-into-one-worksheet.html?fbclid=IwAR2h2ElvSlR-j36TvXCaV9HfvgZ2Lf9JUzgmizxGAaydGnY2pq2oCfO0GqM
It imports txt files as i wish into Excel. But it formats all the comma separated data into something else. Ex. 1,200000 to 1200000.
Do i have to add some restriction?
here is the code:

Sub Test()
'UpdatebyExtendoffice6/7/2016
Dim xWb As Workbook
Dim xToBook As Workbook
Dim xStrPath As String
Dim xFileDialog As FileDialog
Dim xFile As String
Dim xFiles As New Collection
Dim I As Long
Set xFileDialog = Application.FileDialog(msoFileDialogFolderPicker)
xFileDialog.AllowMultiSelect = False
xFileDialog.Title = "Select a folder [Kutools for Excel]"
If xFileDialog.Show = -1 Then
xStrPath = xFileDialog.SelectedItems(1)
End If
If xStrPath = "" Then Exit Sub
If Right(xStrPath, 1) <> "\" Then xStrPath = xStrPath & "\"
xFile = Dir(xStrPath & "*")
If xFile = "" Then
MsgBox "No files found", vbInformation, "Kutools for Excel"
Exit Sub
End If
Do While xFile <> ""
xFiles.Add xFile, xFile
xFile = Dir()
Loop
Set xToBook = ThisWorkbook
If xFiles.Count > 0 Then
For I = 1 To xFiles.Count
Set xWb = Workbooks.Open(xStrPath & xFiles.Item(I))
xWb.Worksheets(1).Copy after:=xToBook.Sheets(xToBook.Sheets.Count)
On Error Resume Next
ActiveSheet.Name = xWb.Name
On Error GoTo 0
xWb.Close False
Next
End If
End Sub

Any suggestions?
Thanks!

paulked
05-11-2020, 03:30 PM
Post a copy of the file you are importing.

Einsener37
05-12-2020, 07:20 AM
It keeps telling me that txt file is not valid, and gives error
So i try to copy some data to here

Time FORCE 2 (AI04) POSITION 2 (AI03)
s kN mm
0,000000 0,935648 -0,042776
0,100000 0,950463 -0,019039
0,200000 0,920833 -0,019039
0,300000 0,950463 -0,019039
0,400000 0,920833 -0,019039
0,500000 0,906019 -0,011127
0,600000 0,920833 -0,034864
0,700000 0,950463 -0,042776
0,800000 0,906019 -0,019039
0,900000 0,935648 -0,011127
1,000000 0,920833 -0,026951
1,100000 0,935648 -0,034864
1,200000 0,920833 -0,050688
1,300000 0,950463 -0,026951
1,400000 0,950463 -0,019039
1,500000 0,920833 -0,019039
1,600000 0,935648 -0,019039
1,700000 0,935648 -0,019039
1,800000 0,965278 -0,034864
1,900000 0,935648 -0,034864
2,000000 0,920833 -0,026951
2,100000 0,891204 -0,019039
2,200000 0,965278 -0,042776

Paul_Hossler
05-12-2020, 08:06 AM
What were you expecting?

The TXT file from your data imports and looks like this which seems what the macro intends

26643

Einsener37
05-12-2020, 08:12 AM
In my excel it shows like that



Time
FORCE 1 (AI02)
POSITION 1 (AI01)
POSITION 1 (zeroed)


s
kN
mm
mm



0,000000
1 134 722
-0,053645
-0,041284



0,100000
1 119 907
-0,061556
-0,049195



0,200000
1 134 722
-0,045734
-0,033374



0,300000
1 119 907
-0,053645
-0,041284



0,400000
1 149 537
-0,069466
-0,057106



0,500000
1 134 722
-0,069466
-0,057106



0,600000
1 090 278
-0,061556
-0,049195



0,700000
1 208 796
-0,053645
-0,041284



0,800000
1 312 500
-0,037823
-0,025463



0,900000
1 401 389
-0,014091
-0,001730



1 000 000
1 445 833
0,009641
0,022002



1 100 000
1 445 833
0,001730
0,014091



1 200 000
1 505 093
0,017552
0,029913



1 300 000
1 519 907
0,017552
0,029913



1 400 000
1 549 537
0,025463
0,037823



1 500 000
1 505 093
0,033374
0,045734



1 600 000
1 505 093
0,033374
0,045734



1 700 000
1 519 907
0,025463
0,037823

Paul_Hossler
05-12-2020, 09:50 AM
That's a different looking file (or files)

What is the delimitator in the files? From your posted sample, I assumed a space

If it was a tab (for exxample) that would make a difference

Take the txt file and just rename it as .zip and you should be able to attach it