vba Read Text file and update in Different Column...

Mallesh23

Well-known Member
Joined
Feb 4, 2009
Messages
983
Office Version
  1. 2010
Platform
  1. Windows
HTML:
Hi Team
I have textfile with data in it as shown below , my task is to seperate below data in ColumnsSeperation Criteria is *---------So in Below situation Data should get populated in Column A,B,C.Thanks in advance.
xxxxxxxx*--------------yyyyyyyy*--------------zzzzzzzz

My attempted Code. with this I get data in First Column only.

Option Explicit
Sub ReadtxtFileIntoSeveralColumns()
Dim fso As FileSystemObject: Set fso = New FileSystemObjectDim txtStream As TextStreamDim TextLine As StringDim i As LongDim Sheet_i As Worksheet
Set fso = New FileSystemObjectSet txtStream = fso.OpenTextFile("C:\Users\User\Desktop\TextData.txt", ForReading, False)
' Read the file one line at a time   Do While Not txtStream.AtEndOfStream    TextLine = txtStream.ReadLine 'read line        If Left(TextLine, 1) = "*" Then Exit Do     ActiveSheet.Cells(i + 1, 1).Value = TextLine    i = i + 1Else    End If
Loop    ' Close filetxtStream.Close

Set txtStream = NothingSet fso = Nothing
End Sub

Regards,
mg
 

Excel Facts

How to fill five years of quarters?
Type 1Q-2023 in a cell. Grab the fill handle and drag down or right. After 4Q-2023, Excel will jump to 1Q-2024. Dash can be any character.
Hello Mallesh,

This worked for me...

Rich (BB code):
Sub ReadtxtFileIntoSeveralColumns()


    Dim File    As String
    Dim Line    As Variant
    Dim RegExp  As Object
    Dim Rng     As Range
    Dim Text    As String
    Dim Wks     As Worksheet
    
        File = "C:\Users\User\Desktop\TextData.txt"
        
        Set Wks = ThisWorkbook.Worksheets("Sheet1")
        Set Rng = Wks.Range("A2:C2")
        
        Set RegExp = CreateObject("VBScript.RegExp")
            RegExp.Pattern = "(.+)\*\-+(.+)\*\-+(.+)\b"


        Open File For Binary Access Read As #1 
            Text = String(LOF(1), Chr(32))
            Get #1 , , Text
        Close #1 
        
            For Each Line In Split(Text, vbCrLf)
                If RegExp.Test(Line) = True Then
                    Rng.Value = Split(RegExp.Replace(Text, "$1 $2 $3"), " ")
                    Set Rng = Rng.Offset(1, 0)
                End If
            Next Line
        
End Sub
 
Upvote 0

Forum statistics

Threads
1,223,896
Messages
6,175,264
Members
452,627
Latest member
KitkatToby

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