Want to add the line from the text box to a list box if it contains a word from words - vb.net

I Want to add the line from the text box to a list box if it contains a word from words.
This code introduces my result into the textbox, I want it in a textbox line or in a listbox line.
So, TempTextBox will have to be a textbox line or a listbox line so I will not create too many textboxes. How can I improve the code?
If TextBox1.Lines(i).Contains(word) Then
found = True
Dim tempTextBox As TextBox = CType(Me.Controls("CheckVar" & i.ToString), TextBox)
On Error Resume Next
If TextBox2.Lines(i).Contains(word) Then
If tempTextBox.Text.Contains(word) Then
Else
tempTextBox.Text = tempTextBox.Text + " " + TxtbValAfterCompar.Text()
End If
Else
End If
End If

Related

VBA - Enter array into multiline textbox on userform

I want to enter each element of an array onto a new line in multiline textbox on a userform. I have the Multiline and EnterKeyBehaviour of the textbox set to True.
The code below will enter the array elements but after entered they are then overwritten by the next element.
For i = LBound(clipArray) To UBound(clipArray)
frmMyForm.txtBox.Text = clipArray(i)
Next
I have tried variations like below (with vbCr, vbCrLf etc) but still does not work.
frmMyForm.txtBox.Text = clipArray(i, vbNewLine)
How can I fix?
Please, use:
frmMyForm.txtBox.Text = Join(clipArray, vbLf)

Check for an empty Text Box

I have a text box in a Word 2016 document. This is not a TextBox form object but a normal Text Box that you insert from the Insert tab here:
I am trying to check if it is empty when the document is opened. For the life of me I cannot figure out how to do this. I have tried all of the following:
If (Len(ActiveDocument.Sections(1).Headers(wdHeaderFooterPrimary).Shapes("Text Box 2").TextFrame.TextRange.Text & vbNullString) = 0) Then
If (IsNull(ActiveDocument.Sections(1).Headers(wdHeaderFooterPrimary).Shapes("Text Box 2").TextFrame.TextRange.Text)) Then
If (LTrim(RTrim(ActiveDocument.Sections(1).Headers(wdHeaderFooterPrimary).Shapes("Text Box 2").TextFrame.TextRange.Text)) = "") Then
If (ActiveDocument.Sections(1).Headers(wdHeaderFooterPrimary).Shapes("Text Box 2").TextFrame.TextRange.Text = "") Then
None of these return true when the text box is empty? This is the text box:
It seems that the text box always contains a paragraph marker (which I can't delete). This is what the VBA watch shows:
Watch : : ActiveDocument.Sections(1).Headers(wdHeaderFooterPrimary).Shapes("Text Box 2").TextFrame.TextRange.Text : "
" : String : ThisDocument.Document_Open
Note that the watch is two separate lines which makes me think that there's CRLF or something in there?
There is always a paragraph break in an otherwise empty textbox. Accordingly, you could use something along the lines of:
Private Sub Document_Open()
With ActiveDocument.Sections(1).Headers(wdHeaderFooterPrimary).Shapes("Text Box 2")
If Not .TextFrame Is Nothing Then
If Trim(.TextFrame.TextRange.Text) = vbCr Then
MsgBox "Empty Text Range"
End If
Else
MsgBox "No Text Range"
End If
End With
End Sub

How can I use the InStr function to search and highlight a value in a multiline textbox?

I am attempting to create a search inside of a multiline textbox that will find each instance of a value and highlight that value inside the textbox. It will need to work regardless of how many instances exist on a given line. So far I have used the following code to identify if a value exists on a line but cannot get the highlighting to work properly when there are multiple instances of the same value.
strVal = "Find Me"
arrLines =Split(myTextbox.value, vbCrLf)
For Each strLine In arrLines
If InStr(strVal, myTextbox.text) > 0 Then
myTextbox.SelStart = InStr(strVal, my textbox.value)
myTextbox.SelLength = Len(strVal)
Exit For
End if
Next
I want to have this macro linked to a button and have the macro find and highlight the next instance each time the button is clicked regardless if that instance is on the same line or a new line. Basically, a Ctrl+F feature for the textbox. Thanks!
You can give this code a try (see comments for explanations):
Option Explicit
Private Sub CommandButton1_Click()
Static lastInstancePosition As Long ' use Static variable to have its value persist after Sub exiting
Dim instancePosition As Long
strVal = "Find me"
With myTextbox
instancePosition = InStr(lastInstancePosition + 1, .Text, strVal) 'search string occurrence starting from last found item
If instancePosition > 0 Then
.SetFocus 'bring focus back ti the textbox after it has been taken by the button
.SelStart = instancePosition - 1
.SelLength = Len(strVal)
lastInstancePosition = instancePosition ' update starting position for next search
End If
End With
End Sub

How to format each line in a textbox in VBA Excel

Can I loop through a Textbox for each line entered and format it? For example if the user entered:
Text1
Text2
I would like to output it as in a single line.
'Text1','Text2'
Also would be awesome if you can get the last comma to not display.
This is what you really need
Add a textbox to your sheet
Add this code on TextBox1_Change() event
Dim mystr
mystr = Split(Sheet1.TextBox1.Text, vbCrLf)
Sheet1.Range("A1") = "'" & Join(mystr, "','") & "'"
Right click to textbox and enable multiline option
Control + Enter to change line inside textbox
And you have the result that you want:

VBA Userform with Textbox - formatting the text

so I'm very new to VBA. I've created a very simple template that when opened, gives me a form to fill out which will insert text into a document through a commandbutton.
I'm trying to take it a step further a bit but am not sure how to go about bringing the code together. To insert the text, I'm using the bookmark feature. On my form, I have 4 Textboxes that act as options. If all 4 are filled in, the text looks like:
Option1Option2Option3Option4
I need it to look like:
Option1, Option2, Option3 and Option4
Not only that but I would like it so that the "and" is added depending on how many textboxes are filled in. For example, if I only have the first two filled it, I need it to look like:
Option1 and Option2
Does that make sense? Below is how it's structured currently. I would appreciate any pointers in moving forward.
Private Sub cmdSubmit_Click()
Application.ScreenUpdating = False
With ActiveDocument
.Bookmarks("Program1").Range.Text = TextBox1.Value
.Bookmarks("Program2").Range.Text = TextBox2.Value
.Bookmarks("Program3").Range.Text = TextBox3.Value
.Bookmarks("program4").Range.Text = TextBox4.Value
End With
Application.ScreenUpdating = True
Unload Me
End Sub
If these bookmarks are contiguous, there is no need for four bookmarks. Add the following module-level variables:
Private s As String, hasAnd As Boolean
Create a Sub which prepends the text of a textbox to the private variable, inserting a comma or and as appropriate:
Private Sub AppendText(txt As TextBox)
If Len(txt.Text) = 0 Then Exit Sub
If Len(s) = 0 Then
s = txt.Text
ElseIf Not hasAnd Then
hasAnd = True
s = txt.Text & " and " & s
Else
s = txt.Text & ", " & s
End If
End Sub
Call the subprocedure for each textbox in reverse order:
AppendText TextBox4
AppendText TextBox3
AppendText TextBox2
AppendText TextBox1
Then, use the value of s as the text of the bookmark:
ActiveDocument.Bookmarks("Program1").Range.Text = s