Solved

Ampersand commas and quotations in VBA formula

Posted on 2014-12-04
3
175 Views
Last Modified: 2014-12-04
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
Comment
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
  • Learn & ask questions
3 Comments
 

Author Comment

by:farmingtonis
ID: 40481275
Sorry, forgot to put the = before the if.
0
 
LVL 37

Accepted Solution

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

Expert Comment

by:Nick67
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

Open in new window


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

Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

Question has a verified solution.

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

This article describes a technique for converting RTF (Rich Text Format) data to HTML and provides C++ source that does it all in just a few lines of code. Although RTF is coming to be considered a "legacy" format, it is still in common use... po…
This article surveys and compares options for encoding and decoding base64 data.  It includes source code in C++ as well as examples of how to use standard Windows API functions for these tasks. We'll look at the algorithms — how encoding and decodi…
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 brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…

724 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question