• Hi All

    Please note that at the Chandoo.org Forums there is Zero Tolerance to Spam

    Post Spam and you Will Be Deleted as a User

    Hui...

  • When starting a new post, to receive a quicker and more targeted answer, Please include a sample file in the initial post.

importing txt to excel [SOLVED]

coolkiran

Member
Hello to all


I am stuck with one part of my application.


Basically i need to import a .txt file to Excel. and some calculation i need to do.


Currently, my .txt file is in the following format.


A

1

2

---------------------

B

3

4


I need output in Excel like :

A 1 2

------------------

B 3 4


Any help.
 
Hi, coolkiran!


First of all welcome to Chandoo's website Excel forums. Thank you for your joining us and glad to have you here.


As a starting point I'd recommend you to read the green sticky topics at this forums main page. There you'll find general guidelines about how this site and community operates (introducing yourself, posting files, netiquette rules, and so on).


Among them you're prompted to perform searches within this site before posting, because maybe your question had been answered yet.


Feel free to play with different keywords so as to be led thru a wide variety of articles and posts, and if you don't find anything that solves your problem or guides you towards a solution, you'll always be welcome back here. Tell us what you've done, consider uploading a sample file as recommended, and somebody surely will read your post and help you.


And about questions in general...


If you haven't performed yet the search herein, try going to the topmost right zone of this page (Custom Search), type the keywords used in Tags field when creating the topic or other proper words and press Search button. You'd retrieve many links from this website, like the following one(s) -if any posted below-, maybe you find useful information and even the solution. If not please advise so as people who read it could get back to you as soon as possible.


And about this question in particular...


Assuming your data is in range A1:B2 in worksheet Sheet1, in worksheet Sheet2 type this in A1 and copy down and across as required (in the example given thru A1:B2):

=INDICE(Hoja1!$A:$Z;COLUMNA();FILA()) -----> in english: =INDEX(Hoja1!$A:$Z,COLUMN(),ROW())

or:

=TRANSPONER(Hoja1!$A$1:$B$2) -----> in english: =TRANSPOSE(Hoja1!$A$1:$B$2)


If that doesn't reflect your case, consider uploading a sample file (including manual examples of desired output if applicable), it'd be very useful for those who read this and might be able to help you. Thank you. Give a look at the green sticky posts at this forums main page for uploading guidelines.


Regards!
 
I am sorry, i was bit urgent to complete this application, another 24 hours TAT remaining. I have just uploaded my test file, which is in .txt format.


http://www.mediafire.com/?p9e45sbqyq5v225


I need this file to import via, excel and merge three rows to one.
 
Hi !


It's Ok now with the txt file to import but if it's really urgent,

take the time to explain the output to Excel !


For Example, A 1 2 : is it in one cell or each data in several cells on the same line ?


Also, is it necessary to keep a line "--------" between each data line in the worksheet ?
 
Hi, coolkiran!

A doubt about your uploaded text file. 2, actually.

a) The data comes in a unique column? Letters are column indicators or the first element of each new generated row? Dashes should remain?

b) Would you mind uploading the Excel worksheet that you expect to get, if it's easier than answer what asked in a)?

Regards!

PS: I didn't see Marc L's comment but I think we agree regarding the accuracy of your specs.
 
Basically i need to import a .txt file to Excel. and some calculation i need to do.

'Cause of calculation this is a one cell equal one data example :

[pre]
Code:
Sub DemonstrationSpeedImportOddTxt()
Const F$ = "D:Teststest.txt"

