Solved

Excel Macro that addes spaces in front of data in a column based on the number in another column.

Posted on 2015-02-10
7
66 Views
Last Modified: 2016-02-11
I have one column with a number from 0-10, called Level, and another column called description.  I would like a macro that addes 1 space in the front of the description if the number in that row is 1, if it is 2 then I want it to add two spaces in front of the discription, so it looks indented.

example:

Level               Description
0                      Top Level
1                        Next level Dodad
2                          Next level after that.
3                           Two level 3's.
3                           Two level 3's.
4                             Next Level
1                        Another number one.
2                         Two level 2's
2                         Two level 2's
0
Comment
Question by:Smilesxl
7 Comments
 
LVL 29

Expert Comment

by:leonstryker
ID: 40601708
Technically you can do it with formulas by using  Concatenate and Vlookup function.

All you need to do is set up a range for a Vlookup table with numbers in the first columns and corresponding spaces in the second column. Then you can copy/paste a formula such as this in the third column next to your data:

=CONCATENATE(VLOOKUP(A1,J1:K3,2,FALSE)&B1)
0
 
LVL 46

Accepted Solution

by:
Martin Liss earned 500 total points
ID: 40601995
This assumes the data is in columns A and B.

Sub ShiftEm()
Dim lngLastRow As Long
Dim lngRow As Long

lngLastRow = Range("A1048576").End(xlUp).Row

Application.ScreenUpdating = False

For lngRow = 2 To lngLastRow
    Cells(lngRow, "B") = Space(Cells(lngRow, "A")) & Cells(lngRow, "B")
Next

Application.ScreenUpdating = True
End Sub

Open in new window

0
 
LVL 46

Expert Comment

by:Martin Liss
ID: 40602003
For greater emphasis you could do something like

Cells(lngRow, "B") = Space(Cells(lngRow, "A") * 4) & Cells(lngRow, "B")
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 29

Expert Comment

by:gowflow
ID: 40602891
Although above suggestions are fine I would suggest a different approach.

I would not use in this specific case a VBA solution as it is writing the result on top of the initial column where as if by any chance the macro is run an other time it will keep on adding spaces to already existing spaces.

I would propose to put in Col C2 this formula and drag it down and would couple this with hiding Col B so that you get the Col A that is the Level and then Col C that is the result.
=REPT(" ",A2)&B2

If and when you need to add data you simply unhide Col B and make your changes and then hide again and the C will show results.

pls see workbook.
gowflow
Indent.xlsx
0
 
LVL 2

Author Closing Comment

by:Smilesxl
ID: 40602893
Thank you!  I actually did use the "*4", it really gives it more emphasis.  Thanks again.
0
 
LVL 29

Expert Comment

by:gowflow
ID: 40602894
Too Late I guess :)
gowflow
0
 
LVL 46

Expert Comment

by:Martin Liss
ID: 40603587
You're welcome and I'm glad I was able to help.

In my profile you'll find links to some articles I've written that may interest you.
Marty - MVP 2009 to 2014
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
DBF to ... Converter 5 49
Automating Excel Weekly Report 13 64
Delete Text 7 35
Need quick fix on defining a variable 5 18
Improved? Move/Copy Add-in Replacement - How to avoid the annoying, “A formula or sheet you want to move or copy contains the name XXX, which already exists on the destination worksheet.” David Miller (dlmille)  It was one of those days… I wa…
A simple tool to export all objects of two Access files as text and compare it with Meld, a free diff tool.
This Micro Tutorial demonstrates how to create Excel charts: column, area, line, bar, and scatter charts. Formatting tips are provided as well.
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

863 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

26 Experts available now in Live!

Get 1:1 Help Now