Thread: [Solved] auto-indent
View Single Post
 
Old 06-14-2013, 04:52 PM
BobBridges's Avatar
BobBridges BobBridges is offline Windows 7 64bit Office 2010 32bit
Expert
 
Join Date: May 2013
Location: USA
Posts: 700
BobBridges has a spectacular aura aboutBobBridges has a spectacular aura about
Default

Yeah, I think that can be done, mickeyd. One question: There's an indent feature in Excel; would you rather use that, rather than inserting actual spaces into the data?

If you plan to export the data to some other format, and want the new leading spaces, then you probably have to use actual spaces as you described. But if you want to keep the data in Excel then it may not matter — and if you will be manipulating the data later, and the spaces would mess you up, then it's probably better to use Excel indentation.

To add spaces, the simplest way would be something like this:

' so is the current worksheet.
' jr is the current row.
so.Cells(jr, 2) = Left(" ",so.Cells(jr, 1) - 1) & so.Cells(jr, 2)

That looks at so.Cells(jr, 1) (the indentation value); subtracts 1 from it; cuts a string of spaces down to that many in length; and sticks that in front of the current value of so.Cells(jr, 2).

But if you want to use Excel indentation, it looks more like this:

' so is the current worksheet.
' jr is the current row.
so.Cells(jr, 2).IndentLevel = so.Cells(jr, 1) - 1

Now, if that's enough for you to go on with, go to it. But if you need help writing the actual macro itself, let me know and I'll coach you.
Reply With Quote