Consulting

Results 1 to 5 of 5

Thread: Change alignment

  1. #1
    VBAX Regular
    Joined
    Oct 2020
    Posts
    24
    Location

    Change alignment

    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?

  2. #2
    VBAX Expert Logit's Avatar
    Joined
    Sep 2016
    Posts
    613
    Location
    .
    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

  3. #3
    VBAX Regular
    Joined
    Oct 2020
    Posts
    24
    Location
    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

  4. #4
    Knowledge Base Approver VBAX Wizard p45cal's Avatar
    Joined
    Oct 2005
    Location
    Surrey UK
    Posts
    5,872
    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.
    p45cal
    Everyone: If I've helped and you can't be bothered to acknowledge it, I can't be bothered to look at further posts from you.

  5. #5
    VBAX Regular
    Joined
    Oct 2020
    Posts
    24
    Location
    Quote Originally Posted by p45cal View Post
    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!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •