Go Premium for a chance to win a PS4. Enter to Win

x
?
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
Medium Priority
?
79 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 49

Accepted Solution

by:
Martin Liss earned 2000 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 49

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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 31

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 31

Expert Comment

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

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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Access developers frequently have requirements to interact with Excel (import from or output to) in their applications.  You might be able to accomplish this with the TransferSpreadsheet and OutputTo methods, but in this series of articles I will di…
Windows Explorer lets you open cabinet (cab) files like any other folder. In VBA you can easily handle normal files and folders, but opening and indeed creating cabinet files takes a lot more - and that's you'll find here.
The viewer will learn how to create two correlated normally distributed random variables in Excel, use a normal distribution to simulate the return on different levels of investment in each of the two funds over a period of ten years, and, create a …
This Micro Tutorial will demonstrate in Microsoft Excel how to add style and sexy appeal to horizontal bar charts.

824 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