Add word & number count at start of listbox items in VB.net 2010 - vb.net

I have a multi-line textbox textbox1 and want to add the content to listbox1, but before each item I need to add "wordX=" where "X" is the item number.
Example of textbox1:
bob
gear
dog
etc.
Then listbox1 should have:
word1=bob
word2=gear
word3=dog
etc.
Currently I am using the line below to copy the textbox3 content to listbox1 but can't find how to add "word" and the proper number.
ListBox1.Items.AddRange(TextBox3.Text.Split(vbNewLine))

This is what I used to complete what you wanted.
Dim tbLines As String() = TextBox1.Text.Split(vbNewLine)
ListBox1.Items.Clear()
For i As Integer = 1 To tbLines.Length
ListBox1.Items.AddRange({"word" & i & "=" & tbLines(i - 1).Trim})
Next
I split the text box using the vbNewLine separator as you did. I then go through each index in that array to concatenate the string "word" with the i (current index) integer. I finish off with concatenating the "=" as well as the trimmed value in the listbox.

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)

How to count how many tabs are in a selection using Macro for formatting tables

I have to format a large document for a file that has been created from a PDF which is editable, so I know all the text is there.
The document is a series of tables. In Word the tables look pretty OK, but in some cases where there should be various cells there is just 1 and tabs have been used to align the text. So, it looks good, but if any of the text gets changed then the formatting will get messed up. I would like to have a macro that looks for cells with a tab, selects the cell, counts the number of tabs, divides the cell into the right number of cells and puts the text into the right cell. For example, a cell that contains "text 1 [tab]text 2[tab]text 3" would become 3 cells "text 1", "text 2" and "text 3".
I thought Word would be able to convert the text to a table, but when the text is already in a table it doesn't work.
If anyone has any suggestions as to how I might achieve this, then they would be much appreciated!
My main issue is not knowing how to count how many tabs are in a selection.
This function will return the number of Tabs in the given string.
Function CountTabs() As Integer
Dim Txt As String
Txt = "This is" & vbTab & "a test" & vbTab & "to count Tabs"
CountTabs = Len(Txt) - Len(Replace(Txt, vbTab, ""))
End Function
The tab character - Chr(9) - is replace with nothing and the number of tabs is the difference in character count before and after the replacement. Here is an implementation of the idea in a snippet.
Private Sub Snippet()
Dim Txt As String
Dim Count As Integer
Txt = "This is" & vbTab & "a test" & vbTab & "to count Tabs"
Count = Len(Txt) - Len(Replace(Txt, vbTab, ""))
MsgBox "There are " & Count & " tabs."
End Sub
Of course, how you get the text for the variable Txt is another story and, in the context of this forum, another question. Prophylactically, I advise against using the Selection object, however. Try to use the Range object instead.

Select name from one list box to another using add item error

Got an issue where I want to select a text from one list box and add the text into another list box. the error only seems to happen if text has a "'" within the text and VBA seems to split the text and add the remaining text to the next column. Also, I'm working with column in my listbox and my code should add each text from left to right. This is fine but I need the whole text (including the ".") instead of the text being split up.
Private Sub btnAddUser_Click()
Dim SelectUser, ItemString1, ItemString2 As String, ItemString3 As String
For Each SelectUser In lstusers.ItemsSelected
Debug.Print lstusers.Column(0, SelectUser)
Debug.Print lstusers.Column(1, SelectUser)
Debug.Print lstusers.Column(2, SelectUser)
ItemString1 = lstusers.Column(0, SelectUser)
ItemString2 = lstusers.Column(1, SelectUser)
ItemString3 = lstusers.Column(2, SelectUser)
Form_frmAS.lstAddedUsers.AddItem ItemString1, 0
Form_frmAS.lstAddedUsers.AddItem ItemString2, 1
Form_frmAS.lstAddedUsers.AddItem ItemString3, 2
Form_frmAS.lstAddedUsers.Requery
Next SelectUser
End Sub
Is there a way to make it work or do I need to find a workaround?
Many Thanks
in the ListBox.AddItem method, the second parameter is the position in the list, i.e. the row number and not the column number. You cannot add the columns one by one.
To make it work, set the Row Source Type of the second ListBox to Value List and change the code to
For Each SelectUser In lstusers.ItemsSelected
lstAddedUsers.AddItem lstusers.Column(0, SelectUser) & ";" _
& lstusers.Column(1, SelectUser) & ";" _
& lstusers.Column(2, SelectUser)
Next
If you don't specify the second parameter in AddItem, the new item will automatically be appended at the end of the list.

Move selected items from one listbox to another

I have two listboxes, named listbox1 and listbox2.
Listbox1 is populated using a SQL query, and contains two columns. Its first column contains values that have commas.
Listbox2 is set as a value list in the "row source type" attribute of the Access property sheet.
My goal is to copy selected items from listbox1 to listbox2 using a control button.
I also need to be aware that listbox1 records contain commas, which act as delimiters during copying. That particular issue has been resolved, though.
I have created two modules to accomplish the copying of selected records from one listbox to another:
Public Sub CopySelected(ByRef frm As Form)
Dim ctlSource As Control
Dim ctlDest As Control
Dim strItems As String
Dim intCurrentRow As Integer
Set ctlSource = Me!listbox1
Set ctlDest = Me!listbox2
For intCurrentRow = 0 To ctlSource.ListCount - 1
If ctlSource.Selected(intCurrentRow) = True Then
'must insert double quote around single quote to escape commas
strItems = strItems & "'" & ctlSource.Column(0, intCurrentRow) & "'" & ";"
Me!listbox2.AddItem (strItems)
End If
Next intCurrentRow
End Sub
And
Private Sub cmdAddSelected_Click()
CopySelected Me
End Sub
I do have the multi-select option on listbox1 set to "extended".
The current problem is that when I click my control button, only the first selection from listbox1 is copied over- with the caveat that it is copied multiple times (copied the same number as those selected records).
Clearly, there is a problem with my For-loop.
R is my main language, and I am only just learning VBA.
In your program you are concatenating all selected values with separator and adding the longer and longer string to Listbox2. The ; acts as a column separator for multicolumn listboxes, i.e. you are kind of "transposing" the selected values from Listbox1 to Listbox2 while the excess items (above the number of columns of Listbox2) are simply ignored. If you want to copy individual values into the single column Listbox2, do this:
For intCurrentRow = 0 To ctlSource.ListCount - 1
If ctlSource.Selected(intCurrentRow) Then
strItems = "'" & ctlSource.Column(0, intCurrentRow) & "'"
Me!listbox2.AddItem (strItems)
End If
Next intCurrentRow

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: