Copying Text from a textbox to a cell in excel through vba

  Kiến thức lập trình

The below is the code for copying the text from a textbox and paste its contents into a given cell while maintaining the proper formatting, but the thing is that it takes a lot of time to run.
I need a solution for reducing its time complexity so that it does not traverses character by character to copy the formatting for text.

This below is the vba macro code that does that task for now but takes a lot of time for large texts.

Private Sub CopycellFromTextbox(cell As Range, sheetName As String)
Dim textrange As TextRange2, tbox1 As Shape, fontType As Font2, cellfont As Font
Set tbox1 = Worksheets(sheetName).Shapes(“TextBox 2”): Set textrange = tbox1.TextFrame2.textrange
cell.Value = textrange.Characters.Text
For i = 1 To Len(cell.Value)
Set fontType = textrange.Characters(i, 1).Font
Set cellfont = cell.Characters(i, 1).Font
With fontType
cellfont.Bold = IIf(.Bold, True, 0) ‘add bold/
cellfont.Italic = IIf(.Italic, True, 0) ‘add italic/
cellfont.Underline = IIf(.UnderlineStyle > 0, 2, -4142) ‘add underline/
cellfont.Color = textrange.Characters(i, 1).Font.Fill.ForeColor.RGB ‘add Font color
cellfont.Size = .Size
End With
Next i
End Sub

Please help me find a way to reduce its time complexity

Theme wordpress giá rẻ Theme wordpress giá rẻ Thiết kế website

LEAVE A COMMENT