Solved

add space to end of cell content

Posted on 2015-02-16
11
70 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:bbao
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
Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

 

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
 
LVL 32

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:bbao
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:bbao
ID: 40659730
better wait for the author's feedback about his 'big picture' to determine the best solution.
0
 
LVL 46

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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

Suggested Solutions

When you see single cell contains number and text, and you have to get any date out of it seems like cracking our heads.
Do you use a spreadsheet like Microsoft's Excel?  Have you ever wanted to link out to a non excel file on your computer or network drive?  This is the way I found to do it!
This Micro Tutorial will demonstrate in Google Sheets how to use the HYPERLINK function to create live links inside your spreadsheet.
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…

785 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