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
76 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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 48

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 48

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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
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 48

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 Webinar] The Cloud Skills Gap

As Cloud technologies come of age, business leaders grapple with the impact it has on their team's skills and the gap associated with the use of a cloud platform.

Join experts from 451 Research and Concerto Cloud Services on July 27th where we will examine fact and fiction.

Question has a verified solution.

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

Describes a method of obtaining an object variable to an already running instance of Microsoft Access so that it can be controlled via automation.
This article describes how to use a set of graphical playing cards to create a Draw Poker game in Excel or VB6.
This Micro Tutorial will demonstrate on a Mac how to change the sort order for chart legend values and decrpyt the intimidating chart menu.
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…

627 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