Add space to end of string using VBA

DougStroud

Well-known Member
Joined
Aug 16, 2005
Messages
2,968
I am attempting to add a space to each string w/ this line of code, but get compile error, expected end of statement. Highlights the "=" sign after .Value

Code:
For Each c In Range("D4:D" & LRow).Value = "=(c.value & " ")"

Thx,

ds
 

Excel Facts

What is the shortcut key for Format Selection?
Ctrl+1 (the number one) will open the Format dialog for whatever is selected.

Yogi Anand

MrExcel MVP
Joined
Mar 12, 2002
Messages
11,454
Hi Doug Stroud:

How about using ...
Code:
    For Each c In Range("D4:D" & LRow)
        c.Formula = c & " "
    Next c

Does this work for you?
 

jindon

MrExcel MVP
Joined
Aug 21, 2004
Messages
16,995
Hi
try
Code:
With Range("d4:d" & LRow)
     .Value = Evaluate(.Address & "&"" """)
End With
 

DougStroud

Well-known Member
Joined
Aug 16, 2005
Messages
2,968
Hello Yogi and Jindon,
Thanks for the your answers. Both worked just fine.

I am interested in a little more info on both procedures please gentlemen.
Yogi, you chose to use the Formula object rather than Value. I was expecting to see the formula to be written to the sheet rather than value, but this is not the case, in fact it turned out the same results and the same values as using the Value object. Could you elaborate on this, and if there are any benefits to using .Formula or .Value?

Jindon,
your code goes a step further by using a two functions- Yogi's and yours deliver the desired results, I am curious if yours adds a benefit; maybe not in this case but in other more complicated cases. Regardless, it is interesting to see it written/approached from a different direction- to see how to make use of other functions.

The other question I would like to pose is why my initial syntax failed. What is the rule(s) regarding procedures? I thought this one was a one liner for sure. I have a few VBA books, so maybe it is in there, I will hunt through them, but if any online info is available and you can point me there I would appreciate this.

Lastly, I accidentally posted this question twice last night. It did not appear to go through initally. I hit submit again and discovered this morning it had been sent twice, my apologies. Here is the link to the other post- http://www.mrexcel.com/board2/viewtopic.php?t=283798
Owen's post proved to be successful as well- Thanks Owen.

Cheers,

Doug
 

Norie

Well-known Member
Joined
Apr 28, 2004
Messages
76,357
Office Version
  1. 365
Platform
  1. Windows
Doug

Firstly Formula and Value aren't objects, there properties.:)

The reason your original code wouldn't work is because the syntax was wrong.
 

jindon

MrExcel MVP
Joined
Aug 21, 2004
Messages
16,995
Jindon,
your code goes a step further by using a two functions- Yogi's and yours deliver the desired results, I am curious if yours adds a benefit; maybe not in this case but in other more complicated cases. Regardless, it is interesting to see it written/approached from a different direction- to see how to make use of other functions.

Using Evaluate method;

1) No loop reauired
2) No working column needed
3) Not adjustable to a complex situation
4) Not easy to read

This is useful only for an easy operation like
Code:
Sub test()
With Range("a1:a5")
     .Value = [{"A";"B";"C";"D";"E"}]
     MsgBox "Populated A1:A5"
     .Value = Evaluate("a1:a5&""x""") ' is equal to [a1:a5&""x""]
     MsgBox "added an x to all the cells"
End With
End Sub
 

Forum statistics

Threads
1,181,055
Messages
5,927,863
Members
436,573
Latest member
CMR237

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
Top