VB Question + Text File Conversion

Moxioron

Active Member
Joined
Mar 17, 2008
Messages
436
Office Version
  1. 2019
Hello all.

Someone provided me some code that allowed me to take a text file and make some of the lines align side by side.

Here is my dilemna. I am only want to do this for the first two lines and then everything else is fine.

Here is a snapshot of the text file lines:
Alternate Account: 100/0000000123456789
Charlie Sheen

I would like to bring Charlie Sheen up next to the account number.

Here is the code that I am using that works, but I get an error message. Any thoughts on how I can perform this function w/o the error message (Run-time error '62' - Input Past End of File):

Do
' get first line
Line Input #FH, strLineIn
strLineOut = strLineIn
' get second line
Line Input #FH, strLineIn
strLineOut = strLineOut & " " & strLineIn


Print #FH2, strLineOut


Loop Until EOF(FH)
 

Excel Facts

Highlight Duplicates
Home, Conditional Formatting, Highlight Cells, Duplicate records, OK to add pink formatting to any duplicates in selected range.
Try:

Code:
' get first line
Line Input #FH, strLineIn
strLineOut = strLineIn
' get second line
Line Input #FH, strLineIn
strLineOut = strLineOut & " " & strLineIn

Print #FH2, strLineOut


Now the rest of the file:

Do

    Line Input #FH, strLineIn

    strLineOut = strLineIn

    Print #FH2, strLineOut


Loop Until EOF(FH)
 
Upvote 0
Thanks for responding to my question.

I pasted your suggestion. I get a 'Compile Error' Do Without Loop message. When I put an apostrophe in front of the second Do, I get the same Run-Time 62 error message.

Here is what my code looks like:


Public Sub convertfile2to1()
Dim FH As Integer
Dim FH2 As Integer
Dim strPath As String
Dim strPathOut As String
Dim strLineIn As String
Dim strLineOut As String
'input file path
strPath = "C:\Documents and Settings\carpean\My Documents\Returns Month End\Incoming Returns.MBK.txt"
' output path for new file converted to 1 line transaction
strPathOut = "C:\Documents and Settings\carpean\My Documents\Returns Month End\Incoming Returns.txt"
FH = FreeFile
Open strPath For Input As #FH
FH2 = FreeFile
Open strPathOut For Output As #FH2
Do
' get first line
Line Input #FH, strLineIn
strLineOut = strLineIn
' get second line
Line Input #FH, strLineIn
strLineOut = strLineOut & " " & strLineIn
Print #FH2, strLineOut
'Now the rest of the file:
'Do
Line Input #FH, strLineIn
strLineOut = strLineIn
Print #FH2, strLineOut
Loop Until EOF(FH)
Close #FH
Close #FH2

End Sub
 
Upvote 0
Some clarification.

Here is what the text file looks like currently:

DATE: 07/01/2011

Depositing Account: 001/00000000012345678/00000000000000000123

Alternate Account: 100/000000024681012141618


Ashton Kutchter


This is what I want it to look like so I can extract Ashton's name with my import VB (using the constate 'Alternate Account'):

DATE: 07/01/2011

Depositing Account: 001/00000000012345678/00000000000000000123

Alternate Account: 100/000000024681012141618
Ashton Kutchter
 
Upvote 0
Some clarification.

Here is what the text file looks like currently:

DATE: 07/01/2011

Depositing Account: 001/00000000012345678/00000000000000000123

Alternate Account: 100/000000024681012141618


Ashton Kutchter


This is what I want it to look like so I can extract Ashton's name with my import VB (using the constate 'Alternate Account'):

DATE: 07/01/2011

Depositing Account: 001/00000000012345678/00000000000000000123

Alternate Account: 100/000000024681012141618
Ashton Kutchter



Ah ... not what you originally post at all.

SO now what you need to do is test the line read with InStr() looking for "Alternate Account". If found then read the next linebefore writing the data.
 
Upvote 0
Thanks for the response. Actually I only commented the 'Do after I received a 'Compile Error Message'.

So maybe I don't have to modify the text file to import the customer name field.

This is what I have to pull in the Alternate Account Number:

'get the alternate acct
If StrComp(Mid(strFileData, 1, 9), "Alternate", vbBinaryCompare) = 0 Then
strAlt = Mid(strFileData, 31, 14)
End If


My challenge and lack of understanding is how to import the customer name since there is no constant on the same row. That is why I thought of 'automagically' moving the customer name next to the Alternate Account so at least I could use 'Alternate' as my anchor.

So how could I write this to pull in data in the row directly after 'Alternate'?

Thanks again for your help and patience. My limited knowledge is from others building the code and me modifying it.
 
Upvote 0
HiTechCoach;2795973... now what you need to do is test the line read with InStr() looking for [COLOR=black said:
"Alternate Account". If found then read the next line [/COLOR]before writing the data.

I would use an IF ...Then to do this.
 
Upvote 0
Thanks so much for taking time to respond.

I apologize for my ignorance, but I am very confused. I am trying to do this on my own, but am getting no where.

In order to grab the alternate account number from this string:

Alternate Account: 100/0000000123456789


I use this and it works great.

'get the alternate acct
If StrComp(Mid(strFileData, 1, 9), "Alternate", vbBinaryCompare) = 0 Then
strAlt = Mid(strFileData, 31, 14)
End If


Since the customer's name is directly below that, I would like use the same constant, "Alternate" to import that data as well.

This is what I have so far lol:
If InStr("Alternate", then what? Thanks again.
 
Upvote 0

Forum statistics

Threads
1,224,521
Messages
6,179,291
Members
452,902
Latest member
Knuddeluff

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