Search code examples
excelalignmentcellword-wrapvba

Excel Macro to change cell alignment, borders and wrap text if a value has been entered into another cell in the same row


I am trying to create an excel macro which updates the cell alignment, wrap text and borders of cells in each row if a value is entered into one cell within that specific row. For example, if a value is entered into cell A1, then I want the macro to update the wrap text, cell alignment and borders of cells A1:O1. Unfortunately, applying conditional formating to each row within the spreadsheet is rather cumbersome and only will handle updating the cell borders, so I think that a macro that can update all 3 cell formatting elements and dynamically searches the entire worksheet, would be best.

Thanks for your assistance!


Solution

  • I don't know how you want to trigger this macro, nor what exact formatting you want to apply, but here's what I would do:

    Private Sub Worksheet_Change(ByVal Target As Range)
        If Target.Column = 1 Then
            With Range(Target, Target.Offset(0,14)
                .HorizontalAlignment = xlCenter
                .WrapText = True
                .Font.Bold = True
                .Borders(xlEdgeBottom).LineStyle = xlContinuous
            End with
        end if
    End Sub
    

    Edit: Add button and Assign Macro window should appear. Select New and put the code there.

    For Each Target in Range(Cells(1,1), Cells(65536, 1).End(xlUp))
      If Target <> "" Then
         With Range(Target, Target.Offset(0,14)
            .HorizontalAlignment = xlCenter
            .WrapText = True
            .Font.Bold = True
            .Borders(xlEdgeBottom).LineStyle = xlContinuous
         End With
      End If
    Next