Indent VBA Code debug error

gheyman

Well-known Member
Joined
Nov 14, 2005
Messages
2,341
Office Version
  1. 365
Platform
  1. Windows
Run0time error 1004 : Unable to set Indentlevel property of the range class

Someone kindly gave me this code and I am trying to use it in a new spreadsheet. it does indent the rows but them it debugs with the message above. Anyone know why?

Code:
Sub Indent()
'
    Dim Ind As Range
'
    'this is where the Level is listed
        For Each Ind In Range("C9", Range("C" & Rows.Count).End(xlUp))
    'these are which rows to indent
        Union(Ind.Offset(, -1), Ind.Offset(, 2)).IndentLevel = Ind.Value
    '
    Next Ind
'
End Sub
 

Excel Facts

How to fill five years of quarters?
Type 1Q-2023 in a cell. Grab the fill handle and drag down or right. After 4Q-2023, Excel will jump to 1Q-2024. Dash can be any character.
Run0time error 1004 : Unable to set Indentlevel property of the range class

Someone kindly gave me this code and I am trying to use it in a new spreadsheet. it does indent the rows but them it debugs with the message above. Anyone know why?

Code:
Sub Indent()
'
    Dim Ind As Range
'
    'this is where the Level is listed
        For Each Ind In Range("C9", Range("C" & Rows.Count).End(xlUp))
    'these are which rows to indent
        Union(Ind.Offset(, -1), Ind.Offset(, 2)).IndentLevel = [COLOR="#FF0000"][B]Ind.Value[/B][/COLOR]
    '
    Next Ind
'
End Sub
One reason would be a non-numerical value (eg text or error) in column C. When you get the error, click Debug then hover your cursor over the red text above and see what value is reported.
 
Upvote 0
Ind.Value = "" which would make sense. There is a formula in column C that actual determines the indent level and will result in a "" value depending on conditions.
 
Last edited:
Upvote 0
Ind.Value = "" which would make sense. There is a formula in column C that actual determines the indent level and will result in a "" value depending on conditions.
Yep, that would cause the error.
I presume that a value of "" in column C means no indent, so could you just alter that formula to return 0 instead of "" for those conditions?
 
Upvote 0
No, unfortunately it needs to be Null and not a value. If there is a way to stop the code when it hits " "" " that would be ideal. Not sure that's possible
 
Upvote 0
No, unfortunately it needs to be Null and not a value. If there is a way to stop the code when it hits " "" " that would be ideal. Not sure that's possible
This should skip those rows with "" in column C
Code:
If IsNumeric(Ind.Value) Then Union(Ind.Offset(, -1), Ind.Offset(, 2)).IndentLevel = Ind.Value
 
Upvote 0
Wow, thanks! That worked. Very much appreciated
 
Upvote 0

Forum statistics

Threads
1,215,267
Messages
6,123,964
Members
449,137
Latest member
yeti1016

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