Retrieve starting positions of the titles in the string - vb.net

I have a line of seven different titles that can consist of one or two words with at least 3 or 4 spaces between the title.
" Company Name Contact Name Address City State Zip Phone"
I need to retrieve the position in the line where each title begins. I use indexOf:
pos1 = line.IndexOf("company", System.StringComparison.InvariantCultureIgnoreCase)
This method works fine but is not that efficient for retrieving positions since the line comes from text documents and there is a great variability, for example I might have something like this:
" C0mpany Name C0ntact Name Address NCity St)te Zip Phone"
So the wording is not always exact. All I know is that there are 7 columns. What is the best way to retrieve 7 beginning positions of those columns programmatically?

This works quite well
Dim s As String
Dim v As Object
Dim tok As Object
s = " C0mpany Name C0ntact Name Address NCity St)te Zip Phone"
v = Split(s, " ")
For Each tok In v
If Len(Trim(tok)) > 0 Then
Debug.Print(tok & vbTab & InStr(s, tok))
End If
Next
output:
C0mpany Name 3
C0ntact Name 23
Address 41
NCity 60
St)te 69
Zip 78
Phone 85
The above is in Access VBA, but here is the SAME code after a cut + paste into VB.net

Related

Using StringSplitOptions to get word list VB.NET

