Format Indent based on a value in a cell

gheyman

Well-known Member
Joined
Nov 14, 2005
Messages
2,347
Office Version
  1. 365
Platform
  1. Windows
Can you write code that will indent a cell based on a number in another cell?

Column A will have a number in it between 1 and 7. Based on the number in that cell can I indent the text in Column C using VBA code?
 

Excel Facts

Create a chart in one keystroke
Select the data and press Alt+F1 to insert a default chart. You can change the default chart to any chart type
Thanks, but I not sure thats what I am looking for. I was thinking of putting it in VBA code so that the sheet formated each time it was activated or maybe a button.
 
Upvote 0
You can do this with a user-defined function.

If the indent level is in A1, and the cell to indent is B1, then in C1 (or any convenient column),

=SetIndent(B1,A1)

Code:
Function SetIndent(r As Range, ByVal Level As Long) As Variant
    ' shg 2008
    ' Sets the indent level of r from 0 to 15 and returns the indent level
 
    Dim cell As Range
 
    SetIndent = IIf(Level < 0, "Min is 0!", IIf(Level > 15, "Max is 15!", Level))
    If Level < 0 Then Level = 0 Else If Level > 15 Then Level = 15
 
    For Each cell In r
        With cell
            If Level - .IndentLevel Then .InsertIndent Level - .IndentLevel
        End With
    Next cell
End Function
 
Upvote 0

Forum statistics

Threads
1,224,616
Messages
6,179,908
Members
452,949
Latest member
beartooth91

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top