Auto Indent (format) the text based on a number in the cell next to it

gheyman

Well-known Member
Joined
Nov 14, 2005
Messages
2,347
Office Version
  1. 365
Platform
  1. Windows
I have a column "C" that has numbers in it (numbers between 0 and 15) The column starts at row 7

I want to go down row C and for every row that has a value I want to "Increase Indent" the text in Column B and D the number of time that equals the value In column C of that row.

So in Row 7 if the value in C7 is 2 I want to change the formatting of the content in B and D by Indenting it two times

I'm looking at a very old post where someone posted this code. But I am having trouble understanding it.

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
 

Excel Facts

Will the fill handle fill 1, 2, 3?
Yes! Type 1 in a cell. Hold down Ctrl while you drag the fill handle.
If B7 currently has an indent of 4 & C7 is 2. Should B7 become 2 or 8?
 
Upvote 0
Sorry for the delayed response. It should be 2. It is not meant to add onto any current Indent.
 
Upvote 0
In that case, try
Code:
Sub IndentCell()
   Dim Cl As Range
   For Each Cl In Range("C7", Range("C" & Rows.Count).End(xlUp))
      Union(Cl.Offset(, -1), Cl.Offset(, 1)).IndentLevel = Cl.Value
   Next Cl
End Sub
 
Upvote 0
Glad to help & thanks for the feedback
 
Upvote 0
I am new to the Editor in excel. It might be a silly question. Could anyone pls help me with the error that i get while using the above discussed code for the same problem statement.
Runtime error-1004
Application-defined or object-defined error

Sub IndentCell()
Dim Cl As Range
For Each Cl In Range("A3", Range("A" & Rows.Count).End(xlUp))
Union(Cl.Offset(, -1), Cl.Offset(, 1)).IndentLevel = Cl.Value
Next Cl
End Sub

Have shared an example table below.

indent Level in column c​
no of indents to be used in column b and c
1​
3
2​
3.1
2​
3.2
2​
3.3
3​
3.3.1
3​
3.3.2
3​
3.3.3
2​
3.4
 
Upvote 0
Please start a new thread for your question, explaining exactly what you are trying to do. Thanks
 
Upvote 0

Forum statistics

Threads
1,223,903
Messages
6,175,284
Members
452,630
Latest member
OdubiYouth

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