5 Replies - 1064 Views - Last Post: 17 August 2012 - 08:14 AM Rate Topic: -----

#1 achavez  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 16-August 12

Adding spaces to excel using VBA

Posted 17 August 2012 - 06:31 AM

Hi Guys,

I am trying to run a loop through a range ex C4:c15 where it is populated with characters.
I would like to create a loop that populates each text by adding 6 extra spaces at the end.
Thanks for your replies in advance!
Is This A Good Question/Topic? 0
  • +

Replies To: Adding spaces to excel using VBA

#2 maj3091  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 300
  • View blog
  • Posts: 1,776
  • Joined: 26-March 09

Re: Adding spaces to excel using VBA

Posted 17 August 2012 - 07:17 AM

Please post the code of what you have tried so far using the [Code ] tags.
Was This Post Helpful? 0
  • +
  • -

#3 achavez  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 16-August 12

Re: Adding spaces to excel using VBA

Posted 17 August 2012 - 07:32 AM

Dim rng As Range
rng = Worksheets("Sheet1").Range("C4:C15")
Dim val As String
Dim cell As Range

For Each cell In rng
val = cell.Value
val = val & " "
cell.Value = val
Next cell
End Sub
Was This Post Helpful? 0
  • +
  • -

#4 maj3091  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 300
  • View blog
  • Posts: 1,776
  • Joined: 26-March 09

Re: Adding spaces to excel using VBA

Posted 17 August 2012 - 08:11 AM

I'm not that familiar with VBA, so can you tell me what it isn't doing?

I can see you're trying to loop around the range and add a space to the cell value and update it again.

So what does it actually do? What do you get written back to your cells?

Are the cells formatted so that you can display them as strings (extra spaces).

This post has been edited by maj3091: 17 August 2012 - 08:12 AM

Was This Post Helpful? 0
  • +
  • -

#5 achavez  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 16-August 12

Re: Adding spaces to excel using VBA

Posted 17 August 2012 - 08:13 AM

Hey DIC!
I just figured it out!

i had to write "set" before RNG..so

set rng = Worksheets("Sheet1").Range("C4:C15")

This will add 6 additional spaces to the cells.
Thanks very much!
Was This Post Helpful? 0
  • +
  • -

#6 maj3091  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 300
  • View blog
  • Posts: 1,776
  • Joined: 26-March 09

Re: Adding spaces to excel using VBA

Posted 17 August 2012 - 08:14 AM

Glad you sorted it out and thanks for posting your solution for others.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1