split text file VBS and give column headers - vbscript

Parse the name field as "Last Name", First Name, Middle. If there is a second name then the second name needs to be in its own field. Not all names have a second name.
If the name is a business I would need the entire name in the last name field and a blank in the first and middle names.
My code:
Const ForReading = 1
x=0
on error resume next
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile("Test.txt", ForReading)
Do Until objFile.AtEndOfStream
strLine = objFile.ReadLine
arrName = Split(strLine, vbTab)
msgbox(arrName(x))
x = x+1
Loop
file looks as follows
Name 2nd name
ABDELMAGID A SMITH & LISA S
ABDELMAGID A SMITH & LISA S
CLEVELAND SANDY LEE
CLEVELAND SANDY LEE
WHALEN STEPHEN H & JOAN L
WHALEN STEPHEN H & JOAN L
BANK TWO OF VIRGINIA INC C/O DOE JOHN
BANK TWO OF VIRGINIA INC C/O DOE JOHN
BANK TWO OF VIRGINIA INC C/O DOE JOHN
SMITH JOHN ELLIS JR
JONES JOHN III

Related

INI file - retrieve a section name by key name in VBS

I would like to retrieve a section name from an INI file with only a unique key name
My ini file :
...
[Area.104]
Title=Central North America
Local=Scenery\NAMC
Layer=104
Active=TRUE
Required=FALSE
[Area.105]
Title=Eastern North America
Local=Scenery\NAME
Layer=105
Active=TRUE
Required=FALSE
[Area.106]
Title=Western North America
Local=Scenery\NAMW
Layer=106
Active=TRUE
Required=FALSE
...
How can I get section name [Area.105] from unique key Title=Eastern North America ???
Thank you
I have two ways of finding the required Area code:
METHOD 1
Option Explicit
Dim strFilePath, ofso, ofile, strFileData, strKey, strPrev, strCurr
strFilePath="" '<-- Enter the absolute path of your .ini file in this variable
Set ofso = CreateObject("scripting.FileSystemObject")
Set ofile = ofso.OpenTextFile(strFilePath,1,False)
strKey = "Eastern North America" '<-- Enter Unique title for which you want the Area code
strPrev=""
strCurr=""
Do
strCurr = ofile.ReadLine
If InStr(1,strCurr,strKey)<>0 Then
Exit Do
End If
strPrev = strCurr
Loop Until ofile.AtEndOfStream
MsgBox strPrev
Set ofile = Nothing
Set ofso = Nothing
METHOD 2(Using Regular Expression)
Option Explicit
Dim strFilePath, ofso, ofile, strFileData, strKey, re, objMatches
strFilePath="" '<-- Enter the absolute path of your .ini file in this variable
Set ofso = CreateObject("scripting.FileSystemObject")
Set ofile = ofso.OpenTextFile(strFilePath,1,False)
strFileData = ofile.ReadAll()
ofile.Close
strKey = "Eastern North America" '<-- Enter Unique title for which you want the Area code
Set re = New RegExp
re.Global=True
re.Pattern="\[([^]]+)]\s*Title="&strKey
Set objMatches = re.Execute(strFileData)
If objMatches.Count>0 Then
MsgBox objMatches.Item(0).Submatches.Item(0)
End If
Set re = Nothing
Set ofile = Nothing
Set ofso = Nothing
>>>Click here for Regex Demo<<<
Regex Explanation:
\[ - matches literal [
([^]]+) - capture 1+ occurrence of any character which is not ] in a group
] - matches literal ]
\s* - matches 0+ white-spaces(which include the newline characters)
Title= - matches the text Title=. This is then concatenated with the variable strKey containing the value of unique title.

VBS Get a section name from an INI file with only a unique key name with ADODB.Stream

