Question : Putting functions in a VBA Macro in Excel or indenting as a macro.

Morning,

I'm trying to create a VBA Macro in Excel that indents the contents of a cell based upon the value of the cell next to it and then runs down a column doing the same for each pair of cells.

Example:

Cell A1  = 3, Cell B1 needs to be indented 3 spaces.
Cell A2 =1, Cell B2 needs to be indented 1 space.

I'm stuck trying to work out how to put an =Cell function within a Macro and unfortunately for me, I'm a complete novice at VBA.

    Range("B1").Select
    With Selection
        .HorizontalAlignment = xlLeft
        .VerticalAlignment = xlBottom
        .WrapText = False
        .Orientation = 0
        .AddIndent = False
        .IndentLevel = ( EQUALS FUNCTION HERE)
        .ShrinkToFit = False
        .ReadingOrder = xlContext
        .MergeCells = False
    End With

Is this even possible, or is there a better way to do what I'm after?

Many Thanks

Will

Answer : Putting functions in a VBA Macro in Excel or indenting as a macro.

Range("B1").Select
    With Selection
        .HorizontalAlignment = xlLeft
        .VerticalAlignment = xlBottom
        .WrapText = False
        .Orientation = 0
        .AddIndent = False
        .IndentLevel = Range("A1").Value
        .ShrinkToFit = False
        .ReadingOrder = xlContext
        .MergeCells = False
    End With

or if you want to do lots of cells:
For Each rngCell in Selection
    With rngCell
        .HorizontalAlignment = xlLeft
        .VerticalAlignment = xlBottom
        .WrapText = False
        .Orientation = 0
        .AddIndent = False
        .IndentLevel = .Offset(0,-1).Value
        .ShrinkToFit = False
        .ReadingOrder = xlContext
        .MergeCells = False
    End With
Next rngCell

just select the cells in column B before running the macro.
HTH
Rory
Random Solutions  
 
programming4us programming4us