Sub ApplyAlignmentToRange()
Dim targetSheetName As String
targetSheetName = "Report Writer"
' Specify the target sheet
Dim targetSheet As Worksheet
On Error Resume Next
Set targetSheet = Worksheets(targetSheetName)
On Error GoTo 0
' Check if the target sheet exists
If Not targetSheet Is Nothing Then
' Specify the range to check (C12:ZZ1000 in this example)
Dim targetRange As Range
Set targetRange = targetSheet.Range("C12:ZZ1000")
' Check if the target range is not empty
If Not targetRange Is Nothing Then
' Disable screen updating for performance
Application.ScreenUpdating = False
' Call a separate subroutine to apply the alignment logic
ApplyAlignmentLogicToColumns targetRange
' Enable screen updating after processing
Application.ScreenUpdating = True
Else
MsgBox "Specified range is empty.", vbExclamation
End If
Else
MsgBox "Worksheet '" & targetSheetName & "' not found.", vbExclamation
End If
End Sub
Sub ApplyAlignmentLogicToColumns(rng As Range)
Dim col As Range
' Loop through each column in the specified range
For Each col In rng.Columns
' Check if the column contains any text
If Application.CountIf(col, "*") > 0 Then
' Align left for text columns
col.HorizontalAlignment = xlLeft
Else
' Align center for other columns
col.HorizontalAlignment = xlCenter
End If
Next col
End Sub