I would like to find a section name from an INI file with only a unique key name using ADODB.Stream instead of scripting.FileSystemObject with Charset "_autodetect_all"
My ini file :
...
...
...
[Area.104]
Title=Central North America
Local=Scenery\NAMC Layer=104
Active=TRUE
Required=FALSE
[Area.105]
Local=Scenery\NAME
Layer=105
Active=TRUE
Required=FALSE
Title=Eastern North America
[Area.106]
Local=Scenery\NAMW
Layer=106
Title=Western North America
Active=TRUE
Required=FALSE
...
...
...
How can I get section name [Area.105] from unique key Title=Eastern North
America ??? Keys are in random order. Thanks
Here is the answer which I have got from another website (thank you very much omen999)
This code works perfectly with ADODB
Dim TitleName
TitleName = Array("Central North America")
Set IniStream=CreateObject("ADODB.Stream")
IniStream.Open
Inistream.Charset="_autodetect_all"
IniStream.LoadFromFile "Area.ini"
IniFile=IniStream.ReadText
PosEnd=InStrRev(IniFile,"]",InStrRev(IniFile,TitleName(0)))
PosStart=InStrRev(IniFile,"[",PosEnd)+1
Wscript.Echo Mid(IniFile,PosStart,PosEnd-PosStart)
IniStream.Close

vbscript to display output upto the blank line

My txt file1 is
Fruits
-------
Apple
Banana
Orange
Vegetables
---------
broccoli
Beans
txtfile 2
Fruits
Vegetables
output should be:
Banana
Orange
Brocoli
Beans
I should be able to grab from ------- to blank space and I am having problems in doing that.
I think the following code might give you some idea about reading txt files,
Set fso = CreateObject("Scripting.FileSystemObject")
strWholeData = fso.OpenTextFile("C:\...\xxx.txt").ReadAll
strDataArr = Split(Trim(strWholeData),vbNewLine)
For di = Lbound(strDataArr) To Ubound(strDataArr)
Msgbox strDataArr(di)
Next

Comparison between two tab separated files in unix using awk

I've written this code on unix but I am facing the problem as mentioned below.
My Code is:
paste 1.txt 2.txt|
awk ' { FS = "\t " } ; NR == 1 { n = NF/2 }
{for(i=1;i<=n;i++)
if($i!=$(i+n))
{c = c s i; s = "," }
if(c)
{print "Line No. " NR-1 " COLUMN NO " c;
c = "" ; s = "" } } '
Expected Output:
Line No. 2 COLUMN NO 2,3
Line No. 4 COLUMN NO 1,2,3,4
Line No. 6 COLUMN NO 2,3,4,5
Line No. 7 COLUMN NO 1,2,3,4,5
Output that is getting generated:
Line No. 2 COLUMN NO 2,3
Line No. 4 COLUMN NO 1,2,3,4
Line No. 6 COLUMN NO 2,3,4,5
Line No. 7 COLUMN NO 1,2,3,4
Below specified file is space separated. To understand it better I have formatted it this way.
File1:
ID_ID First_name Last_name Address Contact_Number
ID1 John Rock 32, Park Lake, California 2222200000
ID2 Tommy Hill 5322 Otter Lane Middleberge 3333300000
ID3 Leonardo Test Half-Way Pond, Georgetown 4444400000
ID8 Rhyan Bigsh 6762,33 Ave N,St. Petersburg 5555500000
ID50 Steve Goldberg 6762,33 Ave N,St. Petersburg 6666600000
ID60 Steve Goldberg 6666600000
File2:
ID_ID First_name Last_name Address Contact_Number
ID1 John Rock 32, Park Lake, California 2222200000
ID2 Tommy1 Hill1 5322 Otter Lane Middleberge 3333300000
ID3 Leonardo Test Half-Way Pond, Georgetown 4444400000
ID80 Sylvester Stallone 5555500000
ID50 Steve Goldberg 6762,33 Ave N,St. Petersburg 6666600000
ID60 Mark Waugh St. Petersburg 7777700000
ID70 John Smith 8888800000