trying to get emails list from RichTextBox to be in listbox.
so i used (for each, StringSplitOptions, to split the full text)
Richtextbox : "one#gmail.com two#yahoo.com three#hotmail.com that's all"
Code
For Each str As String In RichTextBox1.Text.Split(New String() {"#"}, StringSplitOptions.None)
ListBox1.Items.Add(str.Substring(str.LastIndexOf(" ") + 1))
Next
result in listbox :
one
two
three
all
if u search on "#Gmail.com"
you can change the loop to integer
for i = 0 to Length - 1
and use substring
(str.substring(str.indexof(" "), str.indexof(" .com")), or just add +#gmail.com
Well its normal cause u getting splits #, So u need to use Regex
but in that case u find all mail forum, so i recommended, to get valid email list, search on (# and also .) which contain emails list.
Dim reg As Regex = New Regex("(([\w-]+\.)+[\w-]+|([a-zA-Z]{1}|[\w-]{2,}))#" &
"((([0-1]?[0-9]{1,2}|25[0-5]|2[0-4][0-9])\.([0-1]?[0-9]{1,2}|25[0-5]|2[0-4][0-9])\." &
"([0-1]?[0-9]{1,2}|25[0-5]|2[0-4][0-9])\.([0-1]?[0-9]{1,2}|25[0-5]|2[0-4][0-9])){1}|" &
"([a-zA-Z]+[\w-]+\.)+[a-zA-Z]{2,4})", RegexOptions.Compiled Or RegexOptions.IgnoreCase)
For Each email As Match In reg.Matches(RichTextBox1.Text)
ListBox1.Items.Add(email.Value.ToString())
Next
MessageBox.Show("Done")
Hope it work

Search column for text from a Word file and display the cell adjacent

Using VBA for work, I already have code that analyses a Word file, picks out the aspects it needs and converts it to an Excel format.
We assign tasks to people in a Word file, the file and code is set in a way that it recognizes who is responsible for the task, e.g. Joe. We have many employees and I want to be able to get the script to search whatever name it has picked out from a list in one column, find the cell in which the name is and then display the data in the cell directly adjacent to it.
Currently the code has many people manually placed in there in loops i.e. if Joe present then display usernameJoe.
I have the usernames and names split in a table on the workbook structured as:
Name Username
Joe A ajoe
Jack B bjack
John C cjohn
... ...
While my code works when placed individually in loops:
Joe responsible?
If InStr(current_action.resp, "Joe") <> 0 Then
' assigning to online account
current_action.resp = "ajoe"
creating 85 loops is a long process and in an always changing work place too long-winded.
The picked-out name is known as current_action.resp in the code.
You should use an array and loop on it :
Sub test_Mugiwara_Luffy()
Dim PersonS(), _
i As Long, _
NameS() As String
ReDim PersonS(1 To 85, 1 To 2)
PersonS(1, 1) = "Joe A"
PersonS(2, 1) = "Jack B"
PersonS(3, 1) = "John C"
'....
For i = LBound(PersonS, 1) To UBound(PersonS, 1)
If Len(PersonS(i, 1)) >= 5 And InStr(1, PersonS(i, 1), " ") Then
NameS = Split(PersonS(i, 1), " ")
PersonS(i, 2) = LCase(Left(NameS(UBound(NameS)), 1) & NameS(LBound(NameS)))
Else
End If
If InStr(current_action.resp, PersonS(i, 1)) Then
' assigning to online account
current_action.resp = PersonS(i, 2)
Else
End If
Next i
End Sub

VBA insert vbNewLine in the first blank character

I need to insert a vbNewLine in a cell string value after the first blank character encountered after the 50 first characters?
For example:
"At company employees with the right skills have good prospects to be promoted then a relevant position becomes available" should become
"At company employees with the right skills (vbNewLine)
have good prospects to be promoted then a relevant position becomes available"
The Replace function can start at a certain point and replace a single occurrance of a character using parameters that are usually left to defaults.
Dim str As String, i As Long
i = 50
str = "At company employees with the right skills have good prospects to be promoted then a relevant position becomes available"
str = Left(str, i - 1) & Replace(str, Chr(32), Chr(10), _
Start:=i, Count:=1)
Debug.Print str
Use 50 as the starting point, the result I receive is,
At company employees with the right skills have good
prospects to be promoted then a relevant position becomes available
Since your question is so nicely posed, here's a solution
Sub solution()
Dim test As String
Dim pos As Integer
test = "At company employees with the right skills have good prospects to be promoted then a relevant position becomes available"
pos = InStr(51, test, " ") 'search for a space on or after the 51st character
If (pos >= 51) Then
'found a space
test = Left(test, pos) & vbNewLine & Mid(test, pos + 1) 'miss out that space
End If
Debug.Print test
End Sub

How to replace all spaces with new lines?

I have a table that contains spaces in its headers
First Name Last Name Average Level Degree
_________________________________________________
Mike Lowel 25
Stan Wilson 35
Papa Peterson 15
I need it to look like this
First Last Average
Name Name Level
Degree
_____________________________________________
Mike Lowel 25
Stan Wilson 35
Papa Peterson 15
Here is what I tried
Sub test()
myString = ThisWorkbook.Worksheets(1).Range("a1").Value
ThisWorkbook.Worksheets(1).Range("a1").Value = Replace(myString, " ", CHAR(10))
ThisWorkbook.Worksheets(1).Range("a1").WrapText = True
ThisWorkbook.Worksheets(1).Range("a1").EntireRow.AutoFit
myString = ThisWorkbook.Worksheets(1).Range("b1").Value
ThisWorkbook.Worksheets(1).Range("b1").Value = Replace(myString, " ", CHAR(10))
ThisWorkbook.Worksheets(1).Range("b1").WrapText = True
ThisWorkbook.Worksheets(1).Range("b1").EntireRow.AutoFit
End Sub
However, it throws an error. In addition, I am not sure how to loop over all letters. Is there any more efficient way. I need those headers to look nice: I need to get the same effect as if I click Alt+Enter. Each word should appear on a separate line
You can do this with one line of code:
Range("A1:C1").Replace " ",vblf
In the range it replaces all spaces with a VB Line Feed (Return)

excel 2007: append certain characters to value in a column to make it proper lenght

I have following values in a column :
123
456
789
65
1
I want to append correct number of zeros in all the values in that column such that the total length of character is 5.
00123
00456
00789
00065
00001
How do I do that?
If there is one number per cell, you can do this easily by changing the format to "Custom."
Right-click on the cells you would like to format.
From the context menu, choose "Format cells"
Choose the Custom category.
Over the word "General," in the Type textbox, enter 00000. (This tells Excel to print with
leading 0s).
Click OK.
If the number is bigger than five digits, it will print all of the digits.
===EDIT===
You explained that these were all in one cell. #paulmorriss has an excellent Excel-function-only solution, but let me proffer a VBA solution as an alternative:
Sub Macro1()
Dim txt As String
Dim asWords() As String
Dim zeros As String
txt = vbNullString
asWords = Split(Range("A1").Value) 'asWords(0)="123" etc.
For i = 0 To UBound(asWords) ' emulate StrDup (missing in VBA)
zeros = vbNullString
For j = Len(asWords(i)) + 1 To 5: zeros = zeros + "0": Next j
txt = txt + zeros + asWords(i) + " "
Next i
Range("B1").Value = txt 'Places answer in B1
End Sub
If the value you specify is in cell A1 then put the following formulae in B1 to K1. The value in K1 is what you need. You could specify one massive formula, but it's easier for people maintaining the spreadsheet to see what's going on if it's split up like this.
in B1 =TEXT(VALUE(LEFT(A1,SEARCH(" ",A1))),"000000")
in C1 =RIGHT(A1,LEN(A1)-SEARCH(" ",A1))
etc. =TEXT(VALUE(LEFT(C1,SEARCH(" ",C1))),"000000")
=RIGHT(C1,LEN(C1)-SEARCH(" ",C1))
=TEXT(VALUE(LEFT(E1,SEARCH(" ",E1))),"000000")
=RIGHT(E1,LEN(E1)-SEARCH(" ",E1))
=TEXT(VALUE(LEFT(G1,SEARCH(" ",G1))),"000000")
=RIGHT(G1,LEN(G1)-SEARCH(" ",G1))
=TEXT(I1,"000000")
=B1&" "&D1&" "&F1&" "&H1&" "&J1