Copy active cell text to textboox with all formatting

stewart1

Board Regular
Joined
Feb 25, 2010
Messages
66
Hi to all.

I hope you and yours are all keeping well and safe in such challenging times. I am now working from home and looking after my family now schools have closed here.

I am hoping someone can help.

I am looking to run a macro that will copy all formatting contents of an active cell into my textbox. This includes font style, itallics, font colour. Basically what ever was used in terms of composing that test, it will ttransfer exactly to the textbox.

The textbox will allow the user to type freely with their text. It is then I hope to run a second macro that will take from the textbox and put back into the active cell once the user has finished. This again would include all types of text formatting.

Despite my best efforts I have found little code or it has been conflicting.

The following code has allowed for italic and font. I need it to account for further text variable styles.

VBA Code:
Sub passCharToTextbox()

    'select Textbox 1:
    ActiveSheet.Shapes.Range(Array("Textbox 1")).Select

    'set text:
    Selection.ShapeRange(1).TextFrame2.TextRange.Characters.Text = ActiveCell.Value

    'loop through characters in original cell:
    For i = 1 To Len(ActiveCell.Value)

        'add bold/italic to the new character if necessary:
        If ActiveCell.Characters(i, 1).Font.Bold = True Then
            Selection.ShapeRange(1).TextFrame2.TextRange.Characters(i, 1).Font.Bold = True
        Else
            Selection.ShapeRange(1).TextFrame2.TextRange.Characters(i, 1).Font.Bold = False
        End If
        If ActiveCell.Characters(i, 1).Font.Italic = True Then
            Selection.ShapeRange(1).TextFrame2.TextRange.Characters(i, 1).Font.Italic = True
        Else
            Selection.ShapeRange(1).TextFrame2.TextRange.Characters(i, 1).Font.Italic = False
        End If

    Next i

End Sub


Many thanks in advance for taking the time to look.

Stu
 

Excel Facts

Move date out one month or year
Use =EDATE(A2,1) for one month later. Use EDATE(A2,12) for one year later.
Instead of using If - Then - Else statements. e.g.
VBA Code:
       If ActiveCell.Characters(i, 1).Font.Bold = True Then
            Selection.ShapeRange(1).TextFrame2.TextRange.Characters(i, 1).Font.Bold = True
        Else
            Selection.ShapeRange(1).TextFrame2.TextRange.Characters(i, 1).Font.Bold = False
        End If

Try using
VBA Code:
Selection.ShapeRange(1).TextFrame2.TextRange.Characters(i, 1).Font.Bold = ActiveCell.Characters(i, 1).Font.Bold

You can shorten the code even more by using With

VBA Code:
    For i = 1 To Len(ActiveCell.Value)
         With  Selection.ShapeRange(1).TextFrame2.TextRange.Characters(i, 1).Font
               .Bold = ActiveCell.Characters(i, 1).Font.Bold
              .Italic = ActiveCell.Characters(i, 1).Font.Italic
        End With
    Next i
 
Upvote 0
Instead of using If - Then - Else statements. e.g.
VBA Code:
       If ActiveCell.Characters(i, 1).Font.Bold = True Then
            Selection.ShapeRange(1).TextFrame2.TextRange.Characters(i, 1).Font.Bold = True
        Else
            Selection.ShapeRange(1).TextFrame2.TextRange.Characters(i, 1).Font.Bold = False
        End If

Try using
VBA Code:
Selection.ShapeRange(1).TextFrame2.TextRange.Characters(i, 1).Font.Bold = ActiveCell.Characters(i, 1).Font.Bold

You can shorten the code even more by using With

VBA Code:
    For i = 1 To Len(ActiveCell.Value)
         With  Selection.ShapeRange(1).TextFrame2.TextRange.Characters(i, 1).Font
               .Bold = ActiveCell.Characters(i, 1).Font.Bold
              .Italic = ActiveCell.Characters(i, 1).Font.Italic
        End With
    Next i
Lovely thank you, that is helpful! Do you know the right vba for font colour. This is the real clincher as there could be a mixture of different font colours in the cell text that I would need to import to the textbox, write in, and export back again.

Big thanks so far! :)
 
Upvote 0
So I am trying with the new code from CounTepes which is fine, but when I add colour, underline and fontstyle the line breaks on colour :

VBA Code:
Sub passCharToTextbox2()

    'select Textbox 1:
    ActiveSheet.Shapes.Range(Array("Textbox 1")).Select

    'set text:
    Selection.ShapeRange(1).TextFrame2.TextRange.Characters.Text = ActiveCell.Value

    'loop through characters in original cell:
    
    For i = 1 To Len(ActiveCell.Value)
         With Selection.ShapeRange(1).TextFrame2.TextRange.Characters(i, 1).Font
               .Bold = ActiveCell.Characters(i, 1).Font.Bold
              .Italic = ActiveCell.Characters(i, 1).Font.Italic
              .Color = ActiveCell.Characters(i, 1).Font.Color
             .Underline = ActiveCell.Characters(i, 1).Font.Underline
             .FontStyle = ActiveCell.Characters(i, 1).Font.FontStyle
             
             
        End With
    Next i

Any advice here will be greatly appreciated.

Stu
 
Upvote 0

Forum statistics

Threads
1,214,957
Messages
6,122,472
Members
449,087
Latest member
RExcelSearch

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