Excel VBA Course
Excel VBA Course - From Beginner to Expert

200+ Video Lessons
50+ Hours of Video
200+ Excel Guides

Become a master of VBA and Macros in Excel and learn how to automate all of your tasks in Excel with this online course. (No VBA experience required.)

View Course

Notepad to Excel

0

Hi,

I have a notepad with content like name=David age=20 state=tx

I would like to convert this data to Excel. But I want only values after the = operator (like David 20 tx)

Thank you

Answer
Discuss

Answers

0
Selected Answer

Hi Sivagokuk and welcome to the Forum.

In the attached file (which needs to be run with macros enabled), I've added a green button labelled "Import values after a prescribed operator in a text file". If you click that, the macro below (in Module 1) will run to allow you to pick a text file ("with content like

name=David age=20 state=tx

" as you say) which will be split into fields then imported into the active cell and cells to the right. It relies on presence of the defined operator (MyOperator= "=") and spaces after each field.

You could have 20 fields say, provided they follow the pattern <<name>><<operator>><<space>>

If your text file has several lines e.g.:

name=David age=20 state=tx
name=Jenny age=22 state=fl

then they will be imported into the lines(s) below.

The code is commented below so you can see what's happening (hopefully)!

Sub ImportAfterOp()

    Dim ThisLine As Variant
    Dim fileFilterPattern As String
    Dim MyOperator As String, n As Long, p As Long

    MyOperator = "="

    ' Open a  file but just look at text files
    fileFilterPattern = "Text Files *.txt,*.txt"

    ThisLine = Application.GetOpenFilename(fileFilterPattern)

    If ThisLine = False Then
        MsgBox "No file selected."
        Exit Sub
    End If

    Application.ScreenUpdating = False

    Open ThisLine For Input As #1
    ' Look at each line of the file
    Do Until EOF(1)
        Line Input #1, ThisLine
        ' If the line contains something
        If Len(ThisLine) > 0 Then
            ' add a trailing space
            ThisLine = ThisLine & " "
            p = 0
            ' loop to extract field values
            Do Until InStr(ThisLine, MyOperator) = 0
                ' 'find where operator is
                NextFld = InStr(ThisLine, MyOperator)
                ' make cell to the right as string from operator to first space
                ActiveCell.Offset(n, p).Value = Mid(ThisLine, NextFld + 1, InStr(ThisLine, " ") - NextFld - 1)
                ' remove that portion from ThisLine
                ThisLine = Mid(ThisLine, InStr(ThisLine, " ") + 1)
                ' increment column
                p = p + 1
            Loop
        End If
        ' Point to next row
        n = n + 1
    Loop

    ' Done so close the file for input
    Close #1

    Application.ScreenUpdating = True

End Sub

Hope this fixes your problem. If so, please be sure to mark this Answer as Selected.

Discuss

Discussion

Glad that seems to have worked for you. Thanks for selecting my Answer, Sivagokuk.
John_Ru (rep: 6142) Jul 11, '23 at 11:58 am
Thank you so much Jjohn_Ru. Its working as expected. I have one more query (may be i missed to add it in the first query). if possible can u help on that. I have below content in my notepad. Name=Siva Age=28 city=cbe name=yuvi city=dgl age=18   name=John age=    city=mdu

Here i have the standard fields like Name,age and city. but its in different order. i want to populate in same order in excel. Example - in excel the row contains the field names like name ag city and it should copy the content value from notepad and populate in the fields( even the order is different in notepad, it will pick and paste it in the correct field name)
Sivagokuk (rep: 4) Jul 11, '23 at 9:08 pm
Sivagokuk. That's a very different question (and needs a changed approach). Please ask a new question and use the Add Files... button to attach a representative Excel file (for the extracted data) but without real personal data. You can't upload text files to the Forum but you could embed one (as an icon) in your Excel file. That helps you by saving us time. 

I'm quite busy for the next 2 days but eill see if I can find time to respond to your nee question. 
John_Ru (rep: 6142) Jul 12, '23 at 1:23 am
Add to Discussion


Answer the Question

You must create an account to use the forum. Create an Account or Login