?
Solved

Ampersand commas and quotations in VBA formula

Posted on 2014-12-04
3
Medium Priority
?
180 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 2000 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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

After several hours of googling I could not gather any information on this topic. There are several ways of controlling the USB port connected to any storage device. The best example of that is by changing the registry value of "HKEY_LOCAL_MACHINE\S…
Entering time in Microsoft Access can be difficult. An input mask often bothers users more than helping them and won't catch all typing errors. This article shows how to create a textbox for 24-hour time input with full validation politely catching …
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 we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…

762 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