Remove one character without removing all of same characters - vba

So, I need some help from removing one character without removing all of them.
For example, I want to remove "x" but I don't want to remove all of "x" letter what does a text contain. I made code but it removes all "x" letters from a text.
TextBox1.Text = Replace(TextBox1.Text, "x" "")

The VBA Replace function has a few more optional arguments which you could use: start and count. If you only want to replace the first occurrence do:
TextBox1.Text = Replace(TextBox1.Text, "x", "", 1, 1)

Related

Checking if a Word table's cell is empty - Empty cell containing a "marker"

This Word file has a giant table with 2 columns. I have to make a statement like:
If theCellIsEmpty, then
Fill it
End if
The thing is that when I get the content of the cell through:
FirstCellText = ActiveDocument.Tables(Tbl).Cell(Rw, 1).Range.Text
It doesn't get the "" I'm expecting, but it contains a sort of dot, so the text read is never empty.
Here is the dot I'd like to test for:
Moreover, I have no idea what this dot can be or where it came from. I tried to find it in the characters table without success.
Could someone explain me how to test the presence of this "end of line character" or the emptiness of the row? I don't want to check if the cell only has 1 character as if this character is something put there with a purpose, I don't want to delete it.
Also, every single line has this dot at the end...
What is it? How can I use/avoid it?
All you should need is something like:
Dim Cll As Cell
For Each Cll In ActiveDocument.Tables(1)
With Cll.Range
'if all it has is the end-of-cell marker, it's empty, so fill it
If Len(.Text) = 2 Then .Text = "Filler Text"
End With
Next
Thanks for all your help, I might have a simple solution thanks to Freeman, mostly :-)
I took the ascii number of the character to check what it is (through asc(xxxx) function) and it happens to be a "carriage return" having the ascii number "13"
Therefore, so I just need check if the content of the cell gives the ascii number 13 and it's done. (the "asc(xxxxxx)" gives the ascii number of only the first character of the text chain :) )
Of course, if the cell starts with a carriage return and there is text after, it won't be nice, so checking also if the text contained in the cell is only 2 char long will be a nice thing (this "carriage return" is apparently 2 char long... strange...) :)
If Asc(FirstCellText) = 13 and len(FirstCellText) = 2 Then

Copy 5th word of the "rich text box1" to "textbox1.text"

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)

Word Macro for separating a comma-separated list to columns

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!

Can't trim periods (...) and ellipsis (...) from strings in vb.net

I'm getting a very strange result when I try to use string.trim to remove characters.
I want to remove leading and trailing periods (...) from strings, but my code only removes trailing characters for some reason. Microsoft's documentation says that the string.trim(char array) method should trim from the start and end of the string.
http://msdn.microsoft.com/en-us/library/zke31k1x.aspx
My code to trim the string is...
mystring=mystring.trim(".")
if the input is "2342...." it gets shortened to "2342" but if the input is "...2342" the output is still "...2342"
I've tried defining a character array with 1 member (i.e. "."), but I get the same result.
I've also tried mystring.trimstart(".") but it does not work either
I'm pretty confused about why I'm getting this behavior
EDITED/SOLVED:
mystring included two different characters that represent dots (.)
One of the characters in mystring was three dots as an ellipsis (the three together were encoded as a single character with a value of 133). The other was a simple period (value of 46).
This solves the problem:
mystring=mystring.Trim(Chr(133)) 'removes ellipsis
mystring=mystring.Trim(Chr(46)) 'removes periods
I tried it right now:
Dim mystring = "...2342"
mystring = mystring.Trim(".")
Console.WriteLine(mystring)
but result is always 2342.
Are you sure that first char in "...2342" is a "."? Did you check its ASCII value?
Which framework are you using? I'm using Framework 4.0.
EDITED: try this to get ascii values
For Each c As Char In mystring.ToCharArray()
Console.Write(Hex(Asc(c)) & "-")
Next
Console.WriteLine()
Just tried this:
Dim mystring = "...2342"
mystring = mystring.Trim(".")
mystring is always 2342 no matter what permutation I try.
Have you definetly checked the result correctly? You're not checking it in break mode before the operation has taken place?
I agree with #Marco, perhaps the "." you used was not the same as the input.

How can i check for character , number and special characters in a string?

I want to user to enter only numbers and characters in textbox i.e no special charaters.
I don't want to use key press event of textbox.
As i need same validation in gridview.
So i want to validate whole string.
Thanks in advance.
Using the Regex class for regular expressions you can use:
If Regex.IsMatch(myString, "^[A-Za-z0-9]+$") Then
'Do stuff
End If
EDIT: I forgot to add the ^ and the $ to denote that the match should go from start to finish on the string. You'll also need to put a \s in there if whitespace is allowed.
You can parse the string and then check the ascii values to make sure they are only Alpha-numeric. Here's some pseudocode:
StrLength = Len(Text)
For x = 1 To StrLength
sChar = Mid$(Text, x, 1)'Gets the x'th charcter in Text
bASCII = Asc(sChar) 'Gets ASCII value of character
if bASCII(not in Range) Then ERROR
Next x
Here's a link for to the Ascii Values:
http://www.asciitable.com/