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
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
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.