force alignment?

  • Thread starter Thread starter Art
  • Start date Start date
A

Art

I know how to format a cell so the alignment is centered both vertically and
horizontally.

However, if a user copies/pastes text from, say, Microsoft Word into a cell
in Excel, it positiions it to the left at the bottom of the cell. Not a big
deal, really, but I would like for all of the cells to be centered
horizontally and vertically.

Is there a way to force a cell to align its contents centered, even if it is
copied and pasted from another document, rather than manually entered with a
cursor inside the cell? (Perhaps a macro could check a cell once the enter
key is pressed or the focus leaves a cell in some way?)

Thanks!
 
Art

The paste options button allows you to "match destination" formatting.

If you don't want to use that you can use VBA.

Try this event code which retains all cell formatting when something is
copy/pasted into it.

Cells must be pre-formatted to centered and centered as you state.

Private Sub Worksheet_Change(ByVal Target As Range)
Dim myValue
With Application
.EnableEvents = False
myValue = Target.Value
.Undo
Target = myValue
.EnableEvents = True
.CutCopyMode = False
End With
End Sub

This is sheet event code. Right-click on the sheet tab and "View Code".

Copy/paste the code into the module.

Operates only on the one sheet.

If you wanted for all sheets use this code which is to placed in
Thisworkbook module, not a sheet module.

Private Sub Workbook_SheetChange(ByVal Sh As _
Object, ByVal Target As Range)
Dim myValue
With Application
.EnableEvents = False
myValue = Target.Value
.Undo
Target = myValue
.EnableEvents = True
.CutCopyMode = False
End With
End Sub


Gord Dibben MS Excel MVP
 
To start, I pasted the code just in the sheet. However, when I copied/pasted
text from Word then moved to another cell, it didn't keep the formatting. I
was very excited about this working...am I missing something?

Thanks for the work...and look forward to hearing back from you!!!
 
Does this matter:

When a user pastes text from Word into the cell in Excel, they aren't
double-clicking in the cell. They click/select the cell, then paste (usually
CTRL-V). I don't have a problem with formatting being maintained if the user
double-clicks in the cell first, but this doesn't usually happen.

Will the VBA you suggested work if users do not double-click in the cell
(giving them a cursor)?
 
I have tested by copying strings of text from a Word document.

Back to Excel and paste into a cell.

Alignment stayed as Center/Center as I had originally formatted.

What do you mean by "moved to another cell"?


Gord
 
Double-clicking on the cell puts the user into edit mode and they are
essentially pasting into the formula bar.

Either method of pasting works for me.

I rarely use the double-click and paste.

I generally paste directly to the cell. CTRL + V or Edit>Paste or just hit
Paste button.


Gord
 
Either use the TAB key or press the enter key.

Gord Dibben said:
I have tested by copying strings of text from a Word document.

Back to Excel and paste into a cell.

Alignment stayed as Center/Center as I had originally formatted.

What do you mean by "moved to another cell"?


Gord



.
 
If you want to email me the workbook I'll have a look.

gorddibbATshawDOTca change the obvious.


Gord
 
Back
Top