R% = FreeFile
Open F For Input As #R
T = Split(Input(LOF(R), #R), vbCrLf)
Close #R

L& = UBound(T) / 3
ReDim AR(1 To L, 1 To 1)
R = 0

For Each V In T
If V > "" And Left$(V, 1) <> "-" Then
If IsNumeric(V) Then
C% = C% + 1
If C > UBound(AR, 2) Then ReDim Preserve AR(1 To L, 1 To C)

Else
R = R + 1
C = 1
End If

AR(R, C) = V
End If
Next

Application.ScreenUpdating = False
Columns("A:" & Chr$(64 + UBound(AR, 2))).ClearContents
[A1].Resize(R, UBound(AR, 2)) = AR
Application.ScreenUpdating = True
End Sub
[/pre]

Enjoy ‼
 
Thanks Marc L, i am getting one error when i added the code.


I will start again with the my thread.


I have a .txt file and a excel workbook. I wanted to import my .txt file through Excel. and wanted to split data based on column name.


Please check the attachment for .txt that i am receiving from my client and excel that i have created.


http://www.mediafire.com/?gzuyprc8kr5d9f9

http://www.mediafire.com/?5ji9zbo44aoqfo2


Let me know for any queries
 
Good day coolkiran


OK I must be missing some thing but I download one of your files, imported into Excel and used text to columns and like I said I must be reading it all wrong because I think I achieved what you want. Please have a look at the file in Dropbox.


https://dl.dropboxusercontent.com/u/75495784/Coolkiran.xlsm
 
Thanks bobhc, Yes i got it. But i need the output like in Excel. I need macro to do that task, because it will be daily task for my friend.


In single row i need all information for single person.


Let me know.
 
Sorry Marc. I just tried to upload my original file. Both had similar type of data, but my original file have some extra columns. May be that is the error.


I have attached original txt file on my previous post.


Please assist to solve this for original txt.
 
Hi, coolkiran!

After viewing your new updated file layout, please discard my first post.

Regards!


@Marc L

Hi!

Maybe you'd find a slight difference between the original layout and the uploaded text file.

Regards!

PS: Très légère, je dois dire (veeery slight, I should say) :)
 
Yes of course !


It was juste for saying my code is functional with the first txt file …


What a waste of time !
 
@ Marc L


It is annoying when members take time to answer post and then find out that the OP has not thought through their question and keep moving the goal posts.


I think that the problem is that "newbies" are so unsure of excel they have trouble in conveying their questions.


The really annoying bit for forum members is when "old hands" are sloppy in posting questions and keep adding to or altering their question, I have seen members and seasoned Ninjas get quite frustrated with poor post from long time members.
 
Sorry Marc and bobhc. I know its annoying. Problem is i am not able post my original txt, because it had some confidential data. So i modified and send after removing address and name.


I know bit of macro, so i thought to merge 3 rows to one will solve my issue.


The code Marc sent was working perfectly for each row has one cell, but now i have more than 3 columns.


Sorry for the confusion. You guys are really helping.
 
Hi Kiran ,


You have hit the nail on the head ! The problem lies in this one sentence :


I know bit of macro, so i thought to merge 3 rows to one will solve my issue.


Quite a few people who post questions here , have thought through their problem a little , and probably have some idea of what the solution should look like ; a small obstacle may be preventing them from reaching their goal , and they think others can help them do it.


This is not the right way to approach problem-solving ; when solving a problem , never assume that there is only one way to get to the solution ; there may be many ways ; there may even be more than one most efficient or optimal solution.


The job of the person who is posting a problem should be to explain the objective in the clearest way possible , give as much input data as possible , give the desired output and clearly explain how the output is to be obtained from the input.


It should be left to the person who is answering the question to decide on the approach ; if the person posting the question has any constraints , they should be mentioned ; some may still be using Excel 2003 , some may be using Excel on the Mac , some may not be in a position to use VBA or helper cells / columns ....


If such a structured way of problem specification is followed ( which needs a lot of effort and words ! ) , answers will come very quickly.


Narayan
 
Hi, coolkiran!


Give a look at this file:

https://dl.dropboxusercontent.com/u/60558749/importing%20txt%20to%20excel%20-%20demo-2%20%28for%20coolkiran%20at%20chandoo.org%29.xlsm


This is the code:

-----

[pre]
Code:
Option Explicit

Sub StealingAPrintoutWithBarsButNotStars()
'
' constants
Const ksFile = "x:...file.txt"
Const ksWS = "Sheet1"
Const kiRecordsTitles = 2
Const kiRecordsData = 7
'
' declarations
Dim I As Long, J As Long, K As Long, A As String, B As String
'
' start
'  text
I = FreeFile()
Open ksFile For Input As #I
J = 0
'  workbook
With Worksheets(ksWS)
Range(.Rows(2), .Rows(.Rows.Count)).ClearContents
End With
'
' process
With Worksheets(ksWS)
Do While Not EOF(I)
Line Input #I, A
J = J + 1
K = Int(((J - kiRecordsTitles) + kiRecordsData - 1) / kiRecordsData) + 1
Select Case ((J - kiRecordsTitles) Mod kiRecordsData)
Case 0, 1 ' empty, underscores
Case 2 ' name, age, charges
' name
B = Left(A, 40)
.Cells(K, 1).Value = Trim(Left(B, InStr(B, ",") - 1))
.Cells(K, 2).Value = Trim(Right(B, Len(B) - InStr(B, ",")))
' age
.Cells(K, 3).Value = Val(Mid(A, 41, 5))
' charges
.Cells(K, 4).Value = Mid(A, 107, 20)
Case 3 ' charges
' charges
.Cells(K, 4).Value = .Cells(K, 4).Value & vbLf & Mid(A, 107, 20)
Case 4 ' address, charges
' address
.Cells(K, 5).Value = Left(A, 40)
' charges
.Cells(K, 4).Value = .Cells(K, 4).Value & vbLf & Mid(A, 107, 20)
Case 5 ' address, state, zip code, charges
' address
.Cells(K, 5).Value = .Cells(K, 5).Value & vbLf & Left(A, 17)
' state
.Cells(K, 6).Value = Mid(A, 19, 2)
' zip code
.Cells(K, 7).Value = Val(Mid(A, 23, 5))
' charges
.Cells(K, 4).Value = .Cells(K, 4).Value & vbLf & Mid(A, 107, 20)
Case 6 ' bail
.Cells(K, 8).Value = Val(Mid(A, 96, 23))
End Select
Loop
End With
'
' end
'  text
Close #I
'  workbook
ActiveWorkbook.Save
'  beep
Beep
'
End Sub
[/pre]
-----


Check the charges column, particularly the 4th entry of each group (row of Address 1) as it appear to be shifted in the uploaded text file. Adjust values of start and character number for text functions LEFT, MID & RIGHT, if needed.


Regards!


PS: I'd settle for 15% of each imported bail, I think it's fair... please arrange the wire transfers to my Cayman Islands account.
 
Excellent SirJB7. Its working perfectly.


Now i feel relief, from 2 days i am struggling to get this code. You made my day.


And also thanks Bobhc, Marc L. for your support.
 
Hi, coolkiran!

Glad you solved it. Thanks for your feedback and for your kind words too. And welcome back whenever needed or wanted.

Regards!
 
Back
Top