Addinf Spaces

clares

Well-known Member
Joined
Mar 14, 2002
Messages
557
Hi There

Is there anyone that can give me assistance. I am preparing a large file for importing and I need to make sure that each cell in my range contains precisly 68 characters including spaces. I have this piece of code but can not work out how to get it to add the spaces. I have tried using the replacement:="(Spaces)" to create the required spaces but is not working?

Sub Add_Spaces()
Application.ScreenUpdating = False
Dim SelectedCell As Range
Dim Letters As String
Dim Spaces As String


For Each SelectedCell In Selection
Letters = ActiveCell.Characters.Count
Spaces = 68 - Letters
Selection.Replace What:="", Replacement:="(Spaces)", LookAt:=xlPart, _
SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=False
MsgBox Spaces
Next
Application.ScreenUpdating = True
End Sub

As per usual any help would be gratfully received.

Kind regards

Peter
 

Excel Facts

Lock one reference in a formula
Need 1 part of a formula to always point to the same range? use $ signs: $V$2:$Z$99 will always point to V2:Z99, even after copying
Code:
Sub ReplaceSpace()
Dim cl As Range
For Each cl In Selection
    x = 68 - Len(cl)
    cl = cl & Application.WorksheetFunction.Rept(" ", x)
Next cl
End Sub

lenze
 
Upvote 0
There are a couple of VBA functions that will do that without resorting to a worksheet function.


Code:
Sub ReplaceSpace()
Dim cl As Range, x As Long
For Each cl In Selection
    x = 68 - Len(cl)
    cl = cl & Space(x)
Next cl
End Sub
 
Upvote 0
Code:
Sub replacespace2()
Dim c As String * 68
Dim cl As Range
For Each cl In Selection
    c = cl: cl = c
Next
End Sub
 
Upvote 0
Hi Hotpepper

I understand the replacespace code but would you be kind enough to explain replacespace2 to me?

Kind regards

Peter
 
Upvote 0
Sure,

I've dimmed the variable c as a fixed length string of 68 characters.
I'm going to assign the value of the cell to this string, which will fill out the ununsed characters with space to the right to reach the fixed length of 68 characters. I then reassign this padded value back to the cell.
 
Upvote 0

Forum statistics

Threads
1,215,063
Messages
6,122,935
Members
449,094
Latest member
teemeren

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top