Solved

add space to end of cell content

Posted on 2015-02-16
11
68 Views
Last Modified: 2016-02-12
Hi! I'm looking for a quick and easy way to add spaces to the end of the content of every cell in column D of a worksheet. Cell format is general and I just want to go to the end of the content and add a single space.

Thank you!
0
Comment
Question by:behest
11 Comments
 
LVL 23

Accepted Solution

by:
Michael74 earned 500 total points
ID: 40613318
Add this macro to your workbook and run it once

Sub AddSpace()
    Dim i As Long
    For i = 1 To Range("D" & Rows.Count).End(xlUp).Row
        Range("D" & i).Value = Range("D" & i).Value & " "
    Next
End Sub

Open in new window

0
 
LVL 37

Expert Comment

by:Bing CISM / CISSP
ID: 40613324
or add one more column with formular =D1&" " and populate it to the following rows, then hide column D and only show the added column?
0
 
LVL 23

Expert Comment

by:Michael74
ID: 40613330
Instead of hiding column D for @bbao's solution just copy the the column with the formula above and then paste special> values into column D and delete the formula column
0
 

Author Comment

by:behest
ID: 40613331
Set up a separate macro as you defined. When I attempt to run it, I get the following error:

Run-time error '1004':

Application-defined or object-defined error.

Suggestions?
0
 
LVL 23

Expert Comment

by:Michael74
ID: 40613369
Not sure what has happened, I have tested and it works fine. Here is a sample file so you can see it in action
Example.xlsm
0
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 31

Expert Comment

by:Rob Henson
ID: 40613795
Looking at bigger picture, why do you need a space on the end of cell contents??

Thanks
Rob H
0
 
LVL 37

Expert Comment

by:Bing CISM / CISSP
ID: 40613801
> Looking at bigger picture, why do you need a space on the end of cell contents??

+1
0
 
LVL 10

Expert Comment

by:broro183
ID: 40613915
I agree with Rob H too, give us more context & this intermediate step may be able to be removed once we understand your complete requirement.

In saying that, here is a slight alternative to Michael's version:
Option Explicit

Sub AddSpaceToRng()
'10/07/2011, RB: written as "Sub TrimRng()" - see commented out line within the loops
Const Space_Chr As String = " "
Dim Rng As Range
Dim rngArr As Variant
Dim NumOfRows As Long
Dim NumOfCols As Long
Dim RowInd As Long    'Row Index for looping
Dim ColInd As Long    'Column Index for looping

    With ActiveSheet
        'Set Rng = Selection
        Set Rng = .Range("d1:d" & .Range("D" & .Rows.Count).End(xlUp).Row)
    End With

    With Rng
        'rngArr = .Cells
        rngArr = .Cells.Value2
        NumOfRows = .Rows.Count
        NumOfCols = .Columns.Count
    End With
    For RowInd = 1 To NumOfRows
        For ColInd = 1 To NumOfCols
            'rngArr(RowInd, ColInd) = Trim$(rngArr(RowInd, ColInd))
            rngArr(RowInd, ColInd) = rngArr(RowInd, ColInd) & Space_Chr
        Next ColInd
    Next RowInd
    'Rng = rngArr
    Rng.Value2 = rngArr
    MsgBox "done"
    Set Rng = Nothing
End Sub

Open in new window


hth
Rob
0
 
LVL 37

Expert Comment

by:Bing CISM / CISSP
ID: 40659730
better wait for the author's feedback about his 'big picture' to determine the best solution.
0
 
LVL 45

Expert Comment

by:Martin Liss
ID: 40667528
This question has been classified as abandoned and is closed as part of the Cleanup Program. See the recommendation for more details.
0

Featured Post

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
This Micro Tutorial demonstrates in Microsoft Excel how to consolidate your marketing data by creating an interactive charts using form controls. This creates cool drop-downs for viewers of your chart to choose from.
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…

757 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

22 Experts available now in Live!

Get 1:1 Help Now