Correct code for coloring a userform inkEdit textbox text string with the text string on multiple lines in the code

chazrab

Well-known Member
Joined
Oct 21, 2006
Messages
917
Office Version
  1. 365
Platform
  1. Windows
As stated above. This code does the correct job of coloring the text string - as long as the string in the code is on ONE line:
Code:
Private Sub UserForm_Activate()
Dim firstpost, lastpos As Integer
inkText.Text = "And He said to them," & Chr(34) & "Do you not see all these things? Truly I say to you, not one stone here will be left upon another, which will not be torn down." & Chr(34) & "Matthew 24:2 (NASB)"
inkText.SelStart = 0
inkText.SelLength = Len(inkText.Text)
inkText.SelColor = vbBlue
inkText.SelFontSize = 18
firstpos = InStr(1, inkText.Text, Chr(34))
lastpos = InStr(firstpos + 1, inkText.Text, Chr(34))
inkText.SelStart = firstpos
inkText.SelLength = lastpos - firstpos - 1
inkText.SelColor = vbRed
inkText.SelLength = 0
End Sub
[code]
image below show result of this code.  I have a lot of text I wish to color red.  Its impossible to put a very long text string on one line of code
When I try to separate a long text string into shorter line segments in the code, I keep getting "Expected end of statement" messages.  

The bolded code below is what I want to accomplish with very long text strings, achieving the same result as the image displayed with the text string on one line.
Its a matter of separating a long string into multiple lines and still have the correct punctuation to display the entire text string in vbred.

This code gives that error message and this text is colored red
[code]
Dim firstpost, lastpos As Integer
inkText.Text = "And He said to them," _
 & Chr(34) & "Do you not see all these things?" _
 Truly I say to you, not one stone here will be left"
upon another, which will not be torn down." & Chr(34) & _
"Matthew 24:2 (NASB)"
[generates "Expected end of statement erro"r]


inkText.SelStart = 0
inkText.SelLength = Len(inkText.Text)
inkText.SelColor = vbBlue
inkText.SelFontSize = 18
firstpos = InStr(1, inkText.Text, Chr(34))
lastpos = InStr(firstpos + 1, inkText.Text, Chr(34))
inkText.SelStart = firstpos
inkText.SelLength = lastpos - firstpos - 1
inkText.SelColor = vbRed
inkText.SelLength = 0

Using this string as a model, how do you put this text string on any number of multiple lines and still have it displayed correctly in red ?

Please help if you can. Thanks very much
Thanks, cr
 

Attachments

  • CORRECT TEXT STRING COLOR BEGINNING AND ENDING RED TEXT.png
    CORRECT TEXT STRING COLOR BEGINNING AND ENDING RED TEXT.png
    36.5 KB · Views: 19
Here is code that works using double quotes as the markers, text in inkTest comes from C2 on sheet as per first sub:
VBA Code:
Private Sub UserForm_Activate()
inkText.Text = Sheets("MATT24").Range("C2")

End Sub
I put a command button on the form to trigger the code:
VBA Code:
Private Sub cmdColourText_Click()
Dim intCount As Integer, n As Integer
Dim lngPos1 As Long, lngPos2 As Long

strIn = Sheets("MATT24").Range("C2")
''strIn = Me.inkText 'n/w
intCount = Len(strIn) - Len(Replace(strIn, """", ""))
lngPos1 = 1

With Me.inkText
    .SelLength = Len(inkText)
    .SelColor = vbBlue
    .SelLength = 0
End With

For n = 1 To intCount Step 2
    lngPos1 = InStr(lngPos1, strIn, """")
    lngPos2 = InStr(lngPos1 + 1, strIn, """") - 1
    With Me.inkText
        .SelStart = lngPos1
        .SelLength = lngPos2 - lngPos1
        .SelColor = vbRed
        .SelLength = 0
    End With
    lngPos1 = lngPos2 + 2
Next

End Sub
It does not work if I refer to the text showing in the inkEdit control after moving through listbox items, probably because your code adds line wrap characters when the > button is clicked. If you must have them, then you'll have to figure out how to incorporate them in the loop counter as they may or may not be there. Personally I would ditch the idea of manually wrapping text as I think I mentioned before.

Maybe you are aware of some of the following, which I point out, not to be critical but to let you know there are potential issues:

I like to check my code to see if it compiles. To do that I had to comment a LOT of your code. In some cases the object didn't exist, in some it was about missing code elements (like End Sub). There must have been 75 errors, no lie.

Lots of quotations are missing leading or trailing quotes or both. I also saw a double quote after a citation (calling it that because I don't know what NASB is) but I can't find it now. Any issues like that are going to cause the code to not perform as expected. Other verses have trailing characters (.eg. ")" ) but that wouldn't affect the output.

Look at C2 and D5 quotes - they are not the same (C2 is ASCII 34, D5 is ASCII 147). C2 is what I coded for and it works as I showed, but I typed directly in the vb editor so I don't know if I got lucky with a match or not. I expect smart quotes (D5 et al) will cause the code to fail.

I think you've still got a lot of work ahead of you and that this is about as far as I can go with you. Good luck with your project.

EDIT - IMO, most developers advocate that every module start with Option Explicit by default (this is an editor option). Some go so far as to say if you don't have that, you deserve all the grief that you get. You could does not have it.
Micron thanks for all your help. Will get to work on this right now.
cr
 
Upvote 0

Excel Facts

Using Function Arguments with nested formulas
If writing INDEX in Func. Arguments, type MATCH(. Use the mouse to click inside MATCH in the formula bar. Dialog switches to MATCH.

Forum statistics

Threads
1,223,896
Messages
6,175,263
Members
452,627
Latest member
KitkatToby

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top