[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
Solved

# Ampersand commas and quotations in VBA formula

Posted on 2014-12-04
Medium Priority
186 Views
Hi

I have a this formula that is part of my manual code:

=If(A2=A1,B1&", "&C2,C2)

I want to convert this to a VBA process and I am having trouble with the literals & and " in the output.   Here is what I am trying and it isn't working.

Range("B2:B" & lastrow).Formula = "IF(A2=A1,B1&" & "", "" &  "&C2" & "," & "C2)

It isn't working and I am just not good at putting in the literals.  Help please.
0
Question by:farmingtonis
[X]
###### Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

• Help others & share knowledge
• Earn cash & points

Author Comment

ID: 40481275
Sorry, forgot to put the = before the if.
0

LVL 37

Accepted Solution

Neil Russell earned 2000 total points
ID: 40481296
=Range("B2:B" & lastrow).Formula = "IF(A2=A1,B1&" & """, """ &  "&C2" & "," & "C2")
Is that better?
0

LVL 26

Expert Comment

ID: 40481328
@Neilsr's looks good.
In VBA Chr(34) is your friend
It is a double quote
=If(A2=A1,B1&", "&C2,C2)
I Think
=Range("B2:B" & lastrow).Formula = "IF(A2=A1,B1&" & " & chr(34) & ", " & chr(34) & " &  " & C2 ,C2)"
is right

Try
``````Dim theFormula as string
theFormula =  "IF(A2=A1,B1" 'The first bit
theFormula =theFormula & " & " 'The first ampersand
theFormula =theFormula & chr(34) & ", " & chr(34) 'quotes with a space and comma in them
theFormula =theFormula & " & " 'the second ampersand
theFormula =theFormula & ",C2)" ' the end bit
Range("B2:B" & lastrow).Formula =theFormula
``````

You can then messagebox between each line as you test and you can watch it being built up and catch at each part where something may go wrong.
0

## Featured Post

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A theme is a collection of property settings that allow you to define the look of pages and controls, and then apply the look consistently across pages in an application. Themes can be made up of a set of elements: skins, style sheets, images, and o…
Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …
###### Suggested Courses
Course of the Month13 days, 10 hours left to enroll