Text Boxes - Can VBA code copy my text form a cell and bold, etc.

Joyner

Well-known Member
Joined
Nov 15, 2005
Messages
1,202
Hello All,

I hope someone has a solution for me on this problem related to a Text Box and code. I have searched a lot here and found several things regarding code and text and text boxes, but nothing yet that can do what I want, and maybe it’s not possible? I should say that the text box maybe wouldn’t have been my first choice, but it is what I have to work with.

I want to paste/copy text to a textbox from a cell. The text is well over the 255 characters. I found the link to MS and code to loop through to post more that 255 characters. The text I have in the cell has returns in it (for formatting) if that matters. But the code below doesn’t seem to want to work:

Sub Cell_Text_To_TextBox()

' Dimension the variables.
Dim txtBox1 As TextBox
Dim theRange As Range, cell As Range
Dim startPos As Integer

' Set txtBox1 equal to the active sheet's TextBox object. You can
' replace the ordinal number with your text box name in quotes.
' For example: ActiveSheet.DrawingObjects("Text 1")
Set txtBox1 = ActiveSheet.DrawingObjects(1)

' Set a range on the active sheet equal to the range object text
' that you are interested in copying to the text box.
Set theRange = ActiveSheet.Range("A1:A10")

'Set the starting position for the text.
startPos = 1

' Create a For-Each construct to loop through the cells in the range.
For Each cell In theRange

' Populate the textbox with the cell values using the Characters
' method.
' Note: Chr(10) can be used to add a new line in the textbox for
' each cell.
txtBox1.Characters(start:=startPos, _
length:=Len(cell.Value)).Text = cell.Value & Chr(10)

' Update the startPos variable to keep track of where the next
' string of text will begin in the textbox.
startPos = startPos + Len(cell.Value) + 1

Next cell
End Sub


I have played with it, and it seems to work if I delete most of the text from the cell, but of course that defeats the whole looping thing. I tried to put my text in several cells (a range like the code shows), but that didn’t seem to work either (doesn’t seem to go to the next cell).

I have several questions for the experts regarding a solution.

First, without using a looping, code – I can click in the cell where all my text is and all of the text show in the formula bar (I know this will display a lot of characters). Then I can highlight all of the text (including the returns) and go to the text box and paste it in. This works great. Can’t I somehow do that with code?? It seems like if I could do that manually, I should be able to do it with code?

I have tried to record a macro of this and what it records is the actual text that is copied, so if the text changes (which it does, based on different circumstances, and this is the whole point), the code doesn’t capture the change; it just pastes the old recorded text. Is there a way to do this process with code? If so, then I could avoid the whole looping thing.

If that is not possible, then does anyone know why this code won’t loop through the text in my cell? Or is there better code out there to do what I want. Another question and point I would like to know, is I can change the starting position to start pasting the characters (startPos =xx). This will not affect the text/characters above the starting position, but deletes everything below it. Can that be changed or is that just the way it is?

ALSO, if the above isn’t asking too much, there is some text that I would like to bold; this doesn’t appear to be easy either. But if I found a way to loop through and paste my text, can I somehow, identify the text I want to bold? Or can I identify particular character positions to bold – I could have all words to be bolded spaced so they are in the same position each time if necessary (they are like headings or titles, etc).

Hopefully the short answer isn’t just – No, can’t do that with Text Boxes. But any help would be greatly appreciated.

Thanks much.
 

Excel Facts

How to change case of text in Excel?
Use =UPPER() for upper case, =LOWER() for lower case, and =PROPER() for proper case. PROPER won't capitalize second c in Mccartney
Did you find any solution to your issues?
I think I may be dealing with a similar problem.
Please let me know. Thank you.
 
Upvote 0
No, sorry I didn't find the solution I was looking for. I don't recall the exact method, but I used a different approach for my situation.
 
Upvote 0
Thank you for getting back to me.
I read your question a bit more closely, and I believe the following is the code you were looking for. It's not relevant to your original problem anymore, but in case you or anyone else needs it again, here it is.

This line of code assumes there is a text box on sheet 1 of the workbook, and that the desired text box is number 1 in the z-order. It also assumes the desired text is in cell A1.

Sheets(1).Shapes(1).TextFrame.Characters.Text = _
Range("A1").Value

If the desired text is spread out over range A1:A10, you'd best be off constructing the string in advance, so:

For Each cell in Range("A1:A10")
CompleteText = CompleteText & cell.Value
Next cell

Then, you'd use the above line, so:
Sheets(1).Shapes(1).TextFrame.Characters.Text = CompleteText
 
Upvote 0

Forum statistics

Threads
1,225,624
Messages
6,186,068
Members
453,336
Latest member
Excelnoob223

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