How do I read in a text file into VBA and print it out in the Intermediate Window?
How do I read in a text file into VBA and print it out in the Intermediate Window?
Hello AK_Beaver,
If you mean the Immediate Window then this code will work. Copy it to a VBA Module and change the FileName to the text file you want to view.
Sub ReadTextFile() Dim FileName As String Dim TextLine As String FileName = "C:\Read Me.txt" Open FileName For Input As #1 While Not EOF(1) Line Input #1, TextLine Debug.Print TextLine Wend Close #1 End Sub
Sincerely,
Leith Ross
"1N73LL1G3NC3 15 7H3 4B1L17Y 70 4D4P7 70 CH4NG3 - 573PH3N H4WK1NG"
Hello AK_Beaver,
If you mean the Immediate Window then this code will work. Copy it to a VBA Module and change the FileName to the text file you want to view.
Sub ReadTextFile() Dim FileName As String Dim TextLine As String FileName = "C:\Read Me.txt" Open FileName For Input As #1 While Not EOF(1) Line Input #1, TextLine Debug.Print TextLine Wend Close #1 End Sub
Sincerely,
Leith Ross
"1N73LL1G3NC3 15 7H3 4B1L17Y 70 4D4P7 70 CH4NG3 - 573PH3N H4WK1NG"
Yeah I meant Immediate Window. Thanks for the code, it prints the majority of my text file but leaves off the top quarter. Is their a limit to how much can be printed in the window? I've tried uploading the file but I think it's to large for the site.
Hello AK,
Did you try zipping the file before posting it?
Sincerely,
Leith Ross
"1N73LL1G3NC3 15 7H3 4B1L17Y 70 4D4P7 70 CH4NG3 - 573PH3N H4WK1NG"
opus.zip
Here's the text file I am trying to read.
Hello AK,
My testing with your file indicates the Immediate Window is very slow in processing the data. I modified the macro to use a time delay of 25 milliseconds. This work well on my computer. You may need to adjust the speed for your computer (faster or slower). Change FileName to match the file you want to open.
Sub ReadTextFile1() Dim Delay As Single Dim FileName As String Dim LineText As String Dim Text As String FileName = "C:\Users\Owner\Documents\AK_Beaver\opus.txt" Open FileName For Input As #1 While Not EOF(1) Line Input #1, LineText Debug.Print LineText Delay = Timer + 0.0025 While Timer < Delay: DoEvents: Wend Wend Close #1 End Sub
Sincerely,
Leith Ross
"1N73LL1G3NC3 15 7H3 4B1L17Y 70 4D4P7 70 CH4NG3 - 573PH3N H4WK1NG"
Hey Leith,
Did the whole thing print for you? I'm still just getting like 3/4's of it printed out. Highest line is:
STATION NAME: zan1 a 4 (ANCHORAGE WAAS 1; Anchorage, Alaska, U.S.A.)
Not sure what to do.
Hello AK,
It displayed the complete text in the Immediate Window for me with the delay set to 0.025 seconds. Increase the delay to to 0.5 seconds (1/2 second or 500 milliseconds). This should be slow enough to allow the Immediate Window to process the text and display it before the macro process the next line of text.
Not sure why you want to display a complete text file in the Immediate Window. Generally, the window is used to view your code's results while stepping through it and not as a general output device.
Sincerely,
Leith Ross
"1N73LL1G3NC3 15 7H3 4B1L17Y 70 4D4P7 70 CH4NG3 - 573PH3N H4WK1NG"
My professor wants it that why. Don't ask me why haha. Thanks. I'll look into it some more. I appreciate the help. I've got a decent grasp of coding but this stuff is beyond me.
There is a limit of ~200 lines in the immediate window before it starts removing top lines.
Cheers
Andy
Ah gotcha. That makes sense. How would I go about limiting it to print out the first 200 lines? Now I'm curious. Thanks!
Try
Sub ReadTextFile1() Dim Delay As Single Dim FileName As String Dim LineText As String Dim Text As String Dim i As Long FileName = "C:\Users\Owner\Documents\AK_Beaver\opus.txt" Open FileName For Input As #1 While Not EOF(1) i = i + 1 If i = 200 Then GoTo Exits Line Input #1, LineText Debug.Print LineText Delay = Timer + 0.0025 While Timer < Delay: DoEvents: Wend Wend Exits: Close #1 End Sub
MVP (Excel 2008-2010)
Post a workbook with sample data and layout if you want a quicker solution.
To help indent your macros try Smart Indent
Please remember to mark threads 'Solved'