Solved

add space to end of cell content

Posted on 2015-02-16
11
69 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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: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 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Exit the loop 4 35
macro for closing opened workbook 6 19
Manually enter date in datepicker 24 34
Error in formula not increasing value 2 13
Recently Microsoft released a brand new function called CONCAT. It's supposed to replace its predecessor CONCATENATE. But how does it work? And what's new? In this article, we take a closer look at all of this - we even included an exercise file for…
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…
This Micro Tutorial will demonstrate in Google Sheets how to use the HYPERLINK function to create live links inside your spreadsheet.
This Micro Tutorial will demonstrate how to create pivot charts out of a data set. I also added a drop-down menu which allows to choose from different categories in the data set and the chart will automatically update.

911 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