Auto-Center as you go

paquirl

Board Regular
Joined
Oct 12, 2010
Messages
226
Office Version
  1. 2016
Platform
  1. Windows
Looking for code to add to "This Workbook" module which will auto center text (normal centering horizontally) in every column of every sheet as you input data, so you never have to manually center a column.
 
There's probably a more elegant way to do this, and I'm not sure why the action would ever need to be repeated so often but this should accomplish what you're looking for:

Code:
Private Sub Workbook_SheetChange(ByVal Sh As Object, ByVal Target As Range)

    Application.ScreenUpdating = False
    
    ActiveWorkbook.Names.Add Name:="MyOrigPlace", RefersTo:=Selection
    
    Cells.Select
    With Selection
        .HorizontalAlignment = xlCenter
        .VerticalAlignment = xlBottom
        .WrapText = False
        .Orientation = 0
        .AddIndent = False
        .IndentLevel = 0
        .ShrinkToFit = False
        .ReadingOrder = xlContext
        .MergeCells = False
    End With

    Application.Goto Reference:="MyOrigPlace"
    ActiveWorkbook.Names("MyOrigPlace").Delete
    
    Application.CutCopyMode = False
    Application.ScreenUpdating = True
    
End Sub


I'm not sure how all those could have failed... In the original one that I posted, you literally cannot input anything without it being horizontally centered automatically. It even centers everything that wasn't centered previously. Any added sheets act the same way, and even a copy/paste realigns to center...

If you have placed the above code in "ThisWorkbook" and have macros activated, there is no reason it would not work

Samesies for pretty much all the other solutions suggested to you as well.
 
Upvote 0

Excel Facts

Excel motto
Not everything I do at work revolves around Excel. Only the fun parts.

Forum statistics

Threads
1,215,762
Messages
6,126,738
Members
449,334
Latest member
moses007

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