Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

add space to end of cell content

Posted on 2015-02-16
11
Medium Priority
?
80 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
[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
11 Comments
 
LVL 23

Accepted Solution

by:
Michael Fowler earned 2000 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:Michael Fowler
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
Get your Conversational Ransomware Defense e‑book

This e-book gives you an insight into the ransomware threat and reviews the fundamentals of top-notch ransomware preparedness and recovery. To help you protect yourself and your organization. The initial infection may be inevitable, so the best protection is to be fully prepared.

 

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:Michael Fowler
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 33

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 49

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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

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

New style of hardware planning for Microsoft Exchange server.
Explore the ways to Unlock VBA Project Password Excel 2010 & 2013 documents. Go through the article and perform the steps carefully to remove VBA Excel .xls file.
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…

609 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