pass text attributes

  • Thread starter Thread starter Bert
  • Start date Start date
B

Bert

I want to copy text which includes some italicized text) from one cell to
another.
When I use the copy command, the italicized text is preserved, but when I do
..Cells(a,b)=.Cells(c,d) the italics are lost.
Is there a way, using this method to carry the text formatting as well?
 
If you want to assign values, like:

..cells(a,b).value = .cells(c,d).value

Then you'll have to assign the formatting you want to keep:

..cells(a,b).value = .cells(c,d).value
..cells(a,b).Font.Italic = .cells(c,d).font.italic
(for italicizing the whole cell)
 
That's part of the challenge with this: the whole cell is not italicised
only a few words out of the whole string.
Is there a way to preserve that?
Thanks.
Bert
 
You could loop through each character in the sending cell and apply the
italicized format to the same character in the receiving cell.

But I would use copy|paste.

Is there a reason you don't want to use that?
 
If you only have a few cells, this would work ok:

Option Explicit
Sub testme()
Dim iCtr As Long

With ActiveSheet
.Range("c3").Value = .Range("a1").Value
For iCtr = 1 To Len(.Range("a1").Value)
.Range("c3").Characters(Start:=iCtr, Length:=1).Font.FontStyle _
= .Range("a1").Characters(Start:=iCtr, Length:=1).Font.FontStyle
Next iCtr
End With

End Sub

But if you have lots and lots of cells (so lots of characters to inspect), this
type of thing can be really slow.
 
Thanks, Dave.
There aren't too many cells to check. Actually it happens in a range of
five cells per data entry session, so this would be managable.
There's a complicating factor with the copy-paste solution. The cell the
data is being copied to is actually in a different worksheet. When I first
tested it, I just tried copying from one cell to the cell next to it.
Copying to a different worksheet didn't carry the formating with it, even
using the special paste method.
So, I think I'll try to work with the subroutine you've included.
Bert
 
Try the copy|paste again.

If your worksheets were in different workbooks and each of the workbooks were
opened in separate instances of excel, you'll see that problem.

But if you open both workbooks in the same instance, I bet you'll see what you
want.
Thanks, Dave.
There aren't too many cells to check. Actually it happens in a range of
five cells per data entry session, so this would be managable.
There's a complicating factor with the copy-paste solution. The cell the
data is being copied to is actually in a different worksheet. When I first
tested it, I just tried copying from one cell to the cell next to it.
Copying to a different worksheet didn't carry the formating with it, even
using the special paste method.
So, I think I'll try to work with the subroutine you've included.
Bert
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top