Solved

Ampersand commas and quotations in VBA formula

Posted on 2014-12-04
3
153 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
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

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

This article describes how to add a user-defined command button to the Windows 7 Explorer toolbar.  In the previous article (http://www.experts-exchange.com/A_2172.html), we saw how to put the Delete button back there where it belongs.  "Delete" is …
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…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

743 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now