Center Across Selection Macro Button In Excel

Опубликовано: 11 Октябрь 2024
на канале: Excel Bytes
447
18

In this tutorial we are going to look at how to create a single button you can use to activate Center Across Selection. This is a trick I learned from Mynda Treacy at MyOnlineTrainingHub. The link, plus others, is included below. The VBA codes are also in the notes below

Myonlinetraininghub video:
https://www.myonlinetraininghub.com/c...

How to create your PERSONAL.XLSB workbook
https://support.microsoft.com/en-us/o...

How to get the Developer tab on your Ribbon:
https://support.microsoft.com/en-us/o...

Center Across Selection Tutorial:
   • Center Across Selection Vs Merge And ...  
To download the file and follow along, go to the link here:

https://app.box.com/s/wofq3wtg4r0u014...

Do you need help with a formula or an Excel project?
You can send me an e-mail to [email protected]
Including a sample file is always helpful!

You can donate to my channel through any of the following:

PayPal:
https://paypal.me/ExcelBytes?locale.x...

Zelle or Venmo:
Send to [email protected]

Here are the codes:

Sub CAS1()
Dim rng As Range
Dim cell As Range

' Check if any cells are selected
If Selection.Cells.Count = 1 Then
MsgBox "Please select multiple cells to apply the formatting.", vbInformation
Exit Sub
End If

' Store the selected range in a variable
Set rng = Selection

' Loop through each cell in the range
For Each cell In rng
' Check if the cell is merged
If cell.MergeCells Then
' Unmerge the cell
cell.MergeCells = False
End If

' Apply center across selection alignment
cell.HorizontalAlignment = xlCenterAcrossSelection
Next cell
End Sub

----------------------------------------------------------------------------------------------

Sub CAS2()
Dim rng As Range
Dim cell As Range
Set rng = Selection

For Each cell In rng

cell.HorizontalAlignment = xlCenterAcrossSelection
Next cell

End Sub