Splitting a string of a multiline textbox by lines - excel

I want to split the text of a textbox after a specific amount of visible lines.
I've found some codes that "allows that", but all of them consider the lines by the "vbCrLf" parameter, but i want to split using the visible lines of a multiline textbox.
To make it more clear to understand, consider a multiline textbox with the following text:
"The history of textbooks dates back to civilizations of ancient history. For example, Ancient Greeks wrote texts intended for education. The modern textbook has its roots in the standardization made possible by the printing press. Johannes Gutenberg himself may have printed editions of Ars Minor, a schoolbook on Latin grammar by Aelius Donatus. Early textbooks were used by tutors and teachers, who used the books as instructional aids (e.g., alphabet books), as well as individuals who taught themselves."
When i use the Textbox.Linecount function it returns the number 6 because the textbox shows six lines (which depends on the size of the control), but if i use a function like strText = Split(TextBox.Text, vbCrLf) it will return 1, because there is only one vbCrLf. But i need to split the text into two textbox considering the visible lines of the control, something like what happens in page breaks of MS Word.
For a better visual explanation, please look at the attached image.
Example

Firstly, I'm not convinced there is a robust and elegant way to do this, but it was fun to experiment and it might be useful to you.
The following will split the contents of TextBoxInput into TextBoxPage1 and TextBoxPage2 breaking on the line number specified by PAGED_TEXT_BOX_LINES.
It uses the textbox itself to detect natural line breaks and thus implicitly caters for the size of the textbox, the font, etc.
The desired line count is hard coded as a constant - not doing this would require an alternative of calculating the line height of the textbox (requiring calculations based on the font metrics and the textbox's internal line-leading size).
It only handles two "pages". But the concept could be extended simply by repeating the process based on the remainder of text that ends up in TextBoxPage2.
Private Sub CommandButton1_Click()
Const PAGED_TEXT_BOX_LINES As Integer = 5
Dim text As String
Dim i As Long
Dim textLength As Long
Dim curLine As Integer
text = TextBoxInput.text
textLength = Len(text)
TextBoxPage1.SetFocus
'add characters of the input string until the first page textbox
' exceeds maximum line count
For i = 1 To textLength
TextBoxPage1.text = Mid$(text, 1, i)
If TextBoxPage1.LineCount > PAGED_TEXT_BOX_LINES Then
'retreat cursor until we reach previous line, so we can
' detect the word that wrapped
curLine = TextBoxPage1.curLine
Do While TextBoxPage1.curLine = curLine
TextBoxPage1.SelStart = TextBoxPage1.SelStart - 1
Loop
'the remaining text after the SelStart is what
' wrapped, so stop page 1 after SelStart
TextBoxPage1.text = Mid$(text, 1, TextBoxPage1.SelStart)
TextBoxPage2.text = Trim$(Mid$(text, TextBoxPage1.SelStart + 1))
Exit For
End If
Next i
End Sub

Related

Clean data in excel that comes in varying formats

I have an excel table that contain values in these formats. The tables span over 30000 entries.
I need to clean this data so that only the numbers directly after V- are left. This would mean that when the value is SV-51140r3_rule, V-4407..., I would only want 4407 to remain and when the value is SV-245744r822811_rule, I would only want 245744 to remain. I have about 10 formulas that can handle these variations, but it requires a lot of manual labor. I've also used the text to column feature of excel to clean this data as well, but it takes about 30 minutes to an hour to go through the whole document. I'm looking for ways that I can streamline this process so that one formula or function can handle all of these different variations. I'm open to using VBA but don't have a whole lot of experience with it and I am unable to use Pandas or any IDE or programming language. Help please!!
I've used text to columns to clean data that way and I've used a variation of this formula
=IFERROR(RIGHT(A631,LEN(A631)-FIND("#",SUBSTITUTE(A631,"-","#",LEN(A631)-LEN(SUBSTITUTE(A631,"-",""))))),A631)
Depending on your version of Excel, either of these should work. If you have the ability to use the Let function, it will improve your performance, as this outstanding article articulates.
If you're on a really old version of excel, you'll need to hit ctl shift enter to make array formula work.
While these look daunting, all these functions are doing is finding the last V (by this function) =SUBSTITUTE(RIGHT(SUBSTITUTE(A2,"V",REPT("🍄",999)),999),"🍄","") and then looping through each character and only returning numbers.
Obviously the mushroom 🍄 could be any character that one would consider improbable to appear in the actual data.
Old School
=TEXTJOIN("",TRUE,IF(ISNUMBER(MID(MID(SUBSTITUTE(RIGHT(SUBSTITUTE(A2,"V",REPT("🍄",999)),999),"🍄",""),
FIND("-",SUBSTITUTE(RIGHT(SUBSTITUTE(A2,"V",REPT("🍄",999)),999),"🍄","")),9^9),
FILTER(COLUMN($1:$1),COLUMN($1:$1)<=LEN(MID(SUBSTITUTE(RIGHT(SUBSTITUTE(A2,"V",REPT("🍄",999)),999),"🍄",""),
FIND("-",SUBSTITUTE(RIGHT(SUBSTITUTE(A2,"V",REPT("🍄",999)),999),"🍄","")),9^9))),1)+0),
MID(MID(SUBSTITUTE(RIGHT(SUBSTITUTE(A2,"V",REPT("🍄",999)),999),"🍄",""),
FIND("-",SUBSTITUTE(RIGHT(SUBSTITUTE(A2,"V",REPT("🍄",999)),999),"🍄","")),9^9),
FILTER(COLUMN($1:$1),COLUMN($1:$1)<=LEN(MID(SUBSTITUTE(RIGHT(SUBSTITUTE(A2,"V",REPT("🍄",999)),999),"🍄",""),
FIND("-",SUBSTITUTE(RIGHT(SUBSTITUTE(A2,"V",REPT("🍄",999)),999),"🍄","")),9^9))),1),""))
Let Function
(use this if you can)
=LET(zText,SUBSTITUTE(RIGHT(SUBSTITUTE(A2,"V",REPT("🍄",999)),999),"🍄",""),
TEXTJOIN("",TRUE,IF(ISNUMBER(MID(MID(zText,FIND("-",zText),9^9),
FILTER(COLUMN($1:$1),COLUMN($1:$1)<=LEN(MID(zText,FIND("-",zText),9^9))),1)+0),
MID(MID(zText,FIND("-",zText),9^9),
FILTER(COLUMN($1:$1),COLUMN($1:$1)<=LEN(MID(zText,FIND("-",zText),9^9))),1),"")))
VBA Custom Function
You could also use a VBA custom function to accomplish what you want.
Function getNumbersAfterCharcter(aCell As Range, aCharacter As String) As String
Const errorValue = "#NoValuesInText"
Dim i As Long, theValue As String
For i = Len(aCell.Value) To 1 Step -1
theValue = Mid(aCell.Value, i, 1)
If IsNumeric(theValue) Then
getNumbersAfterCharcter = Mid(aCell.Value, i, 1) & getNumbersAfterCharcter
ElseIf theValue = aCharacter Then
Exit Function
End If
Next i
If getNumbersAfterCharcter = "" Then getNumbersAfterCharcter = errorValue
End Function

Bolding multiple specific words based on a read in value in VBA

I am trying to bold specific words based on a string being read into vba code.
I have the string in read into vba and now want to pick out a word or words to bold them.
If the following was read into vba code: The boy ran down the street.
In the above sentence, what code would I use if I wanted to bold just the word boy? What would I use if I wanted to bold the words boy and the?
I have tried the following code but do not know how to modify it to my specific case.
https://code.adonline.id.au/vba-format-text-microsoft-word/
In short, I want code that will read in a string, search that string for x amount of words, and bold said words to be later exported.
Let me know if you need any more details and what I am trying to accomplish.
Excel is pretty good at formatting whole cells, but it's not very good at formatting parts of cells. One way that I've been able to do this is to turn the text into very basic HTML and then paste the text into a cell. Here's an example.
Public Sub BoldCertainWords()
Dim sSentence As String
Dim dobj As DataObject
Set dobj = New DataObject
sSentence = "The boy ran down the street"
sSentence = Replace$(sSentence, "boy", "<strong>boy</strong>")
sSentence = Replace$(sSentence, "the", "<strong>the</strong>")
dobj.SetText "<html>" & sSentence & "</html>"
dobj.PutInClipboard
Sheet1.Range("A1").Select
Sheet1.PasteSpecial "Unicode Text"
End Sub
You need to set a reference to the MS Forms 2.0 library to get the DataObject if you don't already have it.
Note also that case matters. This will bold the but not The. You could repeat for typical capitalization or get clever with how you find the words and build the HTML string.
Also note that this particular PastSpecial method is Worksheet method, not a Range method. You have to select the range first.

Add multiple signature blocks into excel document using vba

I'm using vba to create an excel document and fill it in dynamically (already completed and working perfectly). What I need is: to figure out how to add, size, position, and prefill (suggested signer, email, but not the signature itself) the signature block at multiple locations in this document.
I don't even know if this can be done with vba (my searches on the subject have been unhelpful), but I'm hopeful as it will save me a lot of time and tedious work in the future. Any help on this would be welcome.
You may want to place simple text boxes across defined cells (as anchor points) and fill it with some text. To get you started here's the bare minimum that you need:
the actual text box creating Sub which takes all info as parameters:
Sub CreateShapeText(NailToCell As Range, w_pt As Single, h_pt As Single, DTxt As String)
Dim TB As Shape
' create a text box shape
' note: shapes belong to worksheets, therefore we derive a WS from cell.parent
Set TB = NailToCell.Parent.Shapes.AddLabel(msoTextOrientationHorizontal, NailToCell.Left, NailToCell.Top, w_pt, h_pt)
' make its border visible
TB.Line.Visible = msoTrue
' switch off that annoying auto-resize when text is entered
TB.TextFrame2.AutoSize = msoAutoSizeNone
' enter text ... and yes - this object tree is crazy
TB.TextFrame2.TextRange.Characters.Text = DTxt
' as it should be - text is vertical bottom
' but to have more control over the TB, this could be a parameter, too
TB.TextFrame2.VerticalAnchor = msoAnchorBottom
End Sub
and you would call that from wherever in your code as in below example
Sub CallCreate()
CreateShapeText [A1], 132, 32, "sign: me"
CreateShapeText [C12], 132, 32, "sign: you"
End Sub
You take it from here and research what these objects can do for you (e.g. make dotted lines instead of solid for the frame, experiment with font sizes, alignments etc.) and come back with more questions in case ...

VBA: How to start and end a list, bulleted or numbered, in Word?

I just can't figure out how to get VBA to start a bulleted list in Word.
I've got some code that types out stuff into word, I can get font and paragraph formatting, no problem, but now I want to create a bulleted list. I've found the following code,
ListFormat.ApplyListTemplate ListTemplate:=ListGalleries(wdBulletGallery).ListTemplates(2)
which should create a bulleted list of the second standard type, but all I can determine is to use it with a 'Range' command which causes the entire document to have the list applied to it. What I'd like to do is have it applied just to the new line that I'm having the code type, and then, at some point, be able to turn the list off, to be able to continue without the list being applied.
Thanks!
This link should help you with your query:
VBA - Bullet Points
Basically this code applies it to a selection:
Selection.Range.ListFormat.ApplyBulletDefault
And this code adds it to the selected paragraph number (in this case paragraph 2):
Documents("MyDoc.doc").Paragraphs(2).Range.ListFormat _
.ApplyBulletDefault
This code applies the Bullet points to a range of paragraphs:
Set myDoc = ActiveDocument
Set myRange = myDoc.Range( _
Start:= myDoc.Paragraphs(3).Range.Start, _
End:=myDoc.Paragraphs(6).Range.End)
If myRange.ListFormat.ListType = wdListNoNumbering Then
myRange.ListFormat.ApplyBulletDefault
End If
Assuming you know the text that is being added, you can use the second example. If you don't know how many paragraphs are being added, then each time you create a new one, increment an integer by 1 and use that integer in the third example.
For Example:
Start:= myDoc.Paragraphs(2).Range.Start, _
End:=myDoc.Paragraphs(i).Range.End)

