The richtextbox1 contains 8 words "The brown fox jumped over the lazy dog". I want to copy the 5th word of the richtextbox1 to textbox1.text when the button1 clicked.
can this be done using find method such as find(character, start integer, end integer) etc ie. finding "empty" (space) character and read the characters until second "empty"(space) character found. or any other better method?
I would suggest using the split function for this scenario.
e.g
textbox1.text = richtextbox1.text.split(" ")(4) 'This is for the 5th word.
You may also use the Mid function indeed. Chech it out from
https://msdn.microsoft.com/en-us/library/05e63829(v=vs.90).aspx
Updated
textbox1.text = Mid(richtexbox1.text, 1, 5)
Related
I am trying to get the last word in a cell however, it does not seem to be appearing getting the last word of the cell. It seems to get the partial match and not working correctly. I am using =IFERROR(RIGHT(AP2,SEARCH(" ",AP2)-1),AP2). This works great for getting the first word in a cell even where the first word is the only word in a cell. Any ideas on why this is not getting the last word correctly?
You may try this UDF which can be used in another sub routine or on the worksheet as well.
Function GetLastWord(ByVal Str As String) As String
Dim arrStr
arrStr = Split(Str, " ")
GetLastWord = arrStr(UBound(arrStr))
End Function
If you need to use it on the worksheet, assuming your string is in A1 then try this...
=GetLastWord(A1)
Your formula is based on the first occurrence of a space in the string.
For example with "Man City V Stoke" in the formula SEARCH(" ",AP11) is looking for the first occurrence of a space which occurs at position 4. You are then subtracting 1 from this and using this as the number of characters to return from the right of the original string. So you get Right("Man City V Stoke",3) which is "oke".
A better way is a formula such as
=TRIM(RIGHT(SUBSTITUTE(AP11," ",REPT(" ",LEN(AP11))),LEN(AP11)))
find text after last space
The part written: =SUBSTITUTE(AP1," ",REPT(" ",LEN(AP1)))
This inserts spaces the length of the string (16) in place of every occurrence of a space i.e.
Man City V Stoke
When you then do RIGHT(SUBSTITUTE(AP11," ",REPT(" ",LEN(AP11))),LEN(AP11))
you are guaranteed to only get whitespace followed by the last word. You then use TRIM to get rid of this white space.
As far as I can tell the below worked for me.
=TRIM(RIGHT(SUBSTITUTE(AP40," ",REPT(" ",100)),100))
I can only assume my other method is unreliable at getting the last word.
I have a very large set of data that represents cartesian coordinates in the form x0,y0,z0,x1,y1,z1...xn,yn,zn. I need to create a new line at the end of each xyz coordinate. I have been trying to record a macro that moves a certain number of spaces from the beginning of each line, then creates a new line. This, of course, will not work since the number of digits in each xyz coordinate differs.
How can I create a macro to do this in Microsoft Word?
Try this:
Public Sub test()
Dim s As String
Dim v As Variant
Dim t As String
Dim I As Long
s = "x0,y0,z0,x1,y1,z1,xn,yn,zn"
v = Split(s, ",")
t = ""
For I = LBound(v) To UBound(v)
t = t + v(I)
If I Mod 3 = 2 Then
t = t + vbCr
Else
t = t + ","
End If
Next I
t = Left(t, Len(t) - 1)
Debug.Print t
End Sub
The Split function splits a string along the delimiter you specify (comma in your case), returning the results in a 0-based array. Then in the For loop we stitch the pieces back together, using a carriage return (vbCR) every third element and a comma otherwise.
The final (optional) step is to remove the trailing carriage return.
Hope that helps
The question placed before us was most clearly asked
“Please produce a macro sufficient to the task
I have Cartesian coordinates, a single line of these
Array them in many lines, triplets if you please!”
Instinctively we start to code, a solution for this quest
Often without asking, “Is this way truly best?”
But then another scheme arises from the mind
That most venerated duo: Word Replace and Find
Provide the two textboxes each an encantation
Check the Wildcard option and prepare for Amazation!
Forgive me!
In Word open Find/Replace
Click the More button and check the Use wildcards box
For Find what enter ([!,]{1,},[!,]{1,},[!,]{1,}),
For Replace with enter \1^p
Use Find Next, Replace and Replace All as usual
How it works
With wildcards, [!,]{1,} finds one or more chars that are NOT commas. This idiom is repeated 3 times with 2 commas separating the 3 instances. This will match 3 comma-delimited coordinates. The whole expression is then wrapped in parentheses to created an auto-numbered group (in this case Group #1). Creating a group allows us to save text that matches the pattern and use it in the Replace box. Outside of the parentheses is one more comma, which separates one triplet of coordinates from the next.
In the Replace box \1 retrieves auto-numbered group 1, which is our coordinate triplet. Following that is ^p which is a new paragraph in Word.
Hope that helps!
There's one thing that I want to ask. How can I get the whole line of text that contain a string in Visual Basic 2010?
Let's say:
MyText.txt file contains:
Configurations:
Name: Fariz Luqman
Age: 78
My Favourite Fruit: Lemon, Apple, Banana
My IPv4 Address: 10.6.0.5
My Car: Ferrari
In Visual Basic, I want to get the whole line of text that contain the string "Banana" and print it in the textbox so it will display in that textbox:
My Favourite Fruit: Lemon, Apple, Banana
Why am I doing this? Because the text file is being appended and the line number is random. The contents is also random because the texts is generated by Visual Basic. The text "Banana" can be in line 1, line 2 or can be in any line so how can I get the whole line of text that contain certain string?
Thank you in advance!
You can do this easily all in one line with LINQ:
TextBox1.Text = File.ReadAllLines("MyText.txt").FirstOrDefault(Function(x) x.Contains("Banana"))
However, if the file is rather large, that's not particularly efficient, since it will read the whole file into memory before searching for the line. If you want to make it stop loading the file once it finds the line, could use the StreamReader, like this:
Using reader As New StreamReader("Test.txt")
While Not reader.EndOfStream
Dim line As String = reader.ReadLine()
If line.Contains("Banana") Then
TextBox1.Text = line
Exit While
End If
End While
End Using
Just checked (should have done that first!). VB.Net does have a CONTAINS() method. So:
IF line1.Contains("Banana") THEN
'do something
END IF
I am trying to create a programming editor using a rich text box in vb.net. It should change
the color of text according to that text. For example, the keywords should be displayed in red, and numbers in blue. I can't find how to retrieve word by word from a rich text box .
You can use regular expressions to find the words, and another one for numbers.
Try this website that well help you create your desired regular expressions http://regexpal.com/
I would just take the rich textbox text field and do a string split by space.
If you don't have to roll-your-own, the editors from SharpDevelop are easily extendable.
SharpDevelop3 uses SharpTextEditor(WinForms).
SharpDevelop4 uses AvalonEdit (WPF)
I would do the following:
Dim txt as String = RichTextBox1.Text
Dim arr as String() = txt.Split(" "c) REM split along the whitespace character
For Each i In arr
If IsNumeric(i) Then
REM change the color of the number to blue
Next i
(I have avidly searched the forum, but the only similar 'replace' questions I could find were related to Python, Java etc, and not VBA)
I have a table within a MS Word (2010) document (it has two columns but only the second column has text in)
Some cells in the second column have one line of text and NO paragraph mark
Other cells in the second column have two lines of text and TWO paragraph marks (^p)
There is no regular pattern between these two types of cells
Where there is a second paragraph mark, this always occurs directly before the end-of-cell marker
I really need a macro to remove (replace with " ") this second (=final) paragraph mark from each cell where it occurs, but to ignore any first paragraph marks within the cells.
I would be extremely grateful if someone out there has the time and inclination to help me with this macro. As you might have guessed I have little experience in VBA despite attempting to give myself a crash course: I am more used to recording macros which is not an option in this more intricate case.
My hopes were raised when Google found me this- which looks like it could be adaptable http://www.vbaexpress.com/kb/getarticle.php?kb_id=334; but I have no idea how to tweak it to only replace a SECOND (/final) occurrence of ^p.
Thank you in advance.
How about:
Dim tbl As Table
Dim c As Cell
For Each tbl In ActiveDocument.Tables
For Each c In tbl.Columns(2).Cells
'The end of a cell without a carriage return is vbCr & Chr(7)
If Right(c.Range.Text, 3) = vbCr & vbCr & Chr(7) Then
c.Range.Text = Mid(c.Range.Text, 1, Len(c.Range.Text) - 3) & Chr(7)
End If
Next
Next