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:
Related
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)
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.
I've changed the textbox "enter key behavior", so that everytime I hit enter, textbox will go to the next line,
Project's Form View
When user Click the Button, subform show result for the first line, but then how to get result in every line user input.
Private Sub CountButton_Click()
Dim SQL As String
SQL = "SELECT database.Tracking, database.Date, DateDiff(""d"",[Date],Date()) As Aeging FROM database;"
Me.Query1_subform.Form.RecordSource = SQL
Me.Query1_subform.Form.Requery
End Sub
Do I need to use Loop or VbCrLf or something else to function the textbox
Split textbox value to an array and loop array to build comma-separated string or:
strIN = Replace(Me.textbox, vbCrLf, "','")
If InStrRev(strIN, "','") > 0 Then strIN = Left(strIN, Len(strIN)-3)
SQL = "SELECT database.Tracking, database.Date, DateDiff('d', [Date], Date()) As Aging " & _
"FROM database " & _
"WHERE Tracking IN('" & strIN & "');"
Using a textbox relies on users to be consistent with input - not starting input with a CR and no other extraneous characters input accidentally or otherwise.
More reliable alternative is a multi-select listbox of Tracking values. Code loops through selected items and builds comma-separated string.
I am trying to write a VLOOKUP in a cell as a string, with VBA. This means that I do not want the result to appear in the cell as a value, but I want the whole VLOOKUP expression instead (For this example : "VLOOKUP(C6,'[path_to_file.xlsm]OTD Table!$B:$F,4,0)"). The challenge is that the range argument of the VLOOKUP is a concatenation of a path (path_to_file.xlsm) that the user selects with a GetOpenFilename, and a string that specifies the tab in which the lookup table is located ("OTD Table!$B:$F,4,0").
The issue I am getting is very interesting :
When I print my expression in a Msgbox, the expression appears correctly. However, when I write it in a cell, the path mysteriously appears incorrectly.
Sub macro()
dim data_file_new as String
data_file_new = CStr(Application.GetOpenFilename(FileFilter:="Excel Workbooks (*.xls*),*.xls*", Title:="Select new data file")) ' The user selects the file
str_ = "=VLOOKUP(C6," & "'[" & data_file_new & "]OTD Table!$B:$F,4,0)" ' This will display the expression correctly
cells(1,10)="=VLOOKUP(C6," & "'[" & data_file_new & "]OTD Table!$B:$F,4,0)"' This will not display the same thing as in the messagebox above
end Sub
I hope one of you guys can make sens of this !
Because you're dropping a formula into a cell that you want to display as straight text, you have to be explicit with Excel and tag the text string to prevent interpreting it as a formula. The simplest way to do this is pre-pend the string with a single-quote "'".
Sub macro()
Dim data_file_new, str_ As String
str_ = "'=VLOOKUP(C6,'["
data_file_new = CStr(Application.GetOpenFilename(FileFilter:="Excel Workbooks (*.xls*),*.xls*", Title:="Select new data file")) ' The user selects the file
str_ = str_ & data_file_new & "]OTD Table!$B:$F,4,0)" ' This will display the expression correctly
ActiveSheet.Cells(1, 10).Value = str_
End Sub
Yeah either you'll need to set the string to add a single quote, or you'll need to change the numberformat of the cell to text (Cells(1,10).NumberFormat = "#")
Either of those should work.
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.