Format individual characters in a single Excel cell with python

I am using xlrd, xlwt, and xlutils on the Windows Vista OS with Python 2.7. I have a set of DNA sequences in an excel worksheet that are 100 characters long, with each sequence in a single cell. I am trying to highlight characters at specific positions within each of these sequences in excel (bold them or change color), but have not found a way to format individual characters within a cell. Applying a style applies it to the entire cell to my knowledge. Therefore I am trying to break the sequences down into individual components where some parts of the sequence will be modified with a style while others won't, and to then collate these back together into a single string in a single cell.
Code:
rb = open_workbook('Mybook', formatting_info=True)
rs = rb.sheet_by_index(0)
wb = copy(rb)
ws = wb.get_sheet(0)
minus35style = xlwt.easyxf('font: bold 1') # style I'd like for just one character
for b in range(0, 368, 1):
rscellin = rs.cell(b,9)
f = rscellin.value
tominus35 = str(f[0:34])
minus35 = str(f[35:36])
ws.write(b, 14, tominus35)
ws.write(b, 14, minus35, minus35style)
wb.save('Mybook')
My problem is that adding a style changes the whole cell, and I want just certain characters to be modified. Additionally, subsequent writing to the same cell overwrites what was there previously. Is there a way to modify individual characters in a single cell, or to add differently formatted text to an existing cell that already has text in it?
Please let me know if you require additional information that I've overlooked. I appreciate your time in advance.
Brett
Recent versions of xlwt include the ability to use Rich Text within a cell. Where normally you would use ws.write, use ws.write_rich_text instead. The first two parameters are the row index and column index, as usual; but the next parameter is a sequence of components. Each component can either be a "naked" text value or a (text, font) pair. The naked text values will use the font from the cell's overall style, which can be specified using the optional fourth parameter.
For the (text, font) pairs, it is simplest to generate fonts using the new easyfont feature, which is kind of like easyxf but only for fonts. Here is an example:
import xlwt
wb = xlwt.Workbook()
ws = wb.add_sheet('Sheet1')
font0 = xlwt.easyfont('')
font1 = xlwt.easyfont('bold true')
font2 = xlwt.easyfont('color_index red')
style = xlwt.easyxf('font: color_index blue')
seg1 = ('bold', font1)
seg2 = ('red', font2)
seg3 = ('plain', font0)
seg4 = ('boldagain', font1)
ws.write_rich_text(2, 5, (seg1, seg2, seg3, seg4))
ws.write_rich_text(4, 1, ('xyz', seg2, seg3, '123'), style)
wb.save('rich_text.xls')
You should be able to adapt the above for your purposes. Note that you still have to write or overwrite the whole cell at once; you can't go back and update only part of a cell later.

Resources