Read od Display Each Line in Textbox1 of a text file

SamDsouza

Board Regular
Joined
Apr 16, 2016
Messages
204
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
 

Excel Facts

Bring active cell back into view
Start at A1 and select to A9999 while writing a formula, you can't see A1 anymore. Press Ctrl+Backspace to bring active cell into view.

Domenic

MrExcel MVP
Joined
Mar 10, 2004
Messages
19,829
Office Version
  1. 365
Platform
  1. Windows
The following code contains no error checking, and it assumes that each line in your text file ends in a carriage return and line feed (vbCrLf). Here's the code, which needs to be placed in the userform code module...

VBA Code:
Option Explicit

Dim data As Variant
Dim rw As Long

Private Sub cmdReadNextLine_Click()

    If rw = UBound(data) Then
        rw = LBound(data)
    Else
        rw = rw + 1
    End If
    
    Me.TextBox1.Value = data(rw)
    
End Sub

Private Sub cmdReadPreviousLine_Click()
    
    If rw = LBound(data) Then
        rw = UBound(data)
    Else
        rw = rw - 1
    End If
    
    Me.TextBox1.Value = data(rw)
    
End Sub

Private Sub UserForm_Initialize()

    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
    
    ts.Close
    
    data = Split(txt, vbCrLf)
    
    rw = 0
    
    Me.TextBox1.Value = data(rw)

    Set ts = Nothing
    Set fso = Nothing
    
End Sub

Hope this helps!
 
Solution

SamDsouza

Board Regular
Joined
Apr 16, 2016
Messages
204
Sir,

Extremely Sorry for a very late reply. Kindly excuse me

Just tried your solution. It works Perfectly Well. Thank you so much 🥂

SamD
198
 

Domenic

MrExcel MVP
Joined
Mar 10, 2004
Messages
19,829
Office Version
  1. 365
Platform
  1. Windows
That's great, glad I could help.

And thanks for the feedback.

Cheers!
 

SamDsouza

Board Regular
Joined
Apr 16, 2016
Messages
204

ADVERTISEMENT

Sir just One question although the Solution Worked perfectly

Wanted to know Can rw = Lbound(Data) = 0 and as defined in UF_Inti....
Sir Why cant be rw =1 instead of 0
I checked this too If value of rw =1 then it reads the second line of the file.

Any possiblities if RW = 1 so that it could read the First Line

SamD
199
 

Domenic

MrExcel MVP
Joined
Mar 10, 2004
Messages
19,829
Office Version
  1. 365
Platform
  1. Windows
I'm sorry, but I don't quite understand your question.

The Split function returns a zero-based, one dimensional array, where each element contains a substring. So this means that the indexing starts at 0. And so data(0) contains the first line, data(1) contains the second line, and so on.

Now, if you mean that you want the indexing to sync with something else, where the indexing starts at 1 instead of 0, then you can simply +1. So, for example, to transfer a line of text to the corresponding row in a worksheet...

VBA Code:
Cells(rw+1, "A").Value = data(rw)

Does this answer your question? If not, can you please elaborate?
 

SamDsouza

Board Regular
Joined
Apr 16, 2016
Messages
204

ADVERTISEMENT

Sir

The Split function returns a zero-based, one dimensional array, where each element contains a substring. So this means that the indexing starts at 0. And so data(0) contains the first line, data(1) contains the second line, and so on.
Ok Fine So in this Case i have to take into a/c Zero based one dimensional array. because what happens when pressing cmdNext...Button uptil the last line it goes to first Line so there is no control or any kind of indication that you have reached the Last Line. And Vice Versa ie pressing cmdPrevious.... the record crosses over the First line then goes to the Last Line and second last line uptil the First Line. The movement is of Pressing the Command buttons is continoulsy clock-wise and anti clock wise

Can we control the above issue with Zero based one dimensional array ?

VBA Code:
Cells(rw+1, "A").Value = data(rw)
👆 I've understood the effect . Thanks to show up the method to implement in a worksheet.

The reason why 1 and not 0 or Zero was for: what if i have other textboxes apart from textbox1 which reads/get record of/displays the first line of the text file uptil the last line
and data entered in other textboxes may not work with Zero based Dimensional array if i had to code like below Which i was exploring before you gave your solution.
VBA Code:
Option Explicit

Private Sub UserForm_Initialize()
   
     ReDim sDatatxt1(1 To totLines)
     ReDim sDatatxt2(1 To totLines)
     CurRec = 1
End Sub

Private Sub cmdNext_Click()

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

   Textbox2.text = sDatatxtA(CurRec)
   Textbox3.text = sDatatxtB(CurRec)
  
End If
SamD
200:)
 

Domenic

MrExcel MVP
Joined
Mar 10, 2004
Messages
19,829
Office Version
  1. 365
Platform
  1. Windows
In that case, we can have it do the following. When it's at the last line and cmdNext is clicked, it simply beeps, and then exits the sub. And, the same thing for cmdPrev. However, if you would rather it not beep, you can simply remove the appropriate line. Does this help?

VBA Code:
Private Sub cmdReadNextLine_Click()

    If rw = UBound(data) Then
        Beep 'optional
        Exit Sub
    End If
    
    rw = rw + 1
    
    Me.TextBox1.Value = data(rw)
    
End Sub

Private Sub cmdReadPreviousLine_Click()
    
    If rw = LBound(data) Then
        Beep 'optional
        Exit Sub
    End If
    
    rw = rw - 1
    
    Me.TextBox1.Value = data(rw)
    
End Sub
 

SamDsouza

Board Regular
Joined
Apr 16, 2016
Messages
204
First of all correction
VBA Code:
Private Sub cmdNext_Click()

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

   Textbox2.text = sDatatxt1(CurRec)
   Textbox3.text = sDatatxt2(CurRec)
 
End If
👆 Changed from sDatatxtA and sDatatxtB Something went wrong when posting #7 did not get chance to edit

In that case, we can have it do the following. When it's at the last line and cmdNext is clicked, it simply beeps, and then exits the sub. And, the same thing for cmdPrev. However, if you would rather it not beep, you can simply remove the appropriate line. Does this help?
Yes, it has helped

Any possibility for the remaining part of #7

SamD
201
 

Rick Rothstein

MrExcel MVP
Joined
Apr 18, 2011
Messages
37,561
Office Version
  1. 2019
  2. 2010
Platform
  1. Windows
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...
VBA Code:
Sub Test()
    Dim Arr As Variant
    Arr = Split("one two three four five")
    ReDim Preserve Arr(1 To UBound(Arr) + 1)
    '
    ' Arr is now one-based
    '
End Sub
 
Last edited:

Forum statistics

Threads
1,148,396
Messages
5,746,452
Members
424,020
Latest member
LongDoo

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
Top