PDA

View Full Version : Change alignment



Borut
10-23-2020, 11:24 AM
Hello,

I need vba in excel.
When I put value into cell (if this value is 5),
then this cell must be right alignment.
With sub change works, but
alight all numbers to right, not only number 5.
Activecell not reconize, that is intered number 5.
Can somevone help me?

Logit
10-23-2020, 07:29 PM
.
Paste this in the Sheet Module :



Option Explicit

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Dim cell As Range
Dim lr As Long

lr = Cells(Rows.Count, 1).End(xlUp).Row

For Each cell In Range("A1:A" & lr)

Select Case cell.Value

Case Is = 5
cell.HorizontalAlignment = xlRight

Case Else
cell.HorizontalAlignment = xlCenter

End Select

Next cell

End Sub

Borut
10-24-2020, 02:37 AM
Thanks,

but it takes too long tme beacuse o my range range isE9:AI28. Each row has about 6 to 8 numbers 5
It is possible o aiignmet only cell where I put number 5?

Regads

p45cal
10-24-2020, 05:26 AM
try:
Private Sub Worksheet_Change(ByVal Target As Range)
Dim cll As Range, rngeToProcess As Range

Set rngeToProcess = Intersect(Target, Range("E9:AI28"))
If Not rngeToProcess Is Nothing Then
For Each cll In rngeToProcess
Select Case cll.Value
Case Is = 5
cll.HorizontalAlignment = xlRight
Case Else
cll.HorizontalAlignment = xlCenter
End Select
Next cll
End If
End Sub
It will only process cells that you change within E9:AI28.

Borut
10-25-2020, 11:45 AM
try:
Private Sub Worksheet_Change(ByVal Target As Range)
Dim cll As Range, rngeToProcess As Range

Set rngeToProcess = Intersect(Target, Range("E9:AI28"))
If Not rngeToProcess Is Nothing Then
For Each cll In rngeToProcess
Select Case cll.Value
Case Is = 5
cll.HorizontalAlignment = xlRight
Case Else
cll.HorizontalAlignment = xlCenter
End Select
Next cll
End If
End Sub
It will only process cells that you change within E9:AI28.

It works!

Thanks!