Formatting Spaces in Cell with VBA

lsteffen

Board Regular
Joined
May 11, 2006
Messages
111
Good morning,

Can someone please help with this code?

Dim NumSpaces As Long
NumSpaces = 14
Range("G1").Value = [B1].Value & Application.Rept(" ", NumSpaces) & [B1].Value

This is giving me an ending value of

650********650

and it should be giving me

***********650

Where the * are spaces.

Thank you,
Liz
 

Excel Facts

Return population for a City
If you have a list of cities in A2:A100, use Data, Geography. Then =A2.Population and copy down.
What else would you be expecting? Your code line is this
[B1].Value & Application.Rept(" ", NumSpaces) & [B1].Value
so you have 650 twice (presumably what is in cell B1), separated by the spaces.

instead, do this
Range("G1").Value = Application.Rept(" ", NumSpaces) & [B1].Value
 
Upvote 0
Thank you for your reply. When I update my code to what you suggest, it does not put in the spaces.

Liz
 
Upvote 0
I'm sure Tom is about to respond, but if G1 isn't formatted as Text, it will drop the preceeding zeroes.

If you don't want to worry about formatting, you could alter your code to
(untested)
Code:
"'" & Application.Rept(" ", NumSpaces) & [B1].Value
 
Upvote 0

Forum statistics

Threads
1,224,566
Messages
6,179,550
Members
452,927
Latest member
rows and columns

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