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
68 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
Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

 
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

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

This tutorial explains how to create a series of drop-down lists that are dependent upon prior selections to guide (“force”) the user to make the correct selection and reduce data errors within Microsoft Excel. Excel 2010 was used for this tutorial;…
This code takes an Excel list of URL’s and adds a header titled “URL List”. It then searches through all URL’s in column “A”, looking for duplicates. When a duplicate is found, it is moved to the top of the list. The duplicate URL’s are then highlig…
Graphs within dashboards are meant to be dynamic, representing data from a period of time that will change each time the dashboard is updated with new data. Rather than update each graph to point to a different set within a static set of data, t…
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…

786 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