0

I have a variable a = "C2" and variable b = "C3"

I need to do something like

ws.Range("A2").Value = "=" & a &"" ""& b

which should return:

=C2&" "&C3

in an excel formula however I cant seem to get this to work anyone have a solution??

2
  • 2
    Are you trying to get your cell to have the formula =C2&" "&C3, or do you want it to show the values of C2 and C3, separated by a space? Try ws.Range("A2").Formula = "=" & a & "&"" ""&" & b. If you want the values of your variables, separated by a space, use ws.Range("A2").Value = a & " " & b. Commented Jan 13, 2016 at 21:38
  • 1
    @BruceWayne this is the correct answer the first one worked great. Thanks! Commented Jan 13, 2016 at 22:05

1 Answer 1

2

From my comment,

ws.Range("A2").Formula = "=" & a & "&"" ""&" & b

This works because you need to use .Formula instead of .Value.

Sign up to request clarification or add additional context in comments.

3 Comments

Actually also works with .Value interestingly enough
@TylerCowan - UUhmmm, I'm just going to ignore that then, to keep my sanity :P I thought I was getting the hang of VBA, and then you tell me I can do .Value just the same. If someone can comment as to why, that'd be appreciated! Perhaps it's because you're using an = at the start, so of course, when Excel sees an = at the start, it uses it as a formula. I wonder then, what the "real" big difference in using .Value and .Formula for setting formulas is.
Imagine you have integers, doubles for certain calculations and if you use .formula you gonna get screwed. Because .FORMULA always return a String. (Any value set to .formula without is a plain value not a formula) Unless you have exception handling and the extras ready in your code. Whereas .VALUE returns your data's data type as you expect them to be. In terms of retrieving formula: Value is one way road in case to set formula but not to retrieve. stackoverflow.com/questions/13687800/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.