Read od Display Each Line in Textbox1 of a text file

SamDsouza

Board Regular
Joined
Apr 16, 2016
Messages
205
Hello

Can anyone help me to read Each Line in Textbox1 of a text file.
Have 2 command buttons cmdReadNextLine and cmdReadPreviousLine and Textbox1.Multiline = False

By Pressing cmdReadNextLine i would like to read the single Next Line of Text file in Textbox1
and By Pressing cmdReadPreviousLine i would like to read the previous Line of Text file in Textbox1.

Even if there is blank line in Text file the Textbox should display as is or Blank

I was only able to load the text file with below Code and it displayed the last line of the text file

Any ideas how the First line could be displayed in Textbox1.text and pressing above respective command buttons to do the necessary actions

Require your help for coding for command buttons to get the desired results

VBA Code:
Private Sub fsoMethod_ReadEachLine()

Dim myFilePath As String, strFileData As String, eachLine As String
Dim fso As Object, txtDataObj As Object, lCount As Long
Set fso = CreateObject("Scripting.FileSystemObject")

myFilePath = "C:\ABC\1.txt"

Set txtDataObj = fso.OpenTextFile(myFilePath, 1)

While Not txtDataObj.AtEndOfStream
   eachLine = txtDataObj.readline
   TextBox1.Text = eachLine
Wend
txtDataObj.Close

Set fso = Nothing

End Sub
Thank you

SamD
196
 
Rick Sir.
Was almost hopeless but
If it helps the discussion any, you can convert a zero-based, one-dimensional array to a one-based, one-dimensional array by using the ReDim Preserve statement. For example...
Thanks and Let me check and revert back

SamD
202
 
Upvote 0

Excel Facts

Can you AutoAverage in Excel?
There is a drop-down next to the AutoSum symbol. Open the drop-down to choose AVERAGE, COUNT, MAX, or MIN
Rick Sir
You made my evening. Thank you so much for just single valuable input. Indeed i changed everything as per my requirement and on Domenic Sir's coding
Following is the Refined coding as per my requirement
VBA Code:
Option Explicit
Dim datatxtVar As Variant
Dim curRecLng As Long
Dim sDatatxt1() As String, sDatatxt2() As String
Dim totLines As Integer

Private Sub UserForm_Initialize()
    Call NewfsoMethod_ReadAllLines
    ReDim sDatatxt1(1 To totLines)
    ReDim sDatatxt2(1 To totLines)
End Sub

Public Sub NewfsoMethod_ReadAllLines()

    Const SOURCE_FILENAME As String = "C:\ABC\1.txt"
    Const ForReading As Long = 1
 
    Dim fso As Object
    Set fso = CreateObject("Scripting.FileSystemObject")
 
    Dim ts As Object
    Set ts = fso.opentextfile(SOURCE_FILENAME, ForReading)
 
    Dim txt As String
    txt = ts.readall
    totLines = ts.Line
    ts.Close
 
    datatxtVar = Split(txt, vbCrLf)
    ReDim Preserve datatxtVar(1 To UBound(datatxtVar) + 1)
    curRecLng = 1
 
    TextBox1.Text.Text = datatxtVar(curRecLng)

    Set ts = Nothing
    Set fso = Nothing

End Sub

Private Sub cmdReadNextLine_Click()

If curRecLng < totLines Then
   sDatatxt1(curRecLng) = Textbox2.text
   sDatatxt2(curRecLng) = Textbox3.text
 
    curRecLng = curRecLng + 1
    If curRecLng > UBound(sDatatxt1) Then ReDim Preserve sDatatxt1(1 To curRecLng)
    If curRecLng > UBound(sDatatxt2) Then ReDim Preserve sDatatxt2(1 To curRecLng)


   TextBox1.Text = datatxtVar(curRecLng)
   Textbox2.text = sDatatxt1(curRecLng)
   Textbox3.text = sDatatxt2(curRecLng)
End If

End Sub

Private Sub cmdReadPreviousLine_Click()

If curRecLng > 1 Then
   sDatatxt1(curRecLng) = Textbox2.text
   sDatatxt2(curRecLng) = Textbox3.text
   curRecLng = curRecLng - 1
 
   TextBox1.Text = datatxtVar(curRecLng)
   Textbox2.text = sDatatxt1(curRecLng)
   Textbox3.text = sDatatxt2(curRecLng)
End If

End Sub

Thank you so much for the methods of deriving a result with simple coding. Much appreciated
Rick Sir ?? and Domenic Sir? ? Cheers

SamD? ?
203
 
Upvote 0

Forum statistics

Threads
1,214,646
Messages
6,120,716
Members
448,985
Latest member
chocbudda

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