String search with in the excel column values row wise

I have an Excel sheet which has 200 columns. Now number of rows are 3500. So i have to search a string if it presents within any column for each row. Now to make the process fatser,I am looking for any alternate instead of Looping technique. Is there any such?
IntRow6 = 2
DataCount = 0
Do While objSheet6.Cells(IntRow6,1).Value <> ""
For DataCount = 0 to UBound(VMHArray)
IntClmn3 = 1
Do While 1 = 1
If objSheet6.Cells(IntRow6,IntClmn3).Value = VMHArray(DataCount) Then
objSheet6.Cells(IntRow6,IntClmn3+2).Value=objSheet6.Cells(IntRow6,IntClmn3+5).Value
Exit Do
End If
IntClmn3 = IntClmn3 + 1
Loop
Next
IntRow6 = IntRow6 + 1
Loop
The above is taking to much time, thus i am looking for an equivalent VBScript code which can run more faster search.
EDIT:
ParentColmnCount=ParentColmnCount-1
IntRow6=2
DataCount=0
Do While objSheet6.Cells(IntRow6,1).Value <> ""
For DataCount=0 to UBound(VMHArray)
If objSheet6.Range(objSheet6.Cells(IntRow6,1),objSheet6.Cells(IntRow6,ParentColmnCo‌​unt)).Find(VMHArray(DataCount)) <> Nothing Then
MsgBox("Hi")
End If
Next
IntRow6=IntRow6+1
Loop
I'm getting any error saying that, "Object variable not set" error at the Range line of the above code.
UPDATE
I have updated my code as per your suggestion,and modified the declaration of variables as below:
Option Explicit
Dim objExcel1,objWorkbook
Dim strPathExcel1
Dim objSheet6,objSheet3
Dim IntRow6,IntRow3
Dim IntClmn3
Dim DataCount,ParentColmnCount
Dim Falg
Dim TaskCounter
Dim r As Range
Dim s As Variant
But I am getting the error again: Expected end of statement" in the line "Dim r As Range"
Check out Range.Find (which returns a Range object). It's faster than a loop.
Example:
Sub Test()
Dim r As Range
Dim matched As Range
'Get the second row
Set r = Sheet1.Range("2:2")
Set matched = r.Find("myString")
'Do stuff with matched
End Sub
UDPATE:
Assuming you are using CreateObject("Excel.Application"), then the Range object will be present (as CreateObject will return an instance of Excel which contains Range objects and functionality).
For further proof, you're already looping through the worksheet using Range objects (Cells is a Range object).
UPDATE:
Here's a more advanced example. Assume the following data in on Sheet1:
fred ethel ricky bobby 1 2 3 4
lucy myrtle fonzy rickie 1 2 3 4
joanie chachie donna patty 1 2 3 4
selma homer lisa bart 1 2 3 4
You can loop through the ranges like so:
Sub test()
Dim r As Range
Dim names(3) As String
Dim s As Variant
names(0) = "ethel"
names(1) = "fonzy"
names(2) = "patty"
names(3) = "selma"
For Each s In names
For Each r In Sheet1.Cells.CurrentRegion.Find(s)
r.Offset(0, 4).Value = r.Offset(0, 4).Value + 1000
Next r
Next s
End Sub
After the routine is done, the data is now:
fred ethel ricky bobby 1 1002 3 4
lucy myrtle fonzy rickie 1 2 1003 4
joanie chachie donna patty 1 2 3 1004
selma homer lisa bart 1001 2 3 4
UPDATE:
I just saw your comment on your question (and edited your question to include the updated code).
You're getting "Object variable not set" on that line because you cannot use <> comparisons with objects. Change the line to read:
If Not objSheet6.Range(objSheet6.Cells(IntRow6,1),objSheet6.Cells(IntRow6,ParentColmnCo‌​unt)).Find(VMHArray(DataCount)) Is Nothing Then

Resources